Pipelines and Objects

Pipelines and Objects

Two strong points of PowerShell are the ability to work with Pipelines and Objects. PowerShell is built on top of .NET and has the ability to work with .NET objects as well as sending and receiving information from the pipeline.

Objects

To developers, asking them to go from an object based language to one that parses text for input/output would get you laughed at. That is exactly why when Microsoft decided to write a new console and scripting language they decided to work with objects instead of text parsing.

Those familiar with administrating Unix operating systems are aware of programs like grep, sed, and awk. Those three programs allow the administrator to take text as input, manipulate the text, and output commands for the next program.

Windows is an operating system built around Application Programming Interfaces (API) which allow sophisticated interactions with the underlying OS. Those APIs return objects. Objects contain attributes which house information about that specific object as well as methods, or little functions that can be performed on the object. There are many other properties that apply to objects but those are the important ones.

PowerShell, through cmdlets, allow access to the Windows API, surfacing information about processes, services, files, and more. These types of objects come with rich information and allow you to interact with them in a safe way.

To find out what type of object you’re working with in PowerShell pipe any ‘Get’ command to the cmdlet Get-Member.

[code lang=powershell]
Get-Process -Name Powershell.exe | Get-Member
[/code]

You’ll notice some interesting information on the console. First, you see the type of object sent to Get-Member. The process object is part of the System.Diagnostic object library is referred to by System.Diagnostic.Process. The name is long to make sure that there aren’t type name collisions if there are two completely different types of process objects on a computer.

Next you get the name of the property, property type in member type, and definition. The property name is how you access the information inside of the object. To list the Name,Threads,ID, and HandleCount you can use select-object.

[code lang=powershell]
Get-Process -Name Powershell | Select-Object -Property Name,Threads,ID,HandleCount
[/code]

Pipeline

The pipeline is really a important concept. The idea is that you have small programs that do one thing for you. Get-Process retrieves processes. Set-ADUser will manipulate a user in Active Directory. Cmdlets work best when strung together, one outputting objects, another taking objects as inputs. Consider the following:

[code lang=powershell]
Get-ADUser -Identity NGetchell | Set-ADUser -Title "Automation Wizard"
[/code]

Get-ADUser goes out and finds my user account. It then pushes the Microsoft.ActiveDirectory.Management.ADUser object into Set-ADUser. Set-ADUser will now set the title of the incoming object to ‘Automation Wizard’. In this scenario only one user object was manipulated. Image your 1,000+ employee company has been purchased. You’ve been tasked with changing the Company field in AD for each employee.

[code lang=powershell]
Get-ADUser -filter * | Set-ADUser -Company 'Getchell.org'
[/code]

You can now go home early since you completed hours of manual work in seconds. You earned it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.