Creating lots of users in AD with powershell woes.


guys, trying create 117 accounts in active directory ps script.  dug script through internet research , posts following positive in it's ability create accounts.  i, however, have not had such luck.  thought go straight source , see if help.  script running is:

$users = import-csv "c:\userstobecreated.csv"
$container = [adsi] "ldap://ou=lowusers,dc=develop,dc=ds,dc=amrdec,dc=army,dc=mil"
$users | foreach {
    $username = $_.username
    $newuser = $container.create("user", "cn=" + $username)
    $newuser.put("samaccountname", $username)
    $newuser.setinfo()
    $newuser.psbase.invokeset('accountdisabled', $false)
    $newuser.setinfo()
    $newuser.setpassword("passw0rdpa%%w)rd")
}

and error is:


ps c:\windows\system32> powershell -executionpolicy bypass -file c:\users\hamakerrd\desktop\test1.ps1
exception calling "create" "2" argument(s): "an invalid directory pathname passed
"
@ c:\users\hamakerrd\desktop\test1.ps1:5 char:33
+     $newuser = $container.create <<<< ("user", "cn=" + $username)
    + categoryinfo          : notspecified: (:) [], methodinvocationexception
    + fullyqualifiederrorid : catchfrombaseadaptermethodinvoketi

you cannot call method on null-valued expression.
@ c:\users\hamakerrd\desktop\test1.ps1:6 char:17
+     $newuser.put <<<< ("samaccountname", $username)
    + categoryinfo          : invalidoperation: (put:string) [], runtimeexception
    + fullyqualifiederrorid : invokemethodonnull

you cannot call method on null-valued expression.
@ c:\users\hamakerrd\desktop\test1.ps1:7 char:21
+     $newuser.setinfo <<<< ()
    + categoryinfo          : invalidoperation: (setinfo:string) [], runtimeexception
    + fullyqualifiederrorid : invokemethodonnull

you cannot call method on null-valued expression.
@ c:\users\hamakerrd\desktop\test1.ps1:8 char:30
+     $newuser.psbase.invokeset <<<< ('accountdisabled', $false)
    + categoryinfo          : invalidoperation: (invokeset:string) [], runtimeexception
    + fullyqualifiederrorid : invokemethodonnull

you cannot call method on null-valued expression.
@ c:\users\hamakerrd\desktop\test1.ps1:9 char:21
+     $newuser.setinfo <<<< ()
    + categoryinfo          : invalidoperation: (setinfo:string) [], runtimeexception
    + fullyqualifiederrorid : invokemethodonnull

you cannot call method on null-valued expression.
@ c:\users\hamakerrd\desktop\test1.ps1:10 char:25
+     $newuser.setpassword <<<< ("passw0rdpa%%w)rd")
    + categoryinfo          : invalidoperation: (setpassword:string) [], runtimeexception
    + fullyqualifiederrorid : invokemethodonnull

any send way appreciated!!!


rory

i forgot 1 other possible problem. if common name (the value of cn attribute) contains of following characters, must escaped backslash escape character:

, \ # + < > ; / " =

-----

leading , trailing spaces must escaped. usually, comma causes problems. note these characters should not escaped if create users in aduc, in code. also, characters, commas, not escaped in samaccountname, on cn. not problem example names, thought mention it.

however, problem in case not using "run administrator" when run script. required on newer clients windows 7 , windows server 2008. when launch powershell run script, select "run administrator". otherwise, normal user permissions used, account error.


richard mueller - mvp directory services



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

some help on Event 540

WMI Repository 4GB limit - Win 2003 Ent Question

Event ID 1302 (error 1307) DFS replication service encountered an error while writing to the debug log file