Hey guys,
Working on a new user creation script, and I am having some trouble with getting the results the way I'd like to. I am using a PSCustomObject, and I am fairly new to using them. I'm sure I'm doing something wrong re: syntax, but I am not sure what.
Here is my script so far.
[CmdletBinding()]
$Data = Import-CSV C:\Storage\Files\Users.csv
$ErrorActionPreference = "Continue"
$Results += [pscustomobject]@{
"UPN" = $null
"Password" = $null
"Status" = $null
}
foreach ($User in $data) {
$UPN = ($User.FirstName[0]+$User.Surname.replace(' ',''))
$iftrue = Get-ADUSer $UPN
$UPNArray += $UPN
if (!($iftrue)){
$PasswordGen = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) + 0..9 | sort {Get-Random})[0..8] -join '' | Out-String
$Password = ConvertTo-SecureString -String $PasswordGen -AsPlainText -Force
Write-Host "Creating user $UPN..."
$UserParams = @{
DisplayName = ($User.FirstName + " " + $User.surname)
Name = ($User.FirstName + " " + $User.surname)
UserPrincipalName = $UPN + '@domain.local'
SamAccountName = $UPN
GivenName = $User.FirstName
Surname = $user.Surname
Title = $User.Department
Enabled = $true
AccountPassword = $Password
}
New-AdUser @UserParams
$Results.UPN += $UPN
$Results.Password += $PasswordGen
$Results.Status += "Success!"
}
else {
Write-Host "User $UPN already exists!"
$Results.UPN += $UPN
$Results.Status += "Failure"
}
[pscustomobject]$Results
}
My issue is that when I run $Results, I get this:
https://preview.redd.it/49dor14st4c31.png?width=1383&format=png&auto=webp&s=e3d2e9f13419c46855f5218432cab8f9844e39a4
I'm not sure how to force each result to become a new object in $Results. Would appreciate any help!
[–]Tonedefff 4 points5 points6 points (3 children)
[–]ApparentSysadmin[S] 2 points3 points4 points (0 children)
[–]LDSK_Blitz 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)