ToTitleCase variable with New-ADUser

This topic contains 3 replies, has 2 voices, and was last updated by  Creed Cordonier 1 week, 5 days ago.

  • Author
    Posts
  • #76710

    Creed Cordonier
    Participant

    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.

  • #76747

    Richard Siddaway
    Moderator

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

  • #76750

    Richard Siddaway
    Moderator

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

    outside of the hashtable

    • #76846

      Creed Cordonier
      Participant

      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

You must be logged in to reply to this topic.