DCSIMG
Exchange - Removing Illegal Alias Characters using PowerShell - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

Exchange - Removing Illegal Alias Characters using PowerShell

If you're in a mixed-mode environment with both Exchange 2003 and Exchange 2007/2010 you may have noticed this message when using the Get-* cmdlets in the Exchange Management Shell:

WARNING: The object domin.com/Users/UserName has been corrupted, and it's in an inconsistent state. The following validation errors happened:
WARNING: Property expression "xx xxx" isn't valid. 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 period 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 e-mail address, which is generated from such an alias.

Or one of the following:

WARNING: Object <distinguished name of the recipient> has been corrupted and it is in an inconsistent state. The following validation errors have been encountered:

WARNING: <alias of the recipient> is not valid for Alias.

 

These messages (there are others as well) appears when you try to manage a recipient with spaces (or any other invalid character) in its alias using the Exchange management tools. For example, in Exchange Server 2003, you could create recipients with spaces in aliases. Exchange Server 2007/2010 does not allow recipients to have spaces in their aliases. The biggest problem with invalid aliases - you will not be able to move a mailbox to an Exchange 2007/2010 server. To mitigate this I've written the following function.

Note: In Exchange 2010, the mailbox's alias is generated based on the Name property. Invalid characters in the name will be replaced with a question mark (?) when the alias is generated.

 

function Test-ExchangeAlias 
{
    param(
        [Parameter(
            Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true
        )]
        [ValidateLength(1,64)]
        [string]$Alias,
        
        [switch]$RemoveIllegalCharacters
    )
  
    begin
    {
        $IllegalCharacters = 0..34+40..41+44,47+58..60+62+64+91..93+127..160
    }
   
    process
    {        
        if($RemoveIllegalCharacters)
        {  
            foreach($c in $IllegalCharacters)
            {                
                $escaped = [regex]::Escape([char]$c)
                
                if($Alias -match $escaped)
                {
                    Write-Verbose "illegal character code detected: '$c'" 
                    $Alias = $Alias -replace $escaped
                }
            }
        
            $Alias
        }        
        else
        {            
            for($c=0; $c -lt $Alias.Length; $c++)
            {
                $code = [int][char]$Alias[$c]
                Write-Verbose "Testing current Alias character code: $code" 
                
                if($IllegalCharacters -contains $code)
                {
                    Write-Verbose "Character code: $code is an invalid alias character."
                    $false
                    return
                }
            }    
            
            $true
        }              
    }
}

The function supports two parameters, Alias and RemoveIllegalCharacters. In the Begin block we assign a series of numbers to a variable, $IllegalCharacters, using the range operator along with the plus operator (+) to combine a range with a list of elements in an array. These numbers represents the character codes an alias cannot contain.

In the Process block we check if the RemoveIllegalCharacters parameter has been specified. If it was specified, all invalid characters are removed and a fixed alias is returned. Otherwise the function just tests if the alias is valid and returns $true/$false respectively. Invalid characters are removed using the Replace operator. Since we don't know if each invalid character is a regular expression meta character we use the Escape method to convert it so that the regular expression engine will interpret any metacharacters that it may contain as character literals.

 

With the following command you can fix all invalid aliases on all mailbox objects:

Get-Mailbox –ResultSize Unlimited | Where-Object {-not (Test-ExchangeAlias -Alias $_.Alias)} | Foreach-Object {
     $NewAlias = Test-ExchangeAlias -Alias $_.Alias -RemoveIllegalCharacters
     $_ | Set-Mailbox –Alias $NewAlias
}

 

When running the above you’ll get the ‘inconsistent state’ error for each invalid alias mailbox object but if you issue the command again you’ll see that the error has gone and the Aliases have been fixed.

Comments

BnayaA said:

אחלה פוסט!

# August 15, 2011 12:50 PM

ScriptFanatic said:

@Michel, thanks for the link however in Fix-Alias you have to know the character you want to remove inadvance,my version doesn't require that and includes all invalid characters. In addition you can test if a string you want to set as an Alias is valid.

# August 15, 2011 1:03 PM

John Barsodi said:

What about adding the X500 address since you'll be altering the Alias and thus breaking OLK cache and calendaring?

# August 16, 2011 12:30 AM

ScriptFanatic said:

@John, I could be wrong but don't think that changing a mailbox alias has any effect on other proxy addresses or Outlook. Anyway, thanks for pointing it out. The purpose of the function is to have a function to validate an Alias before you set it or to remove any illegal characters that already exist on the object. As far as I know when we use the Set-Mailbox cmdlet to change an alias only the mailNickName AD attribute is set. I don't have X500 addresses so I can't tell for sure if Exchnage changes them as well. Additionaly, if as you say it can cause issues with caching a simple script soltuion can fix them.

UPDATE: Do you mean chnages to OUtlook's NK2 files?

# August 16, 2011 12:20 PM

Exchange – Removing Illegal Alias Characters using PowerShell | Challenging Complexity... said:

Pingback from  Exchange &#8211; Removing Illegal Alias Characters using PowerShell  |  Challenging Complexity...

# August 19, 2011 3:26 PM

Exchange – Removing Illegal Alias Characters using PowerShell « MS Tech BLOG said:

Pingback from  Exchange &#8211; Removing Illegal Alias Characters using PowerShell &laquo; MS Tech BLOG

# September 10, 2011 12:51 PM

Dann Cox said:

Our organization uses firstname.lastname as a standard for alias.  Your function treats periods (ASC 46) as illegal characters, which they are not, unless they are accidentally doubled.  e.g. firstname..lastname should be invalid.  

I can't see how to modify this to allow single periods.

# November 23, 2011 2:48 AM

ScriptFanatic said:

Hey Dann

Thanks for reporting it, you're absolutely right, the dot char should not be in the list, my bad. I will fix it in the post. In the meantime you can exclude ASC 46 by changing 46+58 to 47+58 in $IllegalCharacters

# November 23, 2011 2:15 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: