Hi, could anyone please tell me why the following script is not running?
$users = import-csv "C:\script\userlist.csv"
$container = [ADSI] "LDAP://cn=users,dc=we,dc=int"
$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("P@55w0rd")
}
I receive the following errors:
Exception calling "Create" with "2" argument(s): "An invalid directory pathname was passed
"
At C:\Script\addusers.ps1:5 char:33
+ $newUser = $container.Create <<<< ("User", "cn=" + $UserName)
+ CategoryInfo : NotSpecified: ) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI
You cannot call a method on a null-valued expression.
At C:\Script\addusers.ps1:6 char:17
+ $newUser.Put <<<< ("sAMAccountName", $UserName)
+ CategoryInfo : InvalidOperation: (Put:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Script\addusers.ps1:7 char:30
+ $newUser.psbase.InvokeSet <<<< ('AccountDisabled', $false)
+ CategoryInfo : InvalidOperation: (InvokeSet:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Script\addusers.ps1:8 char:25
+ $newUser.SetPassword <<<< ("P@55w0rd")
+ CategoryInfo : InvalidOperation: (SetPassword:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Script\addusers.ps1:9 char:21
+ $newUser.SetInfo <<<< ()
+ CategoryInfo : InvalidOperation: (SetInfo:String) [], RuntimeException
$users = import-csv "C:\script\userlist.csv"
$container = [ADSI] "LDAP://cn=users,dc=we,dc=int"
$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("P@55w0rd")
}
I receive the following errors:
Exception calling "Create" with "2" argument(s): "An invalid directory pathname was passed
"
At C:\Script\addusers.ps1:5 char:33
+ $newUser = $container.Create <<<< ("User", "cn=" + $UserName)
+ CategoryInfo : NotSpecified: ) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI
You cannot call a method on a null-valued expression.
At C:\Script\addusers.ps1:6 char:17
+ $newUser.Put <<<< ("sAMAccountName", $UserName)
+ CategoryInfo : InvalidOperation: (Put:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Script\addusers.ps1:7 char:30
+ $newUser.psbase.InvokeSet <<<< ('AccountDisabled', $false)
+ CategoryInfo : InvalidOperation: (InvokeSet:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Script\addusers.ps1:8 char:25
+ $newUser.SetPassword <<<< ("P@55w0rd")
+ CategoryInfo : InvalidOperation: (SetPassword:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Script\addusers.ps1:9 char:21
+ $newUser.SetInfo <<<< ()
+ CategoryInfo : InvalidOperation: (SetInfo:String) [], RuntimeException