0

I have a CSV file that I need to bulk update AD with. The file has an employeeID, which would match the employeeID field in AD. So far, I've been able to update a user successfully with a CSV file if there is just one row of information. What changes would I need to make to my script to allow for many users/rows to be updated?

$Path = "C:\import\users-test.csv "
$users = Get-Content –Path $Path | ConvertFrom-CSV
$users | foreach {
$_.psobject.properties | foreach { if ($_.value -eq "") { $_.value = $null }}
$eid = $_.empID
$user = Get-ADUser -Filter {employeeID -eq $eid}
Set-ADUser $user.samaccountname -title $_.Title –department $_.department –description $_.Description –office $_.Office –streetAddress $_.Street –city $_.City –postalCode $_.Zip –state $_.State –OfficePhone $_.Telephone –Country $_.Country –add @{extensionattribute11=$_.License}

}

EDIT: Updated code based on feedback. still isn't working.

Any help would be appreciated. This would be something that would be ran on a semi regular basis.

Thanks,

Jim

UPDATE: The error I'm getting is this -

Set-ADUser : Cannot validate argument on parameter 'Add'. The argument is null or an element of the argument collection contains a null value. At line:7 char:250
+ ... _.Country –add @{extensionattribute11=$_.License}
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Jim
  • 17
  • 3
  • 6
  • Does the CSV have values for each field of each user? Are any of them null/blank? – Clayton Jul 23 '15 at 20:59
  • yes a couple are blank (ie: description for user1, telephone for user2, etc...), but all the employeeID fields match up – Jim Jul 23 '15 at 21:04

2 Answers2

4

First, create an object with all the data like this:

$Path = "location of csv"
$Users = Get-Content -Path $Path | ConvertFrom-CSV

Then just iterate through the rows of data like this:

$Users | %{
    #Set ad user stuff using $_.Attribute 
}
Colyn1337
  • 2,387
  • 2
  • 22
  • 38
  • Colyn, thanks for the help. When I set it up like that, I'm still getting the Set-ADUser : replace errors.
    I have it starting like this:
    $Path = “C:\import\users-test.csv” $users = Get-Content –Path $Path | ConvertFrom-CSV $users | %{
    Then I run through the $Title = $_.title, etc...
    and close it with
    $user = Get-ADUser -Filter {employeeID -eq $employeeID} Set-ADUser $User.samaccountname -title $Title –department $Dept –description $Desc –office $Office –streetAddress $Street –city $City –postalCode $Zip –state $State –OfficePhone $Phone –Country $Country }
    – Jim Jul 23 '15 at 13:20
  • 1
    Easiest to discuss if you modify original post with "current" code. Within the "$Users | foreach" loop "$users" does not exist. Use "$_" for the current object of each loop iteration, aka $_.samaccountname. – Clayton Jul 23 '15 at 13:48
0

What error are you getting?
Is it working on some users, failing on others?
Are you sure all the employees ALREDY have employee ID's?
If there are users without EID's, the code mod below would handle the error more gracefully.
You may want to get-user based on samaccountname which is a required field (EID is not required)

    $user = Get-ADUser -Filter {employeeID -eq $employeeID}
    if ($user) {
        Set-ADUser $User.samaccountname -title $Title –department $Dept –description $Desc –office $Office –streetAddress $Street –city $City –postalCode $Zip –state $State –OfficePhone $Phone –Country $Country
    } else {
        write-warning "No employee found with ID: $employeeID"
    } # end if
} # end foreach top of script

UPDATE1:

The error is coming from the blank values.
When using Set-Aduser you can say "-clear department" or "-department $null" but not "-department $var" when var is a blank string like "". Why is one allowed, and the other not allowed? No idea, ask the bitheads at microsoft.

You can mod the values to account for this and replace blank with a true null. To do this efficiently, I used the propertes of the current object ($_) intead of making them all new string vars. Except for EID, the current object expansion for that did not work inside of -filter{}.

$Path = "C:\temp\userProps.csv"
$users = Get-Content –Path $Path | ConvertFrom-CSV
$users | foreach {
    $_.psobject.properties | foreach { if ($_.value -eq "") { $_.value = $null }}

    $eid = $_.employeeID
    $user = Get-ADUser -Filter {employeeID -eq $eid}

    Set-ADUser $user.samaccountname -title $_.Title –department $_.department –description $_.Description –office $_.Office –streetAddress $_.Street –city $_.City –postalCode $_.Zip –state $_.State –OfficePhone $_.telePhone –Country $_.Country
    Set-ADUser $user.samaccountname -add @{extensionattribute11 = $_.License}
}

UPDATE2:
What was the exact error?
Again, good to see the full code mod you made edited in the post.
Added working line to my code sample...
If I guessed correctly, you don't need/cannot use quotes around $_.license. When you do that, the entire object is first converted to a string, and then it looks for a property of the string named "license" which no longer exists, because it is now string, and is no longer an object.
Do some research on powershell objects, these are not the flat variables you may be used to...
http://powershell.com/cs/blogs/ebookv2/archive/2012/03/13/chapter-6-working-with-objects.aspx
http://powershell.com/cs/blogs/ebookv2/archive/2012/03/12/chapter-5-the-powershell-pipeline.aspx

Clayton
  • 4,483
  • 16
  • 24
  • Craig, That did it! Thank you so much for the explanation as well as the code. The blank value bit is definitely going into my powershell notes. – Jim Jul 27 '15 at 12:56
  • I've also learned that the extensionattributes have to be added differently than the others. When I put –add @{“extensionattribute11”=”$_.License”} before the closing } it errors out. Is there a recommended syntax for the blank/null scenario for these types of attributes? – Jim Jul 27 '15 at 12:57