Pipelines


One of the most powerful and possibly confusing aspects of PowerShell is its data pipelines, which provide a means of passing data and objects from one cmdlet to another in a very robust fashion. Perhaps you've used the Cmd.exe More utility such as the one shown below to slow down the display of a long directory.

 C:\>Dir | More 

This takes the output of Dir and "pipes" it to More, which displays the data in nice pages, and waits for you to hit a key before displaying the next page.

Pipes have the same basic function in PowerShell. In fact, that character in between Dir and More is called the pipe character, which is usually on the backslash key of your keyboard. Here's a robust example:

 PS C:\>Get-Process | where { $_.handlecount -gt 400 } | Format-List 

PowerShell also has a More command if you want to pipe multi-screen output to it for one-page-at-a-time display.

This example is actually executing three cmdlets. The first, Get-Process, returns a list of all running processes. Each process is actually an object, of sorts, with various properties. The processes are all piped to where, which is an alias for the Where-Object cmdlet. Its job is to sort through a list of objects and pull out those that match some criteria. In this case, the criteria is where their handlecount property is greater than (that's the -gt argument) 400. All of that is piped to the Format-List cmdlet, which creates a nice, pretty list of the results:

 PS C:\> Get-Process | where { $_.handlecount -gt 400 } | Format-List ProcessName : csrss Id          : 1080 ProcessName : explorer Id          : 1952 ProcessName : Groove Id          : 2656 ProcessName : inetinfo Id          : 1524 

(This output is truncated for illustrative purposes.) The ability of pipes to pass data to other cmdlets, and the ability of PowerShell to deal with complex, structured objects in a text interface such as the Process object, is part of what has people so excited about PowerShell. You can, in just a single line of code, complete fairly complex administrative tasks.



Windows PowerShell. TFM
Internet Forensics
ISBN: 982131445
EAN: 2147483647
Year: 2004
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net