Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

It's my first time in PowerShell to create users from a csv file. I am using a code from a tutorial I've watched, it's working on the when demonstrated on the video but, I can't make it work..

$ou=[ADSI] "LDAP:/..."
$dataSource = import-csv ".."

foreach($dataRecord in $dataSource) 
{

  $cn = $dataRecord.cn
  $sAMAccountName = $dataRecord.sAMAccountName
  $givenName = $dataRecord.FirstName
  $sn = $dataRecord.LastName
  $displayName = $sn + ", " + $givenName
  $userPrincipalName = $givenName + "." + $sn + "@nuggetlab.com"
  $newUser =  $ou.Create("user", "cn=" + $cn)

  $newUser.Put("sAMAccountName", $sAMAccountName)
  $newUser.Put("userPrincipalName", $userPrincipalName)
  $newUser.Put("displayName", $displayName)
  $newUser.Put("givenName", $givenName)
  $newUser.SetInfo()
  $newUser.SetPassword("Password")
  $newUser.psbase.InvokeSet("AccountDisabled", $false)
  $newUser.Put("company", "The Fantastic Four")
  $newUser.SetInfo()
}

The error says:

Exception calling "SetInfo" with "0" arguments(s): "The specified directory service or value does not exist"
At C:\Users\Administrator\Documents\CreateGroup.ps1:18 char18
+   $newUser.SetInfo <<<< ()
  + CategoryInfo          : NotSpecified: (:) [], MehthodInvocationException
  + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI
share|improve this question
1  
it's working now, it's embarrasing but my error was caused only by typo. The code above is working –  antonio Feb 21 '12 at 5:10
    
You should post that, as an answer, then mark it accepted. –  jscott Feb 21 '12 at 16:22
1  
It would appear you are using the Powershell ADSI method for creating users. Microsoft has since shipped Powershell v2 and their Active Directory cmdlets that allow you to create users without ADSI. TechNet AD Cmdlets docs: technet.microsoft.com/en-us/library/ee617195.aspx Technet getting started docs: technet.microsoft.com/en-us/magazine/gg299328.aspx –  uSlackr Feb 21 '12 at 16:29
    
Thanks for the comments.. –  antonio Feb 22 '12 at 1:32

1 Answer 1

$ou=[ADSI] "LDAP:/..."
$dataSource = import-csv ".."

foreach($dataRecord in $dataSource) 
{

  $cn = $dataRecord.cn
  $sAMAccountName = $dataRecord.sAMAccountName
  $givenName = $dataRecord.FirstName
  $sn = $dataRecord.LastName
  $displayName = $sn + ", " + $givenName
  $userPrincipalName = $givenName + "." + $sn + "@nuggetlab.com"
  $newUser =  $ou.Create("user", "cn=" + $cn)

  $newUser.Put("sAMAccountName", $sAMAccountName)
  $newUser.Put("userPrincipalName", $userPrincipalName)
  $newUser.Put("displayName", $displayName)
  $newUser.Put("givenName", $givenName)
  $newUser.SetInfo()
  $newUser.SetPassword("Password")
  $newUser.psbase.InvokeSet("AccountDisabled", $false)
  $newUser.Put("company", "The Fantastic Four")
  $newUser.SetInfo()
}
share|improve this answer
    
Dont forget to mark this as accepted –  Mark Henderson Mar 6 '12 at 1:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.