当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 更改用户 UPN

作者:精品下载站 日期:2024-12-14 18:11:20 浏览:13 分类:玩电脑

使用 PowerShell 更改用户 UPN


您希望将本地用户与 Office 365 同步。在此之前,请确保在 Active Directory (AD) 中配置用户的 UPN。在本文中,您将了解如何添加 UPN 后缀以及如何使用 PowerShell 更改 AD 用户 UPN。

信息

任何包含不可路由域(例如 john.doe@local)的 UPN 都将同步到 .onmicrosoft.com 域,例如 [email protected]。事情不应该如此。

如果您当前在 Active Directory 中为用户帐户使用 .local 域,建议您将其更改为使用经过验证的域。例如,[email protected],以便与您的 Office 365 域正确同步。

在 AD 中添加 UPN

第一步是在 Active Directory 中添加 UPN 后缀。

  1. 单击开始并搜索Active Directory 域和信任,然后单击它。

您还可以按Windows 键 + R 打开运行对话框,输入domain.msc,然后选择确定

[玩转系统] 使用 PowerShell 更改用户 UPN

  1. Active Directory 域和信任窗口中,右键单击Active Directory 域和信任,然后选择属性

[玩转系统] 使用 PowerShell 更改用户 UPN

  1. UPN 后缀选项卡的备用 UPN 后缀框中,键入新的 UPN 后缀,然后选择添加。完成后点击确定

[玩转系统] 使用 PowerShell 更改用户 UPN

UPN 添加成功。

使用 PowerShell 在 AD 中添加 UPN

我们可以使用PowerShell在AD中添加UPN后缀。

以管理员身份运行 PowerShell。获取 UPN 后缀列表。

PS C:\> Get-ADForest | Format-List UPNSuffixes

UPNSuffixes : {}

它不显示任何 UPN 后缀。这意味着它是空的。让我们添加 UPN 后缀。

PS C:\> Get-ADForest | Set-ADForest -UPNSuffixes @{add="exoip.com"}

确认UPN后缀添加成功。

PS C:\> Get-ADForest | Format-List UPNSuffixes

UPNSuffixes : {exoip.com}

更改所有 AD 用户的 UPN

现在我们已经在 AD 中设置了 UPN 后缀,我们希望更改 AD 中所有用户的 UPN。

首先,我们获取组织中所有 AD 用户的列表。

PS C:\> Get-ADUser -Filter * | Sort-Object Name | Format-Table Name, UserPrincipalName

Name          UserPrincipalName
----          -----------------
Administrator [email protected]
Amanda Morgan [email protected]
Amelia Nash   [email protected]

更改组织中所有 AD 用户的 UPN。一一运行命令。

PS C:\> $LocalUsers = Get-ADUser -Filter {UserPrincipalName -like '*exoip.local'} -Properties UserPrincipalName -ResultSetSize $null
PS C:\> $LocalUsers | foreach {$newUpn = $_.UserPrincipalName.Replace("exoip.local","exoip.com"); $_ | Set-ADUser -UserPrincipalName $newUpn}

通过运行 Get-ADUser cmdlet 确认 UPN 已更改。

PS C:\> Get-ADUser -Filter * | Sort-Object Name | Format-Table Name, UserPrincipalName

Name          UserPrincipalName
----          -----------------
Administrator [email protected]
Amanda Morgan [email protected]
Amelia Nash   [email protected]

组织中所有用户的 UPN 已成功更改。假设您想将 UPN 更改回 exoip.local,请更改之前命令中的 UPN。

您可能有一长串用户,并且想要验证 AD 中是否没有 .local 地址。获取具有 .local UPN 后缀的所有用户的列表。输出应该为空。

PS C:\> Get-ADUser -Filter {UserPrincipalName -like '*local'} | Sort-Object Name | Format-Table Name, UserPrincipalName

更改特定 OU 中 AD 用户的 UPN

您不必更改所有用户的 UPN。可以更改特定 OU 的 UPN。阅读有关如何使用 PowerShell 获取 OU 的更多信息。

首先,我们获取特定 OU 中的 AD 用户列表。我们有一个名为Finance 的 OU。

PS C:\> Get-ADUser -Filter * -SearchBase "OU=Finance,OU=Users,OU=Company,DC=exoip,DC=local" | Format-Table Name, UserPrincipalName

Name             UserPrincipalName
----             -----------------
Madeleine Fisher [email protected]
Sebastian Nolan  [email protected]
Irene Springer   [email protected]
Amelia Nash      [email protected]
Jasmina Wilson   [email protected]

更改财务 OU 中 AD 用户的 UPN。一一运行命令。

PS C:\> $LocalUsers = Get-ADUser -Filter {UserPrincipalName -like '*exoip.local'} -SearchBase "OU=Finance,OU=Users,OU=Company,DC=exoip,DC=local" -Properties UserPrincipalName -ResultSetSize $null
PS C:\> $LocalUsers | foreach {$newUpn = $_.UserPrincipalName.Replace("exoip.local","exoip.com"); $_ | Set-ADUser -UserPrincipalName $newUpn}

通过运行 Get-ADUser cmdlet 确认 UPN 已更改。

PS C:\> Get-ADUser -Filter * -SearchBase "OU=Finance,OU=Users,OU=Company,DC=exoip,DC=local" | Format-Table Name, UserPrincipalName

Name             UserPrincipalName
----             -----------------
Madeleine Fisher [email protected]
Sebastian Nolan  [email protected]
Irene Springer   [email protected]
Amelia Nash      [email protected]
Jasmina Wilson   [email protected]

财务用户的 UPN 已成功更改。假设您想将 UPN 更改回 exoip.local,请更改之前命令中的 UPN。

您可能有一长串用户,并且您想要验证 AD OU 中是否没有 .local 地址。获取具有 .local UPN 后缀的所有用户的列表。输出应该为空。

PS C:\> Get-ADUser -Filter {UserPrincipalName -like '*local'} -SearchBase "OU=Finance,OU=Users,OU=Company,DC=exoip,DC=local" | Sort-Object Name | Format-Table Name, UserPrincipalName

就是这样!

现在用户的 UPN 已更改,如果您想自动更改怎么办?阅读有关如何使用计划任务自动更改用户 UPN 的更多信息。

在下一步中,我们将了解 Microsoft IdFix - 目录同步错误修复工具。

结论

您了解了如何使用 PowerShell 更改用户 UPN。更改 Active Directory 中的所有用户或仅更改选定的 OU。请记住在完成后验证您的工作。

您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 将用户添加到组。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯