Null valued expression
hi all,
new-ish powershell , have following script calling .csv file in same directory create 250 users test environment.
import-module activedirectory
$users = import-csv -delimiter "," -path "c:\multiusers\users.csv"
foreach ($user in $users)
{
$ou = "ou=users,dc=<domain>,dc=<com>"
$password = $user.passw0rd
$detailedname = $user.firstname + " " + $user.name
$userfirstname = $user.firstname
$firstletterfirstname = $userfirstname.substring(0,1)
$sam = $firstletterfirstname + $user.name
new-aduser -name $detailedname -samaccountname $sam -userprincipalname $sam -displayname $detailedname -givenname $user.firstname -surname $user.name -accountpassword (convertto-securestring $password -asplaintext -force) -enabled $true -path $ou
}
and following error:
convertto-securestring : cannot bind argument parameter 'string' because null.
@ c:\multiusers\multiusers.ps1:11 char:195
+ new-aduser -name $detailedname -samaccountname $sam -userprincipalname $sam -displayname $detailedname -givenname
$user.firstname -surname $user.name -accountpassword (convertto-securestring <<<< $password -asplaintext -force) -ena
bled $true -path $ou
+ categoryinfo : invaliddata: (:) [convertto-securestring], parameterbindingvalidationexception
+ fullyqualifiederrorid : parameterargumentvalidationerrornullnotallowed,microsoft.powershell.commands.converttose
curestringcommand
any idea why getting this? appreciated
many quick replies...
import-module activedirectory
$users = import-csv -delimiter "," -path "c:\multiusers\users.csv"
foreach ($user in $users)
{
$ou = "cn=users,dc=<domain>,dc=<com>"
$password = "passw0rd"
$detailedname = $user.firstname + " " + $user.lastname
$userfirstname = $user.firstname
$firstletterfirstname = $userfirstname.substring(0,1)
$sam = $firstletterfirstname + $user.lastname
write-host $detailedname
new-aduser -name $detailedname -samaccountname $sam -userprincipalname $sam -displayname $detailedname -givenname $user.firstname -surname $user.name -accountpassword (convertto-securestring $password -asplaintext -force) -enabled $true -path $ou
}
with csv column headers :
firstname | lastname | middleinitial | firstnamefirst | lastnamefirst |
worked
issues found:
changed ou cn - user error!
bigteddy's comment around password pointer - thx :) have set password users identical $password = "passw0rd"
$sam taking firstletterfirstname + $user.name when first letter of first name matched previous user failed duplicate: changed to $user.lastname
many gain folks
Windows Server > Windows PowerShell
Comments
Post a Comment