PowerShell Get-ADuser值为字符串

时间:2017-08-04 11:06:13

标签: powershell

我正在努力从AD中抓取用户的脚本部分传递给变量$ TargetUsers

然后将此变量传递给以下内容以更改每个用户的UPN后缀:

$OldSuffix = "@olddomain.com"
$NewSuffix = "@newdomain.com"
Foreach ($User3 in $TargetUsers) {
$Upn3 = $User3.UserPrincipalName
$NewUpn = $User3.UserPrincipalName.Replace($OldSuffix,$NewSuffix)
    Get-ADUser -Filter "UserPrincipalName -eq '$Upn3'" | Set-ADuser `
        -Remove @{proxyAddresses=@("SMTP:$($User3.givenName).$($User3.sn)$OldSuffix","sip:$($User3.givenName).$($User3.sn)$OldSuffix")} `
        -Add @{proxyAddresses=@("SMTP:$($User3.givenName.ToLower()).$($User3.sn.ToLower())$NewSuffix","smtp:$($User3.givenName.ToLower()).$($User3.sn.ToLower())$OldSuffix","sip:$($User3.givenName.ToLower()).$($User3.sn.ToLower())$NewSuffix")} `
        -Replace @{ co = "United Kingdom" } `
        -Email "$($User3.givenName.ToLower()).$($User3.sn.ToLower())$NewSuffix" `
        -UserPrincipalName $NewUpn
    $NewUpn2 = Get-ADuser -Filter "UserPrincipalName -eq '$NewUpn'" | Select UserPrincipalName
    "$(Get-Date -f HH:mm:ss):  $($Upn3): AD Attributes updated & UPN Suffix changed to $NewUpn2" | Tee-Object $UserMigrationLog -Append
    }

输出到主机和日志文件" ... UPN后缀更改为@ {UserPrincipalName=Firstname.surname@newdomain.com}"

如何排除" @ {UserPrincipalName =" &安培;尾随"}"最后?

1 个答案:

答案 0 :(得分:0)

啊排序了。

$NewUpn2 = Get-ADuser -Filter "UserPrincipalName -eq '$NewUpn'" | % {$_.UserPrincipalName}

诀窍。