I have a couple of users that require hundreds of proxy addresses. I am using the following but only inserts "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
.
Anyway, I would suggest not simply adding a bunch of addresses that may already be in the users proxyAddresses
list.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.