Saturday, September 13, 2008

OpsMgr Command Shell tip: where-object vs criteria

I've seen a few OpsMgr Command Shell users posting scripts that use the get-alert cmdlet.

I've seen a few references, where people are using the format:
get-alert|where-object{some_criteria}

When this format is used, get-alert retrieves *all* the alerts from the OpsMgr database, and leaves PowerShell to do all the work of handling all the objects that come in, and then where-object also causes more PowerShell processing time to handle all of the objects passed from get-alert.

Although this may work fine, and a larger environment, this approach will take longer to finish this particular pipeline.

Compare the above format to:
get-alert -criteria 'some_criteria'

I had recently mentioned this in a TechNet Magazine article I wrote. Now if you don't believe me... I'll quote a recent microsoft.public.opsmgr.powershell post by Boris Yanushpolsky a Program Manager on the Microsoft OpsMgr team:

"Using the criteria is significantly more efficient as the query is handled by SQL and only the instances/alerts which meet the criteria are returned to PS. Here is a link to an MSDN article which explains the syntax:

http://msdn.microsoft.com/en-us/library/bb437603.aspx"

The syntax and case used for the query can be a bit harder to master, but it is worth the effort to learn it in larger environment.