Windows Tip Sheet
PowerShell Pipe Dream
String along those Powershell expressions using this nifty trick.
- By Jeffery Hicks
- 01/24/2007
One PowerShell feature that you will want to take advantage of is the ability to pipe the output from one expression to another. Because PowerShell pipes complete objects, it is relatively easy to string a number of expressions together, each one feeding the next. Let’s look at a practical application.
Let’s say you need to know when a particular service started on a member server that is running PowerShell. There is no service property for StartTime. However, there is such a property for processes. All we need to do is find the process associated with a given service and display its StartTime.
Tech Help—Just An
E-Mail Away |
Got a Windows, Exchange or virtualization question
or need troubleshooting help? Or maybe you want a better
explanation than provided in the manuals? Describe
your dilemma in an e-mail to the MCPmag.com editors
at [email protected];
the best questions get answered in this column and garner
the questioner with a nifty Redmond T-shirt.
When you send your questions, please include your
full first and last name, location, certifications (if
any) with your message. (If you prefer to remain anonymous,
specify this in your message, but submit the requested
information for verification purposes.) |
|
|
Here are two expressions that get the job done:
PS E:\> $svc=get-wmiobject win32_service | where {$_.name `
>> -eq "lanmanserver"}
>>
PS E:\> get-process | where {$_.ID -eq $svc.ProcessID} `
>> | select -property StartTime
>>
StartTime
---------
11/17/2006 10:08:34 AM
In the first expression, you create $svc to hold the LanManServer service object. You do this by piping the output of the Get-Wmiobject cmdlet to the Where-Object cmdlet. You need that object because it has a ProcessID property, whose value you can use in the second expression to find the specific process that has the same ID. To only display the processes’ starting time, you pipe the output to the Select-Object. If the service is stopped, you’ll get an “Access Denied” error message.
Now let’s take this to the next level and combine both expressions into one line. Type the following as one line and you will get the same result:
get-process | where {$_.id -eq (get-wmiobject win32_service | where {$_.name -eq "lanmanserver"}).ProcessID} | select -property StartTime
Do you see where you nested the first expression to get the ProcessID? And do you see how each expression passes its result to the next expression?
Finally, let’s go over the top and add an expression that prompts the user for the service name using Read-Host. This expression is actually two, separated by a semi-colon, but should be typed all on one line:
$svcname=Read-Host "Enter a service name" ; get-process | where {$_.id -eq (get-wmiobject win32_service | where {$_.name -eq $svcname}).ProcessID} | select -property StartTime
I’m hoping that by now you’re thinking of your own ways to harness the power of the PowerShell pipeline.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.