Your PowerGUI Community: Simplify your PowerShell related GUI and script editing needs.


Help with script - Update AD using EmployeeNumber

This question is answered

Hi,

Am a novice powershell user.

With the kind help from other users on this forum I have been able to get a working script going which updates my AD with the required details from a CSV - key field being updated is Employee Number.

CODE START

$users = import-csv "c:\temp\update_users.csv"

foreach ($user in $users) {

set-aduser -identity $user.samaccountname -title $user.title -employeenumber $user.employeenumber

}


CODE END

Now that I have the EmployeeNumbers loaded successfully into AD, I need to develop another script which takes another CSV in however this CSV only contains EmployeeNumber and other fields that need to be updated - SamAccountNumber is not included (so can't use -identity)!

This has caused me issues, as I don't really know how to rejig the script so that I can use the EmployeeNumber in the CSV as the key to find the users to update.

I have been mucking about with for each loops etc but am really getting nowhere.

I am thinking I need to use some sort of filter on the EmployeeName as follows, but not sure how best to structure the script to do this effectively.

CODE START

Set-ADUser -Filter 'EmployeeNumber -eq $user'

CODE END


Help much appreciated!!

Verified Answer
  • That's exactly what I would have suggest!

    Only thing for little optimization, you don't need to affect a variable of you use it only once. So you can use this:

    $users = import-csv "c:\temp\update_users.csv"

    foreach ($user in $users)
    {
    get-aduser -filter {employeenumber -eq $user.EmployeeNumber} | set-aduser -title $user.title
    }



    Moreover, if you are like me and love "one-liners", here's another form:

    import-csv "c:\temp\update_users.csv" | %{get-aduser -filter {employeenumber -eq $_.EmployeeNumber} | set-aduser -title $_.title}


    Bill
All Replies
  • Hi Allan,

    I don't know what is the size of your infrastructure, so I don't know if this would suit your need. Here's to approach I've thinked of:

    ### 1 ###
    # Getting all users for each transactoin and filter it with the specific employee number, then doing what you need to do with the account

    $entries = Import-Csv d:\users.csv
    foreach ($entry in $entries){
    Get-QADUser -SizeLimit 0 -IncludedProperties employeeNumber | ?{$_.employeenumber -eq $entry.employeenumber} | %{}
    }


    ### 2 ###
    # Alternatively, you can build a hashtable containing all sam/EmployeeNumber, then using it against your CSV.

    $EmployeeSAM = @{}
    Get-QADUser -SizeLimit 0 -IncludedProperties employeeNumber | ?{$_.employeenumber -ne $null} | %{$EmployeeSAM.Add($_.employeenumber,$_.samaccountname)}

    Import-CSV d:\users.csv | %{Get-Qaduser -Identity $EmployeeSAM[$_.employeenumber] | }



    If your CSV are really short, solution #1 may be faster. But if this is a CSV containing a major part of your user, I think the #2 should be more effective.

    If anybody have a more effecient way to achieve this, please let me know...

    Bill
  • You can do this a few ways

    $users = import-csv "c:\temp\update_users.csv"

    foreach ($user in $users) {
    $EmployeeID = $user.EmployeeNumber
    get-qaduser -filter {employeeid -eq $employeeid}
    }

    or

    foreach ($user in $users) {
    $EmployeeID = $user.EmployeeNumber
    get-qaduser -ldapfilter "(employeeid=$employeeid)"
    }

    This way you are not pulling back all your users then filtering on the employeeID.
  • Thanks Ken!
  • Thanks Bill / Ken for your suggestions.

    So now my script looks like this: -
    **CODE START**
    $users = import-csv "c:\temp\update_users.csv"

    foreach ($user in $users)
    {
    $Employeenumber = $user.EmployeeNumber
    get-aduser -filter {employeenumber -eq $employeenumber}
    }
    **CODE END**

    This gets me the users in the CSV I am looking for - now I am looking to update these users with the remaining fields in the CSV.

    If I replace the get-aduser with a set-aduser and add the other fields I am looking to update to the end of the get-aduser line, I get a script error stating that Set-Aduser cannot be used with the -filter parameter.

    Am I missing something here?

    Should I somehow be storing or passing the results from each filter to another variable so that I can do the updates?

    Thanks for your help do far - much appreciated!
    Allan
  • It MUST be Monday morning. :)

    OK - apologies for missing this first time around - I am a novice after all!

    Code now does what I want - here it is.

    $users = import-csv "c:\temp\update_users.csv"

    foreach ($user in $users)
    {
    $Employeenumber = $user.EmployeeNumber
    get-aduser -filter {employeenumber -eq $employeenumber} | set-aduser -title $user.title
    }

    am now just piping the output from the get to the set and updating the title from the CSV. Tested briefly and this seems to work ok.

    Let me know what you guys think!!
  • That's exactly what I would have suggest!

    Only thing for little optimization, you don't need to affect a variable of you use it only once. So you can use this:

    $users = import-csv "c:\temp\update_users.csv"

    foreach ($user in $users)
    {
    get-aduser -filter {employeenumber -eq $user.EmployeeNumber} | set-aduser -title $user.title
    }



    Moreover, if you are like me and love "one-liners", here's another form:

    import-csv "c:\temp\update_users.csv" | %{get-aduser -filter {employeenumber -eq $_.EmployeeNumber} | set-aduser -title $_.title}


    Bill
  • Awesome!

    Thanks Bill!