Tuesday, January 27, 2009

Remove Invalid Characters from Exchange Aliases

Everytime a powershell command is run against Exchange you may receive warning such as this:

WARNING: Object my.domain.com/Users/Dough, John has been corrupted and it is in an inconsistent state. The following validation errors have occurred:
WARNING: "John Dough" is not valid for Alias. Valid values are: Strings formed with characters from a to z (uppercase or lowercase), digits from 0 to 9, !, #, $, %, &, ', *, +, -, /, =, ?, ^, _, `, {, , } or ~. One or more periods may be embedded in an alias, but each one of them should be preceded and followed by at least one of the other characters. Unicode characters from U+00A1 to U+00FF are also valid in an alias, but they will be mapped to a best-fit US-ASCII string in the email address which is generated from such an alias


In order to remove the invalid characters from the Exchange Alias (usually a space or a period) you can run the command below.

Get-Mailbox -OrganizationalUnit "mydomain.com/OU/Users"
-IgnoreDefaultScope foreach {$_.alias = $_.alias -replace '
\s\,<>\(\)\[\]'; $_}

But to do it right (logging, import specific OUs, etc) the whole script is below. P.S. Cleaning up DL's is as easy as changing the initial query.


# ==================================================================
param(
[switch]$help,
[string] $file = $(if ($help -eq $false){Read-Host "CSV File"}) # Define CSV file for import
)
# ==============================================================================
# Script to remove invalid characters from user's Exchange 'Alias'
# and trim whitespace from 'DisplayName'
#
# Last Revision 1.30.09 by Derek W. Aude
# ==============================================================================
# List of variable
[int]$Count = $null
[string]$WorkingOU = $null
[array]$SplitOU = $null
[string]$OU = $null
[string]$LoggingDir = $null
[string]$OutputFile = $null
[array]$Mailboxes = $null
[string]$Mailbox = $null
[array]$SplitAlias = $null
[string]$InvalidChar = $null
[string]$NewAlias = $null
[string]$OldAlias = $null
[int]$Count = $null
#===============================================================================
# Test to make sure the file path given is valid
if ((Test-Path $file) -eq $false){Write-Host "Could not find CSV file $file" -ForegroundColor Red;exit}
# Import the users to move from CSV into $csv
$Mailboxes = import-csv $file -erroraction stop
foreach ($WorkingOU in $Mailboxes) {
$LoggingDir = $null
$Count = 0
$WorkingOU = $WorkingOU.Name
# Get OU to clean and create log file
$SplitOU = [regex]::Split($WorkingOU, "/")
foreach ($OU in $SplitOU) {
$LoggingDir = $LoggingDir + "$OU\"
}
$OutputFile = New-Item -ItemType file "Logging\$LoggingDir$(Get-Date -UFormat '%Y%m%d%H%M%S').log" -Force
Write-Output "Mailbox Clean-up for $WorkingOU" >> $OutputFile
# Get all mailboxes in OU
$Mailboxes = Get-Mailbox -OrganizationalUnit $WorkingOU
Foreach ($Mailbox in $Mailboxes) {
if($Mailbox.DisplayName -like "* ") {
Set-Mailbox -Identity $Mailbox.Identity -DisplayName $Mailbox.DisplayName.Trim() -EmailAddressPolicyEnabled:$false
$Count = $Count + 1
Write-Output "Issue $Count - Removed leading or trailing whitespace from the display name: $Mailbox.DisplayName" >> $OutputFile
}
$OldAlias = $Mailbox.Alias
$NewAlias = $Mailbox.Alias -replace '\s\,<>\(\)\[\]'; $_
If ($NewAlias -ne $OldAlias) {
$Count = $Count + 1
Write-Output "Issue $Count - Removed invalid characters from $OldAlias" >> $OutputFile
}
if($NewAlias -like "*@*") {
$SplitAlias = [regex]::Split($NewAlias, "@")
$InvalidChar = "@" + $SplitAlias[1]
$NewAlias = $NewAlias -replace $InvalidChar; $_
$Count = $Count + 1
Write-Output "Issue $Count - Removed invalid @ in $OldAlias" >> $OutputFile
}
If ($NewAlias -ne $OldAlias) {
Set-Mailbox -Identity $Mailbox.Identity -Alias $NewAlias -EmailAddressPolicyEnabled:$false
Write-Output "Updated alias $OldAlias with new alias $NewAlias" >> $OutputFile
}
}
Write-Output "Completed Successfully" >> $OutputFile

No comments: