PowerShell 中的空格和 $null 变量处理?
Whitespace & $null variable handling in PowerShell?
我正在尝试创建一个脚本,该脚本将获取 OU 中的所有用户并将当前的主要别名更改为次要别名,同时添加新的主要 smtp 地址并保留任何其他次要别名。我们有 0 个别名的用户,有些是 1,有些是 2,有些是 3。当 $sp1、$sp2、$sp3、$sp4、$sp5 中的任何一个是白色 space 或空。我仍在学习 powershell,所以我不确定如何轻松处理它,哈哈。
$Users = Get-AdUser -Filter * -SearchBase "OU=TestScriptedSMTPAddrChange,OU=***,DC=***,DC=com" -Properties proxyAddresses | Select-Object SamAccountName, proxyAddresses #Change first OU= to the OU you want to change
Foreach ($SAN in $Users){
$SecondaryProxyRaw = $SAN.proxyAddresses #grab proxyAddresses and dump them
$SecondaryProxyRed = $SecondaryProxyRaw.replace('SMTP','smtp') #change primary SMTP addr to secondary smtp addr
$sp1,$sp2,$sp3,$sp4,$sp5 = $SecondaryProxyRed.split(" ",1) #split the proxyAddresses array into variables
$NewPrimaryProxy = "SMTP:$($SAN.SamAccountName)@newdomain.com"} #assign new primary SMTP address
Set-ADUser -Identity $SAN.SamAccountName -replace @{proxyAddresses = "$NewPrimaryProxy","$sp1","$sp2","$sp3","$sp4","$sp5"}
}
Get-AdUser -Filter * -SearchBase "OU=TestScriptedSMTPAddrChange,OU=***,DC=***,DC=com" -Properties proxyAddresses | Select-Object SamAccountName, UserPrincipalName, @{Name="Proxyaddresses";Expression={$_.proxyAddresses -join "*"}}
.split(" ",1)
根本不拆分 - 根据定义它 returns 输入字符串 as-is,因为你只要求 1
令牌 - 请参阅 .NET 的文档[string]
类型的 .Split()
方法。
要按空白运行拆分,您可以使用 PowerShell 的 -split
operator:
的一元形式
# Split by whitespace and collect tokens in an array.
# -ne '' filters out empty elements, so that if
# $SecondaryProxyRed is effectively empty, $sps becomes an empty array.
$sps = -split $SecondaryProxyRed -ne ''
然后您可以创建一个数组,其中 $NewPrimaryProxy
作为第一个元素,然后是 $sps
的元素,如果有的话:
Set-ADUser -Identity $SAN.SamAccountName -replace @{
proxyAddresses = @($NewPrimaryProxy) + $sps
}
您不应依赖在其 proxyAddresses
属性中具有 1 个、3 个或 77 个地址的用户,方法是尝试将这些地址拆分为固定数量的变量。
全部获取,将大写的SMTP:
替换为小写的'smtp:',过滤掉可能等于新代理地址的,将新的主地址添加到数组中。
然后,将整个 proxyAddresses 数组替换为 强类型 (即转换为 string[]]
)新数组。
$Users = Get-AdUser -Filter * -SearchBase "OU=TestScriptedSMTPAddrChange,OU=***,DC=***,DC=com" -Properties proxyAddresses
foreach ($SAN in $Users) {
$NewPrimaryProxy = 'SMTP:{0}@newdomain.com' -f $SAN.SamAccountName
# if you like you can sort the proxies but for the system this will have no effect
$proxies = @($SAN.ProxyAddresses -replace '^SMTP:', 'smtp:' | Where-Object { $_ -ne $NewPrimaryProxy }) + $NewPrimaryProxy
# Note: proxyAddresses needs a Strongly typed string array, that is why we cast $proxies array with [string[]]
$SAN | Set-ADUser -Replace @{proxyAddresses = [string[]]$proxies}
}
我正在尝试创建一个脚本,该脚本将获取 OU 中的所有用户并将当前的主要别名更改为次要别名,同时添加新的主要 smtp 地址并保留任何其他次要别名。我们有 0 个别名的用户,有些是 1,有些是 2,有些是 3。当 $sp1、$sp2、$sp3、$sp4、$sp5 中的任何一个是白色 space 或空。我仍在学习 powershell,所以我不确定如何轻松处理它,哈哈。
$Users = Get-AdUser -Filter * -SearchBase "OU=TestScriptedSMTPAddrChange,OU=***,DC=***,DC=com" -Properties proxyAddresses | Select-Object SamAccountName, proxyAddresses #Change first OU= to the OU you want to change
Foreach ($SAN in $Users){
$SecondaryProxyRaw = $SAN.proxyAddresses #grab proxyAddresses and dump them
$SecondaryProxyRed = $SecondaryProxyRaw.replace('SMTP','smtp') #change primary SMTP addr to secondary smtp addr
$sp1,$sp2,$sp3,$sp4,$sp5 = $SecondaryProxyRed.split(" ",1) #split the proxyAddresses array into variables
$NewPrimaryProxy = "SMTP:$($SAN.SamAccountName)@newdomain.com"} #assign new primary SMTP address
Set-ADUser -Identity $SAN.SamAccountName -replace @{proxyAddresses = "$NewPrimaryProxy","$sp1","$sp2","$sp3","$sp4","$sp5"}
}
Get-AdUser -Filter * -SearchBase "OU=TestScriptedSMTPAddrChange,OU=***,DC=***,DC=com" -Properties proxyAddresses | Select-Object SamAccountName, UserPrincipalName, @{Name="Proxyaddresses";Expression={$_.proxyAddresses -join "*"}}
.split(" ",1)
根本不拆分 - 根据定义它 returns 输入字符串 as-is,因为你只要求 1
令牌 - 请参阅 .NET 的文档[string]
类型的 .Split()
方法。
要按空白运行拆分,您可以使用 PowerShell 的 -split
operator:
# Split by whitespace and collect tokens in an array.
# -ne '' filters out empty elements, so that if
# $SecondaryProxyRed is effectively empty, $sps becomes an empty array.
$sps = -split $SecondaryProxyRed -ne ''
然后您可以创建一个数组,其中 $NewPrimaryProxy
作为第一个元素,然后是 $sps
的元素,如果有的话:
Set-ADUser -Identity $SAN.SamAccountName -replace @{
proxyAddresses = @($NewPrimaryProxy) + $sps
}
您不应依赖在其 proxyAddresses
属性中具有 1 个、3 个或 77 个地址的用户,方法是尝试将这些地址拆分为固定数量的变量。
全部获取,将大写的SMTP:
替换为小写的'smtp:',过滤掉可能等于新代理地址的,将新的主地址添加到数组中。
然后,将整个 proxyAddresses 数组替换为 强类型 (即转换为 string[]]
)新数组。
$Users = Get-AdUser -Filter * -SearchBase "OU=TestScriptedSMTPAddrChange,OU=***,DC=***,DC=com" -Properties proxyAddresses
foreach ($SAN in $Users) {
$NewPrimaryProxy = 'SMTP:{0}@newdomain.com' -f $SAN.SamAccountName
# if you like you can sort the proxies but for the system this will have no effect
$proxies = @($SAN.ProxyAddresses -replace '^SMTP:', 'smtp:' | Where-Object { $_ -ne $NewPrimaryProxy }) + $NewPrimaryProxy
# Note: proxyAddresses needs a Strongly typed string array, that is why we cast $proxies array with [string[]]
$SAN | Set-ADUser -Replace @{proxyAddresses = [string[]]$proxies}
}