简体   繁体   English

向用户添加多个代理地址

[英]Adding multiple proxyaddresses to a user

I have a couple of users that require hundreds of proxy addresses. 我有几个需要数百个代理地址的用户。 I am using the following but only inserts "smtp:'; 我正在使用以下内容,但仅插入“ 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:$_" }}

I would appreciate any help 我将不胜感激任何帮助

The error in your code was that you instatiate an array called $proxyaddress , but later on you use $proxyAddresses . 代码中的错误是,您将一个名为$proxyaddress的数组$proxyaddress ,但后来使用$proxyAddresses

Anyway, I would suggest not simply adding a bunch of addresses that may already be in the users proxyAddresses list. 无论如何,我建议不要简单地添加一堆可能已经在用户proxyAddresses列表中的地址。

Try this 尝试这个

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

Use it like so: 像这样使用它:

$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

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

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