Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic script not running

Status
Not open for further replies.

beefstew

Technical User
Oct 27, 2002
124
GB
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
 
Several methods I've run across won't automatically evaluate expressions such as what you have in your Create ("cn=" + $UserName). If you're working with strings you can see if the PowerShell automatic string expansion will get it, or create the full string before you call the method. See if either of these works:
Code:
$newUser = $container.Create("User", "cn=$UserName")
Code:
$CNUsername = "cn=$UserName"
$newUser = $container.Create("User", $CNUsername)
Since the Create call failed, the other calls didn't have anything to work with. I bet if the Create call works, the rest will too
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top