New-ADUser -path variable breaks script

This topic contains 3 replies, has 2 voices, and was last updated by  Creed Cordonier 3 months ago.

  • Author
    Posts
  • #71203

    Creed Cordonier
    Participant

    Hello,

    I have New-ADUser in a ForEach-Object loop. It works great until I try to set the -path using variables. The variable values come from a CSV I am importing...

    $newStaff | ForEach-Object {
    
        New-ADUser  -Name ($_.FirstName + ' ' + $_.LastName) `
                    -Surname $_.LastName `
                    -DisplayName $_.DisplayName `
                    -GivenName $_.FirstName `
                    -EmailAddress $_.EmailAddress `
                    -SamAccountName $_.UserLogonName `
                    -UserPrincipalName ($_.UserLogonName + '@domain.com') `
                    -Title ($_.PositionTitle) `
                    -HomeDrive "H" `
                    -HomeDirectory ($_.HomeServer + '\home\' + $_.UserLogonName) `
                    -Path "OU=TS,OU=STAFF,OU=User Accounts,DC=mydomain,DC=mydomain" `
                    -AccountPassword (ConvertTo-SecureString "temppassword" -AsPlainText -force) `
                    -ChangePasswordAtLogon $true `
                    -Enabled $true `
    
                    }

    The above works. The below does not. Only difference is in the -path statement...

    -Path "OU=$_.LocationOU,OU=$_.ElemOrSecOU,OU=$_.CertOU,OU=User Accounts,DC=rqs,DC=C2" 

    $_.LocationOU is a column in $newStaff. The new users will be going to different nested OU's.

    I have tried loading $_.LocationOU into $location, and using -path "OU=$location, etc. but I get the error...

    New-ADUser : The object name has bad syntax

    The error is on the -Name property.

    Any advice appreciated. Thank you!

  • #71207

    Don Jones
    Keymaster

    1. Look into splitting ;). It'll make that a lot more readable and let you get rid of the back ticks.

    2. As the first thing in your ForEach loop, construct a variable $path with whatever you want the path to be. Then, Write-Host $path before running New-ADUser. I'm looking to see what that variable actually ends up containing.

  • #71210

    Don Jones
    Keymaster

    Oh, LOL. Never mind. I just figured it out. Needed to stare an extra minute.

    You can't do "$_.Something". If you do the $path suggestion I just posted, you'll see why. You need "$($_.Something)" instead – using a subexpression is the only way to refer to a property of $_ within a double-quoted string. The way you're doing it, the period signals the end of the variable name, and ".LocationOU" is being taken as a literal string.

    • #71213

      Creed Cordonier
      Participant

      The sub-expression did it =). I was hot on the trail, but it appears the variable was basically being 'quoted out'?

      Yes, .LocationOU was being taken as the literal string. Good catch. I set it up in $path and that is what it showed.

      Using $($_.LocationOU) worked.

      Thank you!

You must be logged in to reply to this topic.