Author Posts

August 4, 2017 at 3:21 pm

Hello,

I have been trying to convert an all caps string to title case, and it works, but not as part of a New-ADUser routine.

If I do not include the conversion, the script works fine. Here is part of a ForEach-Object loop...

$params = @{

$preferredname = (Get-Culture).TextInfo.ToTitleCase($_.PreferredName.ToLower())
$lastname = (Get-Culture).TextInfo.ToTitleCase($_.LastName.ToLower())

                    'Name' = $preferredname + ' ' + $lastname
                    'Surname' = $lastname
                    'DisplayName' = $_.DisplayName
                    'GivenName' = $preferredname
                    'EmailAddress' = $_.EmailAddress
                    'SamAccountName' = $_.UserLogonName
                    'UserPrincipalName' = ($_.UserLogonName + '@domain')
                    'Title' = ($_.WorkDuties)
                    'Department' = ($_.Loc_Name)
                    'HomeDrive' = "H:"
                    'HomeDirectory' = ($_.HomeServer + '\HOME\' + $_.UserLogonName)
                    'Path' = $path
                    'AccountPassword' = (ConvertTo-SecureString "$($genPass)" -AsPlainText -force)
                    'ChangePasswordAtLogon' = $true
                    'Enabled' = $true
                    'OtherAttributes' = @{'Info'="$($genPass)"}

                    }
                    
                    Try {New-ADUser @params -ErrorAction Continue
                         $successUsers += "$($_.PreferredName) $($_.LastName) - $($_.WorkDuties) at $($_.Loc_Name). $($genPass) `r`n"}
                    
                    Catch {$usersErr += "$($_.Name) - $($_.WorkDuties) at $($_.Loc_Name). `r`n"} 
                       
                    } -ErrorAction SilentlyContinue -ErrorVariable +usersErrException

Doing this causes the Try New-ADUser to fail. Removing this and just using 'Surname' = $_.LastName, for example, works.

August 5, 2017 at 8:34 am

What error messages do you get?
have you checked the contents of $preferredname and $lastname?

August 5, 2017 at 8:52 am

Try moving these 2 lines
$preferredname = (Get-Culture).TextInfo.ToTitleCase($_.PreferredName.ToLower())
$lastname = (Get-Culture).TextInfo.ToTitleCase($_.LastName.ToLower())

outside of the hashtable

August 7, 2017 at 12:51 pm

Before posting this here, I had already tried moving those 2 lines just above the hashtable. It didn't work.

After your suggestion I started to wonder why it shouldn't, so I tried again this morning and it worked fine.

I am not sure what I did before, but it works.

Thanks