Blog Post

Exchange Team Blog
3 MIN READ

Exchange 2007 Scripting Corner: fix-alias

The_Exchange_Team's avatar
The_Exchange_Team
Platinum Contributor
Jun 15, 2007

One of the things that I have liked the most about the move to Exchange 2007 is the introduction of PowerShell into the management of Exchange. PowerShell is a very powerful tool that the Exchange administrator now has in their arsenal. Many regular, advanced and or mundane tasks can now be automated with PowerShell scripts or commands.

To help demonstrate what we can do with PowerShell, and get you the administrator interested in writing your own scripts, I hope to start posting a series of scripts (as I am able to write them) to the EHLO blog. The intent is that these scripts will solve a problem that you the Exchange administrators are having and will work as a way to teach PowerShell scripting thru example.

The first script I have for you is to solve a problem I have seen coming in on the phones here at Microsoft a few times. As our customers are migrating from Exchange 2003 to Exchange 2007 they are finding that some of the aliases of their contacts, users, or distributions groups contain invalid characters. If there are only a few it is simple to modify them by hand, but in many cases there are 10s if not hundreds that are incorrect.

With PowerShell and .net we can write a script that will correct this issue easily for us. Now most of the documentation of the script is in the comments sections of the script itself; but, I am going to go over the script flow and a few key things that I use in my script writing.

Script Flow:

At the bottom of the script your will find the Main body. This is actually where we begin processing.

  1. First we determine if the user has requested the help documentation using the –help input parameter.
  2. Next we check if the –type switch was passed otherwise we ask the user to choose and set the type of objects to look for.
  3. We construct the get command we will run into a variable (this is the best way to build "complex" commands to execute via multiple script inputs)
  4. We call and move into the FixObject Function
  5. The FixObject function is where the work is actually done
  • Execute the command we built and place the output in an array
  • Look thru each member of the array for our bad character
  • Replace the bad character when found (here we actually use a .net method from within PowerShell)
  • Build and execute the modification command

The true power of PowerShell I have found is in the ability to create functions. With a function you can encapsulate a given set of actions and then call those actions over and over again as needed in the rest of your script. In this script we use two functions ShowHelp; a function that appears in almost all of my scripts to provide the user with a "help" file on the script, and FixObject; the function that actually does all of the work in the script.

The other amazing power of PowerShell that we use in this script is the string.replace method. We are able to call that method directly from within PowerShell. Just to get an idea of the potential of PowerShell and .Net try this at a PowerShell prompt. Create a string Variable and set the value of the variable. (e.g. [string]$new = "Test" ). After setting our variable we will type the name of the variable followed by a period at the prompt. (e.g. $new.) Now simply press the tab key. What you will see is that you are now rotating thru the string methods available in .Net.

I would like to explicitly solicit feedback on this post. Did you find this ramble and the associated script helpful in showing some of the things that PowerShell can do and how to do them? Did this encourage you to write a script of your own? Also I would like to solicit Ideas from you on what you would like to see PowerShell do; and I will look into turning your Idea into my next posted script.

You can get the fix-alias script here: Fix-alias.PS1 script

- Matthew Byrd

Updated Jul 01, 2019
Version 2.0

17 Comments

  • I'm a bit surprised how much you are driving the powershell and scripting together with it. Are there real so many tasks on Exchange server which requires you to run script prefer GUI ? I'm asking this because I have no idea.....yet :)
    e.g. fixing the alias after migration, sounds that migration process is not finalized or something else went wrong. But in my mind task like this is once in lifetime and these are totally to AD related.
    Or the alert systems.... don't we have monitorin tools for monitoring our environment, instead of script based.
    Or creating new storage group, okay, that might be nice, but again once in a lifetime.

    So please don't forget the GUI...
  • An example of what I would like to accomplish is have a monitoring program run a script and send an alert based on the output of the script. Particularly run get-storagegroupcopystatus and if either the CopyQueueLength or ReplayQueueLength are too high then send a notification message. There are multiple other command I can see using in this same way.

  • Hi Vivek,

    Thank you for the response and the suggested optimizations ... I will be using both of those going forward.

    Hi Cooks,

    You can use powershell to return data directly too you in all sorts of forms.  XML, CSV, HTML etc ... you can even further manipulate the data directly in powershell.  Powershell has access to all of the .net classes so you can do just about anything that .net can do right in powershell.

    What are some tasks that you would like to see automated ... and or information that you would like reported.

    Thanks again for the feed back ... if anyone has anything furher please post away ... :)

    My Next Scripting corner is in the works and I am looking to do some one liners that help with basic automation stuff.

    -Matt
  • I do find this to be rambling for someone who has not yet written a powershell script which would be me. At this point I'm more interested in how to use powershell to automate tasks rather than run interactively. You can get good information from powershell but how can this be used in web sites or with a monitoring product? Can I receive output from running a powershell script without using vb or C with a pipeline?

    Hope these questions are not outside the scope of what you are trying to communicate.
  • In your mail you only sent /files/12/... as link...

    :)
    Klaus
  • Thank you for the cool script Matthew. Is there a list of invalid characters? Christian
  • "2007 Scripting corner" -- that is a really cool idea. Looking forward to more entries.

    The script is really cool, the idea of prompting users through fixing things, is great, and the implementation is neat---I really like the use of invoke-expression here. Nice work.

    Minor optimizataion: use here strings @' '@ to encapsulate large chunks of multi-line text and reduce the number of write-host lines you have to write.

    Minor nit: I typicall avoid using aliases in public scripts (e.g. iex) as most folks won't know what the alias is.