簡體   English   中英

使用 PowerShell 添加特定格式的 proxyAddresses 屬性?

[英]Using PowerShell to add the proxyAddresses attribute with specific format?

我需要為特定 OU 中的用戶添加新的 email 別名,具體格式如下:

user Alias / sAMAccountName = First Lastname
newAlias = FLastname@NewBrandX.com

User with Apostrophe
user Alias / sAMAccountName = John O'Dea
newAlias = JOdea@NewBrandX.com

User with either First or Lastname with spaces
user Alias / sAMAccountName = Peter Van Denberg
newAlias = PVanDenberg@NewBrandX.com

不過我的腳本功底很有限,不知道有沒有人可以幫忙修改下面的腳本:

$newproxy = "@NewBrandX.com"
$userou = 'OU=Users,OU=Seattle,DC=usa,DC=contoso,DC=com'
$paramGetADUser = @{
   Filter      = 'Enable -eq $true'
   SearchBase = $userou
   Properties = 'SamAccountName', 'ProxyAddresses', 'givenName', 'Surname'
}


$users = Get-ADUser @paramGetADUser


Foreach ($user in $users)
{
   $FirstName = $user.givenName.ToLower() -replace '\s', ''
   $LastName = $user.surname.ToLower() -replace '\s', '' -replace "'", ''
   Set-ADUser -Identity $user.samaccountname -Add @{ proxyAddresses = "smtp:" + $FirstName + $LastName + $newproxy }
}

在我看來,您想以格式創建一個新的代理地址

  • GivenName 的第一個字符
  • 沒有撇號或空格的姓氏
  • 其次是"@NewBrandX.com"

但是,您的代碼采用完整的 GivenName。

要添加到 ProxyAddresses 數組,您需要替換整個 [string[]] 數組

$newproxy = "@NewBrandX.com"
$userou   = 'OU=Users,OU=Seattle,DC=usa,DC=contoso,DC=com'
$paramGetADUser = @{
   Filter     = 'Enable -eq $true'
   SearchBase = $userou
   Properties = 'ProxyAddresses'  # SamAccountName, GivenName and Surname are returned by default
}


$users = Get-ADUser @paramGetADUser

foreach ($user in $users) {
    $newAddress = ("smtp:{0}{1}$newproxy" -f $user.GivenName.Trim()[0], ($user.Surname -replace "[\s']+").ToLower())
    $proxies    = @($user.ProxyAddresses)
    # test if this user already has this address in its ProxyAddresses attribute
    if ($proxies -contains $newAddress) {
        Write-Host "User $($user.Name) already has '$newAddress' as proxy address" 
    }
    else {
        Write-Host "Setting new proxy address '$newAddress' to user $($user.Name)" 
        $proxies += $newAddress
        # proxyAddresses needs a **strongly typed** string array, that is why we cast the $proxies array with [string[]]
        $user | Set-ADUser -Replace @{ proxyAddresses = [string[]]$proxies }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM