2017-04-26 65 views
1

我想获得一个特定的AD用户并更改他们的UPN,但不是他们的UPN后缀。如何检索用户UPN后缀为一个字符串

正如你现在所看到的,我必须手动输入他们当前的UPN后缀,这是有点没有意义的,因为你必须进入AD发现无论如何,有一些字符串,如$_.UPNSuffix,将调用用户的当前后缀?

$container = "OU=MyOU,DC=MyDomain,DC=local" 
$Filter = Read-Host -Prompt "Enter users Username/P-number" 
$UPNSuffix = Read-Host -Prompt "Enter users current UPN Suffix" 
$users = Get-ADUser -Filter "UserPrincipalName -like '$Filter*'" -SearchBase $container 

Foreach ($user in $users) 
    { 
    $newFQDN = $user.GivenName + "." + $user.Surname 
    $NewDN = $user.GivenName + " " + $user.Surname 
    Set-ADUser -Identity $user -UserPrincipalName [email protected]$UPNSuffix -SamAccountName $newFQDN 
    Write-Host "User's UPN is now [email protected]$UPNSuffix" 
    } 

回答

2

您可以通过分割@符号来获得UPN组件。 我会沿着这样的路线做点什么:

$container = "OU=MyOU,DC=MyDomain,DC=local" 
$Filter = Read-Host -Prompt "Enter users Username/P-number" 
$users = Get-ADUser -Filter "UserPrincipalName -like '[email protected]*'" -SearchBase $container 

Foreach ($user in $users) 
    { 
    $null, $UPNSuffix = $user.UserPrincipalName -split '@' # Dump the first part, store the 2nd 
    $newFQDN = $user.GivenName + "." + $user.Surname 
    $NewDN = $user.GivenName + " " + $user.Surname 
    Set-ADUser -Identity $user -UserPrincipalName "[email protected]$UPNSuffix" -SamAccountName $newFQDN 
    Write-Host "User's UPN is now [email protected]$UPNSuffix" 
    } 
1

从快速谷歌似乎不存在为后缀的专用场,但我想你可以得到的UserPrincipalName属性,然后就分开的@和抢分的第二个元素:

$UPN = (Get-ADUser -Identity $user -Property UserPrincipalName).UserPrincipalName 
If ($UPN) { 
    $UPNSuffix = ($UPN -Split '@')[1] 
} Else { 
    Write-Warning "Failed to get UserPrincipalName for $User" 
} 

注意:这是未经测试的代码。

相关问题