none
Unable to create users from .csv file RRS feed

  • Question

  • Hi, I have the following script that shoud create one user but for some reason its not working

    $users = import-csv "C:\script\userlist.csv"
    $objADSI = [ADSI]“LDAP://OU=*Users,DC=corp,DC=we”
    $users | foreach {
     $UserName = $_.UserName
     $objUser = $objADSI.Create("User", "cn=$UserName")
     $objUser.Put(“sAMAccountName”, $UserName)
     $objUser.setInfo()
     $objUser.SetPassword("Password1")
     $objUser.psbase.InvokeSet('AccountDisabled', $false)
     $objUser.setInfo()

    }

    Its bombong out with the following error.... Could anyone shed some light on this please

    Exception calling "Create" with "2" argument(s): "An invalid directory pathname was passed
    "
    At C:\Script\addusers1.ps1:5 char:28
    +  $objUser = $objADSI.Create <<<< ("User", "cn=$UserName")
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

     

    Thursday, October 6, 2011 7:41 AM

Answers

  • PS C:\scripts> GC users.csv
    UserName
    Mike
    John
    Gert
    Jens
    Aage

    ===================================

    PS C:\scripts> Import-Csv users.csv | select username

    UserName                                                                                                                                     
    --------                                                                                                                                     
    Mike                                                                                                                                         
    John                                                                                                                                         
    Gert                                                                                                                                         
    Jens                                                                                                                                         
    Aage       

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Marked as answer by Beefstew123 Thursday, October 6, 2011 10:12 AM
    Thursday, October 6, 2011 10:00 AM
  • You can simply add it as an additional property (the account isn't much use without this, actually!):

     

    $users = Import-Csv users.csv
    $objADSI = [ADSI]“LDAP://OU=*Users,DC=contoso,DC=internal”
     $users | foreach {
      $UserName = $_.UserName
      $username
      $objUser = $objADSI.Create("User", "cn=$UserName")
      $objUser.Put(“sAMAccountName”, $UserName)
      $objUser.setInfo()
      $objUser.Put("userPrincipalName", "$UserName@contoso.internal")
      $objUser.setInfo()
      $objUser.SetPassword("Password1")
      $objUser.psbase.InvokeSet('AccountDisabled', $false)
      $objUser.setInfo()
     
    }
    
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"


    • Edited by Bigteddy Thursday, October 6, 2011 11:17 AM
    • Marked as answer by Beefstew123 Thursday, October 6, 2011 11:45 AM
    Thursday, October 6, 2011 11:08 AM

All replies

  • It looks like there may be a problem with the LDAP path.  Are you sure the OU=*Users ?  Is it not a CN?
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Thursday, October 6, 2011 8:08 AM
  • No that is correct, it is a manually created OU and not the default users container
    Thursday, October 6, 2011 8:29 AM
  • This works for me:

     

    $users = Import-Csv  "C:\script\userlist.csv"
    $objADSI = [ADSI]“LDAP://CN=Users,DC=corp,DC=we” $users | foreach { $UserName = $_.UserName $username $objUser = $objADSI.Create("User", "cn=$UserName") $objUser.Put(“sAMAccountName”, $UserName) $objUser.setInfo() $objUser.SetPassword("Password1") $objUser.psbase.InvokeSet('AccountDisabled', $false) $objUser.setInfo() }

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"

    • Edited by Bigteddy Thursday, October 6, 2011 8:31 AM
    Thursday, October 6, 2011 8:30 AM
  • I can't reproduce your error. I created an OU called *Users, and ran the script as you posted it originally. 

    It created the users correctly in the OU.  I can only think there is something else wrong with your LDAP path.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Thursday, October 6, 2011 8:50 AM
  • Could you paste the CSV file you created for this please as the issue maybe with that?

    Thanks

    Thursday, October 6, 2011 9:33 AM
  • PS C:\scripts> GC users.csv
    UserName
    Mike
    John
    Gert
    Jens
    Aage

    ===================================

    PS C:\scripts> Import-Csv users.csv | select username

    UserName                                                                                                                                     
    --------                                                                                                                                     
    Mike                                                                                                                                         
    John                                                                                                                                         
    Gert                                                                                                                                         
    Jens                                                                                                                                         
    Aage       

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Marked as answer by Beefstew123 Thursday, October 6, 2011 10:12 AM
    Thursday, October 6, 2011 10:00 AM
  • Hi BigTeddy,

    Just one more thing, if you dont mind. How would I then go about adding say the User Principle name to these newly created accounts? Would I be able to incorporate it into this script or would I have to create another?

     

    Cheers

    Thursday, October 6, 2011 10:29 AM
  • You can simply add it as an additional property (the account isn't much use without this, actually!):

     

    $users = Import-Csv users.csv
    $objADSI = [ADSI]“LDAP://OU=*Users,DC=contoso,DC=internal”
     $users | foreach {
      $UserName = $_.UserName
      $username
      $objUser = $objADSI.Create("User", "cn=$UserName")
      $objUser.Put(“sAMAccountName”, $UserName)
      $objUser.setInfo()
      $objUser.Put("userPrincipalName", "$UserName@contoso.internal")
      $objUser.setInfo()
      $objUser.SetPassword("Password1")
      $objUser.psbase.InvokeSet('AccountDisabled', $false)
      $objUser.setInfo()
     
    }
    
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"


    • Edited by Bigteddy Thursday, October 6, 2011 11:17 AM
    • Marked as answer by Beefstew123 Thursday, October 6, 2011 11:45 AM
    Thursday, October 6, 2011 11:08 AM