Exchange PowerShell Quickstart – Bonus

Over the past 5 episodes we have been covering all sorts of aspects of Exchange PowerShell fundamentals.  This week I want to give just a few extra hints and tips as you step into the brave world of Exchange PowerShell.

Hint 1: Don’t abbreviate

Abbreviation is fine when writing directly at the command line, since no one else aside from you will ever have to read it and understand what you are doing.

$a = Get-Mailbox
foreach( $b in $a ) {
foreach( $c in $b.EmailAddresses ) {
# Do Something
}
}

the above is much harder to read than

$Mailboxes = Get-Mailbox
foreach( $Mailbox in $Mailboxes ) {
foreach( $EmailAddress in $b.EmailAddresses ) {
# Do Something
}
}

Hint 2: Use indenting in scripts

Indenting really helps understand the flow of the script, I have seen scripts that are multiple screens long and every line is a command and starts at the same point, yet there were iterations and conditions, making it very hard to follow what was going on.  When I read someone else’s code that hasn’t been indented, generally the first thing I do is indent it.

$Mailboxes = Get-Mailbox
foreach( $Mailbox in $Mailboxes ) {
foreach( $EmailAddress in $b.EmailAddresses ) {
# Do Something
}
}

 

is harder to follow than

$Mailboxes = Get-Mailbox
foreach( $Mailbox in $Mailboxes ) {
   foreach( $EmailAddress in $b.EmailAddresses ) {
      # Do Something
   }
}

 

Hint 3: Avoid long pipelines within scripts

Again on the command line using piping from one to another is fine, but in a script those long pipelines become hard to understand and to debug if they go wrong.  In Scripts it is generally better to stick to the more traditional one task per line method.

Hint 4: Use Select-Object to bring back only what you need

This is more for scripts used in big organizations, e.g. $mailboxes = Get-Mailbox -ResultSet Unlimited works fine even in a 300,000 mailbox firm, however the amount of memory consumed runs into multiple GB.  If you only need say Alias and PrimarySmtpAddress, then add a Select-Object. This will dramatically reduce the amount of memory that your PowerShell session consumes.

$Mailboxes = Get-Mailbox -ResultSet Unlimited | Select-Object Alias, PrimarySmtpAddress

Hint 5: Avoiding ‘Pipelines cannot be executed concurrently’

When you are using a pipeline on the command line PowerShell will by default run the next command as soon as it starts getting results back.  Now depending on what the next command does that can cause issues.  The end result is screeds of Red Errors saying that Pipelines cannot be executed concurrently.  There are three things you can do to resolve this.

  1. Don’t use a pipeline
    Breaking the pipeline resolves the issue since PowerShell can’t move onto the next line until it has got all the results in the previous line$TransportServers = Get-TransportServer$TransportServers | % { Get-MessageTrackingLog -Start 12/1/2014 -Server $_.Name }
  2. Add OutBuffer
    The common parameter OutBuffer can be used to set how many results need to be returned before moving to the next command (or it needs to get to the end of the result set)Get-TransportServer -OutBuffer 999 | % { Get-MessageTrackingLog -Start 12/1/2014 -Server $_.Name }
  3. Add Sort-Object
    Adding Sort-Object will block PowerShell from going forward until it has all of the results and then has sorted them all.Get-TransportServer | Sort-Object Name | % { Get-MessageTrackingLog -Start 12/1/2014 -Server $_.Name }

Hint 6: Use Group-Object to summarise data into categories

The Group-Object cmdlet summarises a result set, e.g. if we want a count of the number of mailboxes subject to each retention policy we can do:

Get-Mailbox | Group-Object RetentionPolicy | Select-object Name, Count

Hint 7: Use Measure-Object to summarise data numerically

Measure-Object is an easy way to get a numerical summary into sum, minimum, maximum and/or average.

Get-MailbxoStatistics -Server Server1 | Measure-Object -Property TotalItemSize -Average -Minimum -Maximum -Sum

Hint 8: Use Out-GridView to graphically see results

The Command line is great but sometimes it is nicer to see results graphically and to be able to further filter the results.

Get-Mailbox | Out-GridView

I hope this collection of hints will help you in your foray into PowerShell mastering!

Twan van Beers

Twan is a senior consultant with over 20 years of experience. He has a wide range of skills including Messaging, Active Directory, SQL, Networking and Firewalls. Twan loves to write scripts and get deep and dirty into debugging code, in order to understand and resolve the most complex of problems.

Leave a Reply

Your email address will not be published. Required fields are marked *

Search