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

[玩转系统] 通过 PowerShell 批量更改 Active Directory 中的 UserPrincipalName

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

通过 PowerShell 批量更改 Active Directory 中的 UserPrincipalName


为了做好云迁移或其他任何不同的准备,许多公司需要更改其用户 UPN。作为顾问,我必须多次这样做,因此我编写了几行 PowerShell 代码来管理此问题。这并不困难,但为了记录,我的脚本还创建了一个 CSV 文件,其中包含更改前后的 UPN。通过这个简单的机制,您可以轻松地进行回滚。但首先我们要将 UPN 更改为电子邮件地址。 (此脚本需要 ActiveDirectory PowerShell 模块)。

注意:在更改 ActiveDirectory 中的 UPN 之前,请验证是否有任何第三方软件依赖于 UPN(如果您不知道,请在某些用户上手动更改它来尝试)。

用户看起来怎么样?

[玩转系统] 通过 PowerShell 批量更改 Active Directory 中的 UserPrincipalName

[玩转系统] 通过 PowerShell 批量更改 Active Directory 中的 UserPrincipalName

运行脚本后,用户应如下所示(现在 UPN 与电子邮件地址相同):

[玩转系统] 通过 PowerShell 批量更改 Active Directory 中的 UserPrincipalName

注意:请验证替代登录 UPN 后缀(可以在 MMC“域和信任”中找到)

[玩转系统] 通过 PowerShell 批量更改 Active Directory 中的 UserPrincipalName

该脚本会生成两个 CSV 文件,因此可以轻松比较是否存在问题。

[玩转系统] 通过 PowerShell 批量更改 Active Directory 中的 UserPrincipalName

让我们看一下空白脚本:

$ous = "OU=MENGEN,OU=DEMO,DC=demo,DC=it-koehler,DC=com","OU=SIGMARINGEN,OU=DEMO,DC=demo,DC=it-koehler,DC=com"
[boolean]$logonly = $true
[string] $csvuserLogfilePath = "C:\temp\UPN"
  
  [string] $csvuserLogfileDate = Get-Date -Format "yyyy-MM-dd-HH-mm"
  [string] $csvuserLogfileNamePrefix = "UPN-Before"
  [string] $csvuserLogfileName = $($csvuserLogfileNamePrefix + $csvuserLogfileDate + ".csv")
  [string] $csvuserLogfile = $csvuserLogfilePath + "\" + $csvuserLogfileName

  [string] $csvuserLogfileNamePrefix = "UPN-Changed"
  [string] $csvuserLogfileName = $($csvuserLogfileNamePrefix + $csvuserLogfileDate + ".csv")
  [string] $csvuserLogfilechanged = $csvuserLogfilePath + "\" + $csvuserLogfileName
    $dcfqdn  = ((Get-ADDomainController).Hostname)
 Add-Content -Path $csvuserLogfile -Value "SamAccountName;UserPrincipalName;Mail"
 if($logonly -ne "$true"){
 Add-Content -Path $csvuserLogfilechanged -Value "SamAccountName;UserPrincipalName;Mail"
 }
foreach($ou in $ous){

    $users = (Get-ADUser -Server $dcfqdn  -Filter * -Properties * -SearchBase "$ou"   | Where-Object {$_.mail -ne $null}) 

        foreach($user in $users){
                $attribute1 = $user | Select-Object samaccountname -ExpandProperty samaccountname
                $attribute2 = $user | Select-Object userprincipalname -ExpandProperty userprincipalname
                $attribute3 = $user | Select-Object mail -ExpandProperty mail
                    Add-Content -Path $csvuserLogfile -Value "$attribute1;$attribute2;$attribute3"
                
                if($logonly -ne "$true"){
                Write-Host "set UPN $attribute3 for user $attribute1" -ForegroundColor Green 
                Set-ADUser -Server "$dcfqdn" -Identity "$user" -UserPrincipalName "$attribute3"
                $changeduser = Get-ADUser -Server $dcfqdn -Identity "$attribute1" -Properties mail | select samaccountname,userprincipalname,mail
                $change1 = $changeduser | Select-Object samaccountname -ExpandProperty samaccountname
                $change2 =  $changeduser | Select-Object userprincipalname -ExpandProperty userprincipalname
                $change3 = $changeduser | Select-Object mail -ExpandProperty mail
                Add-Content -Path $csvuserLogfilechanged -Value "$change1;$change2;$change3"
                
                 }
                
               
                }

    }

如果您在自己的计算机上运行它,则必须进行一些修改(第1-3行)
您需要为其提供OU的可分辨名称(可以处理多个OU)
Logonly设置为“True”默认情况下(因此根本不进行任何更改)
CSV 文件日志的路径。

[玩转系统] 通过 PowerShell 批量更改 Active Directory 中的 UserPrincipalName

希望该脚本可以帮助您尽可能轻松地更改 UPN。
玩得开心。

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

取消回复欢迎 发表评论:

关灯