使用 csv 更新 EmployeeID 属性

Update EmployeeID Attribute using csv

这是我用来从 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"

UserList-Results.csv输出结果为空,无法在AD中设置员工id。有什么建议吗?

问题是你的循环没有输出任何要在 $Results 中收集的东西。
默认情况下,Set-ADUserAdd-Member 都不提供输出,除非您将开关 -PassThru 附加到这些 cmdlet。

此外,为了能够处理 catch 块中的异常(终止和 non-terminating),您需要在可能引发异常的行上使用 -ErrorAction Stop

无需过多更改您的代码,下面的内容应该适合您

$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