PowerShell for Windows Admins

Nov 2 2011   2:32PM GMT

Why use the pipeline?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


In this post http://msmvps.com/blogs/richardsiddaway/archive/2011/10/31/multiple-value-query-in-wql.aspx I showed how to use Get-WmiObject, WQL and Invoke-WmiMethod to stop multiple services.  The code looks like this

Get-WmiObject -Class Win32_Service -Filter “State=’Running’ AND Name LIKE ‘%BITS%’ OR Name LIKE ‘%WinRM%'” |
Invoke-WmiMethod -Name StopService

I was asked why I didn’t use foreach – ie make the PowerShell code look like this

Get-WmiObject -Class Win32_Service -Filter “State=’Running’ AND Name LIKE ‘%BITS%’ OR Name LIKE ‘%WinRM%'” |
ForEach-Object { Invoke-WmiMethod -Path $_.__Path –Name StopService }

Fine the code works but it is more complicated than we need.

lets look at Invoke-WmiMethod

type Invoke-WmiMethod –      then hit the tab key

we get this

Invoke-WmiMethod –InputObject

Now try

Get-Help Invoke-WmiMethod –Full

One of the parametersets looks like this

Invoke-WmiMethod -InputObject [-ArgumentList ] [-Name] [-AsJob] [-ThrottleLim it ] [-Confirm] [-WhatIf] []

Notice that InputObject is the first parameter.

Now try this

PS> Get-Help Invoke-WmiMethod -Parameter InputObject

-InputObject <ManagementObject>
Specifies a ManagementObject object to use as input. When this parameter is used, all other parameters except the Flag and Argument parameters are ignored.

Required?                    true
Position?                    named
Default value
Accept pipeline input?       true (ByValue)
Accept wildcard characters?  false

This parameter accepts pipeline input. It is the only parameter for Invoke-WmiMethod that does.

This means that when  Invoke-WMiMethod is on the pipeline and receives an object it will check to see if it is a ManagementObject  object i.e. a WMI type object and if it is it will accept it as input through the InputObject parameter.

Our original pipeline creates some WMI objects using Get-WmiObject.  They are then put on to the pipeline. Invoke-WmiMethod identifies them as WMI objects and tests to see if they have a method called StopService.  They are created from the  Win32_Service so they better have that method.  Invoke-WmiMethod  then invokes the method.

The alternative

Get-WmiObject -Class Win32_Service -Filter “State=’Running’ AND Name LIKE ‘%BITS%’ OR Name LIKE ‘%WinRM%'” |
ForEach-Object { Invoke-WmiMethod -Path $_.__Path –Name StopService }

effectively does the same BUT involves more work on out part.  The WMI objects are put on the pipeline and then for each one we use the path to the object to identify the object on which we call the StopService method.

In practice the two sets of code do exactly the same – they stop the services. Just using the pipeline requires less typing and therefore less mistakes.

Use the pipeline – its what it was designed for

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: