【问题标题】:Adding multiple proxyaddresses to a user为用户添加多个代理地址
【发布时间】:2018-08-17 01:07:37
【问题描述】:

我有几个用户需要数百个代理地址。我正在使用以下内容,但只插入“smtp:';

$proxyaddress 
='admin@test.edu.au','admin@test.com.au','ctiadmin@test.edu.au',
'fax@test.edu.au','info@test.edu.au'.....
Set-ADUser -Identity hello@test.edu.au -Add @{'proxyAddresses' = 
$proxyAddresses | % { "smtp:$_" }}

如果有任何帮助,我将不胜感激

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    您的代码中的错误是您实例化了一个名为$proxyaddress 的数组,但后来您使用了$proxyAddresses

    无论如何,我建议不要简单地添加一堆可能已经在用户proxyAddresses 列表中的地址。

    试试这个

    function Add-UserEmailAliases {
        [CmdletBinding(DefaultParameterSetName = 'ById')]
        param(
            [Parameter(ValueFromPipeline = $true, Mandatory = $true, ParameterSetName='ById', Position = 0)]
            [string]$Identity,             # distinguishedName, objectGUID or sAMAccountName
    
            [Parameter(Mandatory = $true, ParameterSetName='ByUPN', Position = 0)]
            [string]$UserPrincipalName,    # UPN
    
            [Parameter(Mandatory = $true, ParameterSetName='ByEmail', Position = 0)]
            [string]$EmailAddress,         # emailadress
    
            [Parameter(Mandatory = $true, Position = 1)]
            [string[]]$AliasesToAdd
        )
    
        # add the 'smtp:' prefix to each address; if already present, remove first
        $newProxies = @($AliasesToAdd | ForEach-Object { "smpt:{0}" -f ($_ -replace '^smpt:', '') })
    
        # get the user object
        switch ($PsCmdlet.ParameterSetName) {
            'ByUPN'    { $user = Get-ADUser -Filter "UserPrincipalName -eq '$UserPrincipalName'" -Properties ProxyAddresses ; break }
            'ByEmail'  { $user = Get-ADUser -Filter "EmailAddress -eq '$EmailAddress'" -Properties ProxyAddresses ; break }
            default    { $user = Get-ADUser -Identity $Identity -Properties ProxyAddresses }
        } 
    
        # store the primary email address
        $primaryAddress = $user.ProxyAddresses | Where-Object { $_ -cmatch '^SMTP:.*' }
        # read all other existing addresses
        $otherAddresses = @($user.ProxyAddresses | Where-Object { $_ -cnotmatch '^SMTP:.*' })
        # combine with the addresses to add and remove any duplicates
        $otherAddresses = ($otherAddresses + $newProxies) | Sort-Object -Unique
    
        # merge the existing and added addresses with the primary email address
        $newProxies = @($primaryAddress) + $otherAddresses
    
        # finally replace the users addresses. I like:
        $user | Set-ADUser -Clear ProxyAddresses
        $user | Set-ADUser -Add @{'proxyAddresses'=$newProxies }
    
        # but you could also do
        #$user | Set-ADUser -Replace @{'proxyAddresses' = $newProxies}
    }
    

    像这样使用它:

    $proxyaddresses = @('admin@test.edu.au','admin@test.com.au','ctiadmin@test.edu.au',
                        'fax@test.edu.au','info@test.edu.au')
    
    Add-UserEmailAliases -UserPrincipalName "hello@test.edu.au" -AliasesToAdd $proxyaddresses
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 2011-10-04
      相关资源
      最近更新 更多