need help with PS script
hello new ps , scripting am trying to learn exchange server 2010 using a training dvd that included script will create 300 users when i execute the script am prompted to chose the csv file after tht i get following error each time tries creat user:-
you cannot call method on null-valued expression.
@ c:\aaa\createusersfromcsv.ps1:78 char:31
+ $labuser.psbase.commitchanges <<<< ()
+ categoryinfo : invalidoperation: (commitchanges:string) [], runtimeexception
+ fullyqualifiederrorid : invokemethodonnull
here script:
# powershell script creates users sample csv file active directory
# version 1.0
# author: andy grogan
# http://www.telnetport25.com
#
# compatible with:
# powershell 1.0 , 2.0
# windows 2003
# windows 2008
# windows 2008 r2
#
function select-filedialog
{
param([string]$title,[string]$directory,[string]$filter="csv files (*.csv)|*.csv")
[system.reflection.assembly]::loadwithpartialname("system.windows.forms") | out-null
$objform = new-object system.windows.forms.openfiledialog
$objform.initialdirectory = $directory
$objform.filter = $filter
$objform.title = $title
$objform.showhelp = $true
$show = $objform.showdialog()
if ($show -eq "ok")
{
return $objform.filename
}
else
{
exit
}
}
$filename = select-filedialog -title "import csv file" -directory "c:\"
$exchangeusersou = "ou=exchangeusers"
$domain = [system.directoryservices.activedirectory.forest]::getcurrentforest()
$domaindn = (([system.directoryservices.activedirectory.forest]::getcurrentforest()).domains | ? {$_.name -eq $domain}).getdirectoryentry().distinguishedname
$final = "ldap://$domaindn"
$domainpath = [adsi]"$final"
$cou = $domainpath.create("organizationalunit",$exchangeusersou)
$cou.setinfo()
$userinformation = import-csv $filename
$oupath = "ldap://$exchangeusersou,$domaindn"
$userpath = [adsi]"$oupath"
write-host "---------------------------------------------------------------"
write-host "creating lab users"
write-host ""
write-host "---------------------------------------------------------------"
foreach ($user in $userinformation){
$cn = $user.samaccountname
$sn = $user.surname
$given = $user.givenname
$samaccountname = $user.samaccountname
$display = $user.displayname
$labuser = $userpath.create("user","cn=$cn")
write-host "creating user: $user.samaccountname"
$labuser.put("samaccountname",$samaccountname)
$labuser.put("sn",$sn)
$labuser.put("givenname",$given)
$labuser.put("displayname",$display)
$labuser.put("mail","$samaccountname@$domain")
$labuser.put("description", "lab user - created via script")
$labuser.put("userprincipalname","$samaccountname@$domain")
$labuser.setinfo()
$pwrd = $user.password
$labuser.psbase.invoke("setpassword",$pwrd)
$labuser.psbase.invokeset("accountdisabled",$false)
$labuser.psbase.commitchanges()
}
write-host "script completed"
please me fix it.
your csv file must not have header line defining fields. seems every field of file not recognized. example, first error indicates $samaccountname has no value (which explains subsequent errors raised setinfo , commit changes). means statement assigned $null variable:
$samaccountname = $user.samaccountname
which means csv file has no header "samaccountname". first line of file must define of fields. does help?
richard mueller - mvp directory services
Windows Server > Windows PowerShell
Comments
Post a Comment