Question : Powershell - Help with existing script/csv combo

Ive got this code.. that one of you guys helped me with about two weeks ago.  The script just as below ran perfectly then.. on a standard .csv

However this week I needed to import some more users, I took another spreadsheet with all the data, re-arranged the columns to match that first csv, verified the column headers were identical.. except is give nothing but errors.  Here's the code with domain info replaced.  I also attached the .csv as well... I know its something stupid thats escaping me...


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
## define constants
$domainstr = "ou=testarea,dc=domain,dc=domain,dc=org"


$ADs_UF_NORMAL_ACCOUNT = 512   ## enables account and sets password required

## get default passsword - encrypted so not stored in script
#  $defaultPassword = Read-Host "Please enter default Password" -asSecureString

## get the list of users from the CSV file
## if need other user properties can add to CSV
## could speed processing by sortng user list by OU but need code 
## to handle change of OU.  This is simpler as an example

Import-csv c:\users\bhart.difc\desktop\importusers.csv | foreach {

## create user name
#	$strusr = $_.Last + " " + $_.First
    $strusr = $_.SN + " " + $_.givenName
#    $last = $_.SN 
#    $first = $_.givenName
    
 #   $strusr = $_.last + " " + $_.first
 	$ldapstr = "LDAP://OU=" + $_.OU + $domainstr

	$target = [ADSI] $ldapstr
	$newuser = $target.create("user", "cn=" + $strusr)
	$newuser.SetInfo()
	
        $userid = $_.givenName + $_.SN
	if ($userid.length -gt 20){$userid = $userid.substring(0,20)}
# echo $userid;exit;      	
	$newuser.samaccountname = $userid.ToString()
	$newuser.givenName =  $_.givenName
	$newuser.sn = $_.SN
	$newuser.displayName = $_.displayName
	$newuser.userPrincipalName = $_.givenName[0]+$_.givenName[1]+$_.SN + "@" + $domain
    $newuser.company =$_.company
    $newuser.mail = $_.mail
    $newuser.division = $_.division
    $newuser.employeeType = $_.empType
    $newuser.employeeID = $_.empID
    $newuser.telephoneNumber = $_.telephoneNumber
    $newuser.description = $_.description
	$newuser.SetInfo()

#	$newuser.SetPassword($defaultPassword.ToString())
    $newuser.SetPassword($_.password)
## normal user that requires password & is enabled
	$newuser.userAccountControl = $ADs_UF_NORMAL_ACCOUNT
	$newuser.SetInfo()

# set User must change password at next logon flag
#	$newuser.pwdLastSet = 0
#	$newuser.SetInfo()

## now set the country
#	$newuser.c = $_.Country
#	$newuser.SetInfo()


	Write-Host "Created Account for: "  $newuser.Displayname

}




*EDIT* I verified that I do have rights to this OU and that is the correct path
Attachments:
 
importing users csv
 

Answer : Powershell - Help with existing script/csv combo


One of t he values you're inserting here is invalid:

      $userid = $_.givenName + $_.SN
      if ($userid.length -gt 20){$userid = $userid.substring(0,20)}
# echo $userid;exit;            
      $newuser.samaccountname = $userid.ToString()
      $newuser.givenName =  $_.givenName
      $newuser.sn = $_.SN
      $newuser.displayName = $_.displayName
      $newuser.userPrincipalName = $_.givenName[0]+$_.givenName[1]+$_.SN + "@" + $domain
    $newuser.company =$_.company
    $newuser.mail = $_.mail
    $newuser.division = $_.division
    $newuser.employeeType = $_.empType
    $newuser.employeeID = $_.empID
    $newuser.telephoneNumber = $_.telephoneNumber
    $newuser.description = $_.description

I suggest you start with the bare minimum, SamAccountName, UserPrincipalName and slowly add them in again until it throws the error message.

Chris
Random Solutions  
 
programming4us programming4us