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

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Windows 2016 RDS event 1306 Connection Broker Client failed to redirect the user... Error: NULL