Archive for June 16th, 2009

Measure script run time

When I am writing PowerShell scripts, one thing that is always in my mind: How long will it take to run this script and what can I do to reduce the run time.

While optimizing the code itself is not the scope of this article, what I am going to write may help you determine how long your script, function or code sections takes to run.

Insert the following at start of the section of code you want to check:

# Get Start Time
$startDTM = (Get-Date)

Insert the following at the end of the section of code you want to check:

# Get End Time
$endDTM = (Get-Date)
 
# Echo Time elapsed
"Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"

Here is an example of my code optimization:

BEFORE (Run Time 40.4 seconds):

filter GetMailboxCount{
$Server = $_
 
$strFilter = "(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(msExchHomeServerName=$Server)) ))))"
 
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
 
$colResults = $objSearcher.FindAll()
$colCount = ($colResults | Measure-Object | %{$_.Count})
"$Server,$colCount"
 
}

AFTER (Run Time 4.53 seconds):

filter GetMailboxCount{
$Server = $_
 
$strFilter = "(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(msExchHomeServerName=$Server)) ))))"
 
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
 
$colProplist = "name"
$retval = $colProplist | foreach {$objSearcher.PropertiesToLoad.Add($_)}
 
$colResults = $objSearcher.FindAll()
$colCount = ($colResults | Measure-Object | %{$_.Count})
"$Server,$colCount"
 
}

As you will see the “name” property is irrelevant to my code but by querying on indexed attribute and limiting scope of the query, I was able to improve run time ten folds. Now that is huge time savings when you have to run this code in a loop across all of your servers!

  • Share/Bookmark
Print

Tags:

How do I check Rollup Update version on Exchange 2007 server?

This is one question that gets asked around many times! The article “Exchange Server 2007: Platforms, Editions and Versions” gives important information to identify the build numbers for each Rollup Updates, however, it is unclear where to look for this information.

To solve that problem, I have come up with a script which looks at Exsetup.exe version and registry entries for each Rollup Update installed…

Uh, what happened to the rest of this post?

Well, since I wrote a better script that handles both Exchange 2010 and Exchange 2007, I have removed this script. You can find new version here.

  • Share/Bookmark
Print

Tags: ,