In my Powershell script that I created earlier this year to create new domain user objects while also editing several attributes I ran into an issue here yesterday where I wanted to package it up as an executable for other members of IT here. Once converted using the PS2EXE script here, it'd fail to add the streetaddress, city, zip, and scriptpath attributes only. Very, very weird.
So in ISE the script would process 100% correctly, however in a Powershell session or after converting to an EXE it'd fail.
I posted to ExpertsExchange and only really got head scratches, and a couple comments about the organization of the script. I had a switch array before the actual $ value, which I swapped around. I had thought initially that maybe there was a limit to the number of values you could modify either in one line or in one cmdlet but that's just crazy talk. I tried breaking the New-ADuser part up over multiple lines but then it started missing other attribute values.
So I went looking for the correct method of busting up a long cmdlet and read about Splatting. Splatting didn't work for me.. not sure why but most likely I got the syntax wrong. So then I found the back tick (`) line continuation. This method actually worked.. so to show you before I had this:
New-ADUser -path $myOU -samaccountname $name -name $displayname -DisplayName $DisplayName -Surname $Surname -givenname $givenname -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -force) -enabled:$false -emailaddress $proxyaddress -Description $Description -Title $jobtitle -Office $office -UserPrincipalName $proxyaddress -Department $department -Company $company -StreetAddress $Street -city $city -state $state -PostalCode $zip -ScriptPath $scriptpath
All on one gigantic line.. Now though it's this:
New-ADUser -path $myOU -samaccountname $name -name $displayname -DisplayName $DisplayName -Surname $Surname -givenname $givenname `
-AccountPassword (ConvertTo-SecureString $Password -AsPlainText -force) -enabled:$false -emailaddress $proxyaddress `
-Description $Description -Title $jobtitle -Office $office -UserPrincipalName $proxyaddress -Department $department `
-Company $company -StreetAddress $Street -city $city -state $state -PostalCode $zip -ScriptPath $scriptpath
I know it's not MUCH different here but notice the back ticks.. those are breaking it up over 4 lines. Makes it incredibly easier to read and now my script works again. Now I'm not saying that breaking up this cmdlet over multiple lines actually fixed the missing variables but I'm glad I had to go through it.
No comments:
Post a Comment