Mr. Roboto

PowerShell: The Next Generation

PowerShell 2.0 introduces many shiny new features that are built on a solid 1.0 core first delivered in 2006.

I'll always remember the first version of Windows PowerShell fondly, much like I enjoyed the original "Star Trek" series. But that doesn't mean I wasn't excited with the arrival of "Star Trek: The Next Generation." The sequel introduced numerous technologies and concepts such as the "holodeck," which provided an early glimpse of virtual reality, yet remained faithful to its roots. The same is true today with PowerShell version 2.0. PowerShell 2.0 introduces many shiny new features that are built on a solid 1.0 core first delivered in 2006. With PowerShell 2.0, you have a veritable Swiss army knife of features at your disposal.

Remote Improvements
If you can type, you can perform common management tasks right from your desk. Here's how easy it is to see service information on a remote computer using the Get-Service cmdlet:

PS C:\> get-service -computer 
FILE01

If you can work with one remote computer, you can easily work with several by using a slightly longer one-line PowerShell expression:

PS C:\> get-service -computer 
FILE01,FILE02,DC1,DC2 | Sort 
MachineName,Status | format-
table Status, Displayname, 
MachineName -autosize

Because we're getting service information from multiple computers, we need to see the ComputerName via the MachineName property. To keep everything easy to follow, PowerShell is sorting the service objects from Get-Service in all the computers by means of the MachineName property first and then the Status property. The sorted objects are then piped to the Format-Table cmdlet to produce an easy-to-read report.

Let's look at another typical management task. What processes are running on those servers?

PS C:\> get-process -computer 
name FILE01

Using this cmdlet to get process data for multiple computers in a meaningful manner is more complicated than I'd like. But there's an easier approach to getting the results you're familiar with via the Get-Process: Use the Invoke-Command cmdlet. This will require that PowerShell 2.0 be installed on any remote computer:

PS C:\> invoke-command -Com 
puterName FILE01,FILE02
 -ScriptBlock {get-process}

This cmdlet will create a remote session on the specified computers. Execute the command in the script block via the Get-Process cmdlet and return the results to your computer. How much easier could it be? Well, what about a GUI?

Introducing GridView
Even though PowerShell is a console application, it contains GUI elements, including one called Out-GridView. You can pipe any PowerShell expression to the Out-GridView cmdlet:

PS C:\> get-process -computer 
name FILE01 | out-gridview

Out-GridView displays the results from Get-Process in a graphical grid. You can click on column headings to sort or use the Add Criteria button to filter the results. These are all things you can do from the PowerShell prompt using the Sort-Object, Select-Object and Where-Object cmdlets. However, you'd have to keep re-running the expression at the prompt. Using Out-GridView, you can manipulate the data just about any way you want. One thing to keep in mind is that this isn't like Task Manager. You're not dealing with live data but, instead, the process state that existed when you ran Get-Process. Here are some more examples:

PS C:\>get-service -computer   
FILE01| out-gridview

PS C:\> get-eventlog system

-newest 25 -EntryType

Error,Warning -ComputerName
FILE02 | out-gridview

PS C:\> get-hotfix -computer
FILE01,FILE02 | out-gridview

If you want to simulate the results without connecting to a remote computer, use your own ComputerName.

You can accomplish so much from a PowerShell prompt that many previous tools covered here may be obsolete or, at the very least, due for an upgrade. To leverage all the benefits of PowerShell 2.0, stay tuned. Meanwhile, check out the PowerShell forum I moderate at ScriptingAnswers.com.

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.

Featured

comments powered by Disqus

Subscribe on YouTube