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.
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
}
}
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
}
}
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.
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
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.
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
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
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!