這是我用來從 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。有什么建議?
uj5u.com熱心網友回復:
問題是您的回圈不會輸出任何要在 $Results 中收集的內容。
兩者都Set-ADUser默認Add-Member不提供輸出,除非您將開關附加-PassThru到這些 cmdlet。
此外,為了能夠處理catch塊中的例外(終止和非終止),您需要-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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/454120.html
標籤:电源外壳
