Exchange – Removing Illegal Alias Characters using PowerShell

August 15, 2011

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.















Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

45 comments

  1. ScriptFanaticAugust 15, 2011 ב 13:03

    @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.

    Reply
  2. John BarsodiAugust 16, 2011 ב 00:30

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

    Reply
  3. ScriptFanaticAugust 16, 2011 ב 12:20

    @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?

    Reply
  4. Dann CoxNovember 23, 2011 ב 02:48

    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.

    Reply
  5. ScriptFanaticNovember 23, 2011 ב 14:15

    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

    Reply
  6. DanMan32July 25, 2012 ב 16:30

    This is great. I found out MS has a couple scripts to find and remove invalid alias characters but it has serious limitations in that it can only find one invalid character or caracter type at a time, so this function is much better.

    How would we fix aliases for other recipietn types?
    I used “get-recipient” without the fix side of the sample command, and that did list all the recipients that had bad aliases. But when I tried to fix the alias and then replace “set-mailbox’ with “update-recipient” I got an error that there were two simultaneous pipes.

    Reply
  7. ScriptFanaticJuly 25, 2012 ב 17:18

    Get-Recipient returns all recipient types so you can’t pipe them all to one command. I suggest that you get your recipients by type, fix the alias and then and pipe them to a matching cmdlet.

    Which object types you want to fix?

    Reply
  8. Jan VinterbergOctober 3, 2012 ב 14:53

    And it also works for DistributionGroups 🙂 hence if Get-DistributionGroup and Set-DistributionGroup are used instead of the get/set-mailbox

    Thanks.

    Reply
  9. SujFebruary 6, 2013 ב 06:19

    HI Shev, what syntax should i be using to run this function & command. Thanks.

    Reply
  10. Fantastic!February 8, 2013 ב 06:05

    This exactly what i’m after, however, as Suj asked, how do i actually run the function and command together?

    Newbie to PowerShell!

    Reply
  11. dtdecby@gmail.comJuly 25, 2013 ב 14:24

    delle scarpe buone di marca per esempio adidas o lotto le trovi a partire da 30 delle scarpe cinesi equivalenti le trovi a 10 . per貌 le scarpe adidas anche se made in china hanno il plantare studiato e durevole . per esempio i giocattoli cinesi anche un sacco di giocattoli di marca sono made in china . Oakley Gafas Graduadas http://www.makeateam.com/gafas/oakleygafas.html

    Reply
  12. jpjtxsw@gmail.comAugust 10, 2013 ב 01:58

    [url=http://www.Sdyh0B1Opq6pt8K260h2m0.com/]ZjZfPYMT[/url]
    ZjZfPYMT http://www.Sdyh0B1Opq6pt8K260h2m0.com/
    ZjZfPYMT

    Reply
  13. thsdihk@outlook.comAugust 13, 2013 ב 11:13

    phdcpqej
    [url=http://www.g9yxw492h9838g6bw20t2cr992gztze2s.com/]phdcpqej[/url]
    phdcpqej http://www.g9yxw492h9838g6bw20t2cr992gztze2s.com/

    Reply
  14. fccdjpy@outlook.comAugust 13, 2013 ב 11:13

    puclualh
    [url=http://www.gj175u5w2c58a5bkf4e17jcw2k8777tys.com/]puclualh[/url]
    puclualh http://www.gj175u5w2c58a5bkf4e17jcw2k8777tys.com/

    Reply
  15. yfdieph@outlook.comAugust 13, 2013 ב 11:13

    yutulecw
    yutulecw http://www.g3zey118lh5i8l12j93w40992zrrdru0s.com/
    [url=http://www.g3zey118lh5i8l12j93w40992zrrdru0s.com/]yutulecw[/url]

    Reply
  16. djicxcj@outlook.comAugust 13, 2013 ב 11:13

    [url=http://www.g7qsf30v4g96ma926kd3q4vv9x70oa16s.com/]yobzeysi[/url]
    yobzeysi http://www.g7qsf30v4g96ma926kd3q4vv9x70oa16s.com/
    yobzeysi

    Reply
  17. jhwxnyb@outlook.comAugust 13, 2013 ב 11:14

    lqoyesdf http://www.g532ltpb7m2i84116ewt74knx6rn7k73s.com/
    [url=http://www.g532ltpb7m2i84116ewt74knx6rn7k73s.com/]lqoyesdf[/url]
    lqoyesdf

    Reply
  18. calfspr@outlook.comAugust 13, 2013 ב 11:14

    csycingk
    [url=http://www.gnl0hcb930cs938n690p9z4723souob4s.com/]csycingk[/url]
    csycingk http://www.gnl0hcb930cs938n690p9z4723souob4s.com/

    Reply
  19. zrasqfp@outlook.comAugust 13, 2013 ב 11:15

    jolsjtgq
    [url=http://www.gckqs895k6gp927ozzp2s070v44c1y46s.com/]jolsjtgq[/url]
    jolsjtgq http://www.gckqs895k6gp927ozzp2s070v44c1y46s.com/

    Reply
  20. nfkrppv@outlook.comAugust 13, 2013 ב 11:16

    czibukur http://www.gw708623vgqg7q5ji1pad90x0g650u7us.com/
    czibukur
    [url=http://www.gw708623vgqg7q5ji1pad90x0g650u7us.com/]czibukur[/url]

    Reply
  21. wflvpds@outlook.comAugust 13, 2013 ב 11:17

    ayocpnxh http://www.g1tobii25w7p2tx89zn21m930301f3sss.com/
    ayocpnxh
    [url=http://www.g1tobii25w7p2tx89zn21m930301f3sss.com/]ayocpnxh[/url]

    Reply
  22. fbjwedq@outlook.comAugust 13, 2013 ב 11:48

    nnsjfzzj http://www.gcca76u6qze2z7rn80358ozt61r3p871s.com/
    [url=http://www.gcca76u6qze2z7rn80358ozt61r3p871s.com/]nnsjfzzj[/url]
    nnsjfzzj

    Reply
  23. rbidhqm@outlook.comAugust 13, 2013 ב 11:48

    xzwsxlkp
    [url=http://www.g9l8s818b3kr3g535ggpve4nk65h815gs.com/]xzwsxlkp[/url]
    xzwsxlkp http://www.g9l8s818b3kr3g535ggpve4nk65h815gs.com/

    Reply
  24. uqwmkwn@outlook.comAugust 13, 2013 ב 11:48

    [url=http://www.g3m8l4c0z57jwk56pn3u94j5pg5k763qs.com/]ofcqhyxi[/url]
    ofcqhyxi http://www.g3m8l4c0z57jwk56pn3u94j5pg5k763qs.com/
    ofcqhyxi

    Reply
  25. sudizhl@outlook.comAugust 13, 2013 ב 11:48

    [url=http://www.gvdx411bqxfjx120m6sb9x0719y8n593s.com/]wtecivrq[/url]
    wtecivrq
    wtecivrq http://www.gvdx411bqxfjx120m6sb9x0719y8n593s.com/

    Reply
  26. jiimupu@outlook.comAugust 13, 2013 ב 11:48

    ubwbwvnu
    ubwbwvnu http://www.gkd873ib69x69og2yl8911w6635tslois.com/
    [url=http://www.gkd873ib69x69og2yl8911w6635tslois.com/]ubwbwvnu[/url]

    Reply
  27. vthhifo@outlook.comAugust 13, 2013 ב 11:48

    fvektrjr http://www.guz96z7so4l35x2nqe26l2861hl7cc46s.com/
    fvektrjr
    [url=http://www.guz96z7so4l35x2nqe26l2861hl7cc46s.com/]fvektrjr[/url]

    Reply
  28. iyvguhg@outlook.comAugust 13, 2013 ב 11:49

    vnkzwood http://www.g78f07ufqb7573zt6372spy2yc5ki30ts.com/
    [url=http://www.g78f07ufqb7573zt6372spy2yc5ki30ts.com/]vnkzwood[/url]
    vnkzwood

    Reply
  29. yxpjiml@outlook.comAugust 13, 2013 ב 11:49

    ecjgdjku
    ecjgdjku http://www.g0dt2i98b8z5zdg853fip03p125xwa30s.com/
    [url=http://www.g0dt2i98b8z5zdg853fip03p125xwa30s.com/]ecjgdjku[/url]

    Reply
  30. pxwbnhi@outlook.comAugust 13, 2013 ב 11:49

    [url=http://www.gfl5p79vb5v6soxz1qh7122d2959xo07s.com/]rrdgcjzv[/url]
    rrdgcjzv http://www.gfl5p79vb5v6soxz1qh7122d2959xo07s.com/
    rrdgcjzv

    Reply
  31. qzowpue@outlook.comAugust 13, 2013 ב 11:50

    wvsvwljb
    [url=http://www.glj298s89x3937z5waumu9nsk4v51k10s.com/]wvsvwljb[/url]
    wvsvwljb http://www.glj298s89x3937z5waumu9nsk4v51k10s.com/

    Reply
  32. mzqlopc@outlook.comAugust 13, 2013 ב 12:57

    wmrbklao
    [url=http://www.gbcnh8w7ng360n3904d68av4xu06hc27s.com/]wmrbklao[/url]
    wmrbklao http://www.gbcnh8w7ng360n3904d68av4xu06hc27s.com/

    Reply
  33. useqgod@outlook.comAugust 13, 2013 ב 12:57

    vxmqcpus http://www.g76o9e4j0651soh8r1jk4jsb0vi1959xs.com/
    [url=http://www.g76o9e4j0651soh8r1jk4jsb0vi1959xs.com/]vxmqcpus[/url]
    vxmqcpus

    Reply
  34. ugevdjf@outlook.comAugust 13, 2013 ב 18:34

    [url=http://www.g4t4vbkxe8l7bk91n36q417b19jt455os.com/]vlxrtsad[/url]
    vlxrtsad
    vlxrtsad http://www.g4t4vbkxe8l7bk91n36q417b19jt455os.com/

    Reply
  35. klfnyfd@outlook.comAugust 13, 2013 ב 23:30

    ieerkiik
    ieerkiik http://www.gnl324di7z62958rvduu898rd15dje08s.com/
    [url=http://www.gnl324di7z62958rvduu898rd15dje08s.com/]ieerkiik[/url]

    Reply
  36. *******@gmail.comAugust 15, 2013 ב 07:56

    impact). civil engineers; mining operators; building personnel; oil effectively operators, etc.). Sadly, innocent civilians have frequently been the victims of unintended energetic materials associated accidents.

    Reply
  37. sdvokza@outlook.comAugust 17, 2013 ב 03:56

    [url=http://www.gq7893p760h0792x3mp7eze5vazary42s.org/]uktbodjywhp[/url]
    aktbodjywhp
    ktbodjywhp http://www.gq7893p760h0792x3mp7eze5vazary42s.org/

    Reply
  38. qtohyrc@outlook.comAugust 17, 2013 ב 04:06

    xlndvhlp http://www.g35ozh8405ew900o57rwhb54c6vj37ios.org/
    axlndvhlp
    [url=http://www.g35ozh8405ew900o57rwhb54c6vj37ios.org/]uxlndvhlp[/url]

    Reply
  39. mqgnify@outlook.comAugust 17, 2013 ב 04:07

    apofwkhfr
    pofwkhfr http://www.g9oicp3753p05a8gjalm89ud08d4s325s.org/
    [url=http://www.g9oicp3753p05a8gjalm89ud08d4s325s.org/]upofwkhfr[/url]

    Reply
  40. guftaee@outlook.comAugust 17, 2013 ב 04:09

    [url=http://www.g374t1i8wob608a73qipe5676n8xbsz4s.org/]ucyxxjnxoox[/url]
    acyxxjnxoox
    cyxxjnxoox http://www.g374t1i8wob608a73qipe5676n8xbsz4s.org/

    Reply
  41. brjuxyt@outlook.comAugust 20, 2013 ב 03:36

    mjsrjdofr http://www.g69l5h99l3fer129066bi31c0f7poeues.org/
    amjsrjdofr
    [url=http://www.g69l5h99l3fer129066bi31c0f7poeues.org/]umjsrjdofr[/url]

    Reply
  42. vtpoll@gmail.comAugust 22, 2013 ב 18:14

    bpedij Exchange – Removing Illegal Alias Characters using PowerShell – Shay Levy http://www.HolidayInn-Vermont.com/#Fendi-Handbags

    Reply