我有一个脚本可以每周从CSV更新各种AD字段(包括扩展属性)。它似乎适用于很多帐户,但有些会得到以下错误,并且不会更新:
Set-ADUser : replace At C:\ADMaint\ADupdate.ps1:45 char:11 + Set-ADUser <<<< -EmployeeNumber $($user."employeeNumber") -Department $($user."department") -Title $($user."title") -Office $($user."physicalDeliveryOfficeName") -StreetAddress $($user."streetAddress") -City $($user."l") -State $($user ."st") -PostalCode $($user."postalCode") -Company $($user."Company") -Officephone $($user."telephoneNumber") -Mobile $( $user."mobile") -Fax $($user."facsimileTelephoneNumber") + CategoryInfo : InvalidOperation: (CN=Name\, Name...C=domain,DC=com:ADUser) [Set-ADUser], ADInvalidOperationException + FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser
这是剧本:
$users = Import-Csv -Path C:\ADMaint\ADUpdate.csv
# Loop through CSV and update users if the exist in CVS file
foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -ErrorAction SilentlyContinue -Filter "employeeID -eq '$($user.employeeID)'" -Properties * -SearchBase "ou=Logins,dc=domain,dc=com" |
Set-ADUser -EmployeeNumber $($user."employeeNumber") -Department $($user."department") -Title $($user."title") -Office $($user."physicalDeliveryOfficeName") -StreetAddress $($user."streetAddress") -City $($user."l") -State $($user."st") -PostalCode $($user."postalCode") -Company $($user."Company") -Officephone $($user."telephoneNumber") -Mobile $($user."mobile") -Fax $($user."facsimileTelephoneNumber")
Get-ADUser -ErrorAction SilentlyContinue -Filter "employeeID -eq '$($user.employeeID)'" -Properties * -SearchBase "ou=Logins,dc=domain,dc=com" |
Set-ADUser -Replace @{ExtensionAttribute1=($user.extensionAttribute1); ExtensionAttribute2=($user.extensionAttribute2); ExtensionAttribute3=($user.extensionAttribute3); ExtensionAttribute4=($user.extensionAttribute4)}
}