简体   繁体   English

Powershell 交换 Proxyaddresses 和 PrincipalName 的脚本

[英]Powershell Script to swap Proxyaddresses and PrincipalName

Been writing script:一直在写脚本:

foreach ($Username in (Import-Csv -Path "C:\Users\...\nope.csv")) {

    set-aduser $Username.Username -remove @{Proxyaddresses="smtp:$Username.Username@domain.com"}
        Write-Output "remove proxy"
    set-aduser $Username.Username -replace @{userPrincipalName="$Username.Username@domain.com"}
        Write-Output "replace principal"
    set-aduser $Username.Username -add @{Proxyaddresses="smtp:$Username.Username@domain.com"}
        Write-Output "add proxy"

}

the code runs but ends up putting this garbage -> @{Username=###}.Username@domain.com in the attribute Editor.代码运行但最终将这个垃圾 -> @{Username=###}.Username@domain.com 放在属性编辑器中。

Any help would be appreciated, been trying different ways for like 8h now.任何帮助将不胜感激,现在正在尝试不同的方式,比如 8 小时。

Basically the double-quotes are expanding the object $UserName however are not allowing you to reference the property UserName of your object.基本上双引号正在扩展 object $UserName但是不允许您引用object 的属性UserName An easy way to get around this is to use the Subexpression operator $( ) :解决此问题的一种简单方法是使用Subexpression operator $( )

$object = [pscustomobject]@{
    Hello = 'World!'
}

$object.Hello      # => World!
"$object.Hello"    # => @{Hello=World!}.Hello
"$($object.Hello)" # => World!

On the other hand, a much easier solution in this case would be to reference the UserName property while enumerating the collection:另一方面,在这种情况下,一个更简单的解决方案是在枚举集合时引用UserName属性:

foreach ($UserName in (Import-Csv -Path "C:\Users\...\nope.csv").UserName) {
    $upn = "$UserName@newDomain.com"
    Set-ADUser $UserName -Remove @{ ProxyAddresses = "smtp:$UserName@currentDomain.com" }
    Write-Output "Remove ProxyAddresses for $UserName"
    Set-ADUser $UserName -Replace @{ userPrincipalName = $upn }
    Write-Output "Replace UserPrincipalName for $UserName"
    Set-ADUser $UserName -Add @{ ProxyAddresses = "smtp:$upn" }
    Write-Output "Add ProxyAddresses for $UserName"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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