简体   繁体   中英

PowerShell Get-ADuser value as string

Part of a script that I'm working on grabs users from AD passes to the variable $TargetUsers

This variable is then passed on to the following to change the UPN suffix for each user:

$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
    }

This outputs to host and log file "...UPN Suffix changed to @{UserPrincipalName=Firstname.surname@newdomain.com}"

How can I get this to exclude the "@{UserPrincipalName=" & trailing "}" at the end?

Ah sorted it.

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

Did the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM