INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Basic script not running

Basic script not running

(OP)
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

RE: Basic script not running

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close