Monday, March 12, 2012

Month of Lunches - Day 4

Sorry for the delay folks but the Flu can do that to you.

Ok so now we start to get to the meat of what makes PowerShell so Powerful (pardon the Pun), and that is the ability to connect commands. Chapter 4 is all about pipelining or piping one command to another and some of the output cmdlets within "The Shell".

The first thing we cover is the outputting of commands to Comma Separated Value (CSV) files and Command Line Interface Extensible Markup Language (CliXML or just XML) files. The Export-CSV, and Export-CliXML and their inversely named Import commands allow for an easy way to output and archive almost any information that you can get out of PowerShell or import previously exported information for reference or for use.

Let's try something shall we? In your console type

                Get-Process

This will display a list of all running process on your computer at that very moment as well as the process ID, CPU utilization percentage, memory utilization, and a few other pieces of information. In PowerShell this is actually an object and only a small portion of the information that is gathered is actually displayed. Don't believe me? Want to see the rest? Let's export that to a CSV file.

                Get-Process | Export-CSV <filepath>\process.csv (filepath being where you created the file before)

There are literally 60 plus columns of information. In fact all of the Get- cmdlets i tested are the exact same way. Export-CliXML will show you the same thing, only in a slightly less readable, but very useful xml structure (if you know what you are working with).

Now say you have two computers that are supposed to be identical but want to make sure (very useful if you are troubleshooting issues with programs at startup). Well you're in luck. You have the ability to compare files. Compare-Object, or better known by its alias DIFF, can take two objects and compare them and report the differences. You can even compare two CSV or XML files for that matter. An important thing that Don notes here in the book is that PowerShell is not very good at comparing text documents. This is good to remember and will save you headaches later.

Ok so let's give this one a shot. Lets open a couple of miscellaneous programs like Calculator, Notepad, and Paint. Remember the export from earlier, let's compare it to our current process'. We only want to see what is new by the process name so lets specify that with the -Name parameter. The reason we do this is that we are comparing all the process' and their associated information and something is bound to have changed, like memory usage, CPU utilization, or any other piece of information.

Compare-Object -ReferenceObject (Import-CSV <filepath>\process.csv) -DifferenceObject (get-process) -Property Name

Phew that is alot to type. No worries. Remember in PowerShell you can use alias' or shorten parameter names as long as what you use is different enough that PowerShell knows what you are typing. For instance the below command will do the same thing.

Diff -Ref (ipcsv <filepath>\process.csv) -Diff (get-process) -prop Name

Half the typing and all the power. You can even add other property names to show other results if you want for different outputs.

This is only the beginning, you can even convert the output files to HTML (Convert-HTML) that can viewed in any browser, pipe them to a text file (Get-Process | out-file <filename>), output them to a printer (get-process | out-printer), as well as a few other out commands. As we went over on the previous day, use the Get-Help or the Get-Command cmdlets to find and learn more about the other Out- cmdlets or any cmdlets for that matter.

No comments:

Post a Comment