Friday, February 26, 2010

Exchange 2007 Alias update - Powershell

With a recent migration from an old email system I needed to bring over aliases from the old system that would be grandfathered for those users but not new users. This Powershell script checked the accounts to see if the alias was present and if not added it to the account as an accepted Email Address.

### -----------------------------------------------------------------
### Written by Matt Brown - 01/07/2010
###
### Powershell script to update Exchange Aliases
### from ones found old email system
###
### Requires Exchange Powershell extenstions
###
### Input file should contain csv row for alias and username
### Example: username,alias
### jdoe,jon.doe
### -----------------------------------------------------------------

$thedate = Get-Date -f yyyy-MM-dd_HH-mm
$filename = $thedate + "_output.rtf"
start-transcript -path $filename

# ---------------------------
# Add Quest AD Snapin
# ---------------------------
if(-not (Get-PSSnapin | where { $_.Name -match 'quest.activeroles.admanagement' })) {
add-PSSnapin quest.activeroles.admanagement
}
if(-not (Get-PSSnapin | where { $_.Name -match 'Microsoft.Exchange.Management.PowerShell.Admin' })) {
add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
}

## Grab all the aliases in the file and group in an array by username
$MailAliases = @{}
Import-Csv "test.txt" | foreach {
$MailAliases[$_.Username] += @($_.Alias)
}

## set the domains we want to see for each alias
$Domains = @()
$Domains += "@domain.com"
$Domains += "@sub.domain.com"

## loop through the users and look for each alias
## with each domain in the current list if missing add
## it to the accepted addresses and update the user account
$x = $MailAliases.count
$length = $x
$MailAliases.keys | foreach {
# Get the user account
$User = Get-Mailuser -identity $_
$updateuser = $false
# Check each mail alias in the list
$MailAliases[$_] | foreach {
$ua = $_
$Domains | foreach {
$check = $ua + $_
$needsadd = $true
$User.EmailAddresses | foreach {
if($_.SMTPAddress -eq $check) {
# address found in list, will not be added
$needsadd = $false
}
}
if($needsadd -eq $true) {
# address wasn't found, add to accepted addresses
$User.EmailAddresses += $check
$updateuser = $true
}
}
}
if($updateuser -eq $true) {
# Now Update the User Account with new aliases
#Write-Host $User.Name
$User | Set-Mailuser
}
}
#cleanup
Stop-Transcript

1 comment:

  1. Only one man helped me when I got into trouble. All my MS Exchange data was corrupted and he offered an one utility. It worked out my trouble effortlessly. Moreover I hope that this software has enough facilities for solving this situation - exchange server recovery.

    ReplyDelete