Home > Enterprise >  Update EmployeeID Attribute using csv
Update EmployeeID Attribute using csv

Time:04-01

Here is the code i using to update the ad user from csv

$Users = Import-CSV "C:\PSS\UserList.csv"
$Results = $Users | ForEach-Object {
 try {
     $User = Get-ADUser -Filter {mail -eq $_.mail}
     if ($null -ne $User -and $null -eq $_.EmployeeID) {
         try {
             Set-ADUser $User.SamAccountName -EmployeeID $_.EmployeeID
             $_ | Add-Member -MemberType NoteProperty Status -Value "Complete"
         } catch {
             $ErrorMessage = $_.Exception.Message
             $_ | Add-Member -MemberType NoteProperty Status -Value $ErrorMessage
         }
     } else {
         $_ | Add-Member -MemberType NoteProperty Status -Value "Skipped"
     }
 } catch {
     $ErrorMessage = $_.Exception.Message
     $_ | Add-Member -MemberType NoteProperty Status -Value $ErrorMessage
 }
}
$Failed = $Results | Where-Object {$_.Status -ne "Complete" -and $_.Status -ne "Skipped"}
Write-Host "Failed Accounts: $($Failed.Count)"
$Results | Export-Csv -Path "UserList-Results.csv"

The output results in UserList-Results.csv are empty and the employee id can't be set in AD. Any suggestions?

CodePudding user response:

The thing is that your loop doesn not output anything to be collected in $Results.
Both Set-ADUser and Add-Member do not provide output by default, unless you append switch -PassThru to these cmdlet.

Also, to be able to handle exceptions (both terminating and non-terminating) in the catch blocks, you need use -ErrorAction Stop on the lines that can throw an exception.

Without changing your code too much, below should work for you

$Users   = Import-CSV -Path "C:\PSS\UserList.csv" # assuming there are fields `mail` and `EmployeeID` in this file
$Results = $Users | ForEach-Object {
    # store in a variable because in a catch block, $_ is the actual exception object, no longer the user from the CSV
    $currentUser = $_  
    # Get-ADUser by default returns objects with these properties:
    # DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
    $User = Get-ADUser -Filter "EmailAddress -eq '$($currentUser.mail)'" -Properties EmailAddress, EmployeeID -ErrorAction SilentlyContinue
    if ($User) {
        if ([string]::IsNullOrWhiteSpace($currentUser.EmployeeID)) {
            Write-Warning "Field EmployeeID was empty for user '$($User.Name)'. Skipped"
            $currentUser | Add-Member -MemberType NoteProperty -Name Status -Value "Skipped" -PassThru
        }
        elseif ($User.EmployeeID -eq $currentUser.EmployeeID) {
            Write-Host "EmployeeID already set to '$($User.EmployeeID)' for user '$($User.Name)'. Skipped"
            $currentUser | Add-Member -MemberType NoteProperty -Name Status -Value "Skipped" -PassThru
        }
        else {
            try {
                $User | Set-ADUser -EmployeeID $currentUser.EmployeeID -ErrorAction Stop
                Write-Host "EmployeeID for user '$($User.Name)' set to '$($currentUser.EmployeeID)'" -ForegroundColor Green
                $currentUser | Add-Member -MemberType NoteProperty -Name Status -Value "Complete" -PassThru
            } 
            catch {
                Write-Warning "Error setting the UserID property on user '$($User.Name)'"
                $ErrorMessage = $_.Exception.Message
                $currentUser | Add-Member -MemberType NoteProperty -Name Status -Value $ErrorMessage -PassThru
            }
        }
    }
    else {
        Write-Warning "The user with email address '$($currentUser.mail)' could not be found"
        $currentUser | Add-Member -MemberType NoteProperty -Name Status -Value "Skipped" -PassThru
    }
}

$Failed = @($Results | Where-Object {$_.Status -ne "Complete" -and $_.Status -ne "Skipped"})
Write-Host "Failed Accounts: $($Failed.Count)"

$Results | Export-Csv -Path "C:\PSS\UserList-Results.csv" -NoTypeInformation
  • Related