我正在通过Powershell更改所有用户的UPN。我运行以下脚本没有错误。但是,所有UPN均未更改。当我运行部分脚本来获取属性时,会看到有关属性的更多信息。
更改UPN的脚本:
$ou = "DC=companyname,DC=office"
$local = Get-ADUser -SearchBase $ou -filter * -Properties userPrincipalName -ResultSetSize $null
$local | foreach {
$newUpn = $_.UserPrincipalName.Replace("companyname.office","companyname.com")
$_ | Set-ADUser -UserPrincipalName $newUpn
}
我收到以下错误:
You cannot call a method on a null-valued expression.
At Y:\it\powershell scripts\upn_change.ps1:11 char:19
+ ... | ForEach {$newUpn = $_.UserPrincipalName.Replace("companyname.office"," ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [],RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Set-ADUser : The operation failed because UPN value provided for addition/modification is not unique forest-wide
At Y:\it\powershell scripts\upn_change.ps1:11 char:96
+ ... e.office","companyname.com"); $_ | Set-ADUser -UserPrincipalName $newUpn}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (CN=Guest,CN=Users,DC=ComanyName,DC=office:ADUser) [Set-ADUser],ADException
+ FullyQualifiedErrorId : activeDirectoryServer:8648,microsoft.activeDirectory.Management.Commands.SetaDUser
我知道将用户从 .office 更改为 .com 可以正常工作,因为我有一个一直在使用的小型测试组,我是手动更改的。>
更新:
更新了脚本,将$_.UserPrincipalName.Replace
替换为$local[0].UserPrincipalName.Replace
,同时使用测试OU而不是完整的AD。我现在只遇到第二个错误。
Set-ADUser:操作失败,因为提供了UPN值 添加/修改不是全林唯一在Y:\ it \ powershell 脚本\ upn_change.ps1:11 char:103 + ... e.office“,” companyname.com“); $ _ | Set-ADUser -UserPrincipalName $ newUpn} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:未指定:(CN = Daniel McInt ... riese,DC = office:ADUser)[Set-ADUser],ADException + FullyQualifiedErrorId:activeDirectoryServer:8648,microsoft.activeDirectory.Management.Commands.SetaDUser
欢迎-不知道我做了什么。
我在网上找到了另一个脚本。
$upn = Get-Aduser...... (essentially the same as $local above). The rest is what I think did it - foreach { Set-ADUser $_ -UserPrincipalName ("{0}@{1}" -f $_.name,"kfriese.com")}
所以整个脚本是这样的:
$upn | foreach { Set-ADUser $_ -UserPrincipalName ("{0}@{1}" -f $_.name,"kfriese.com")}
现在,一切都很好。我使用了测试OU,所有UPN都更改为.com。可能不是建议这样做的确切方法,但确实有效。