August 18, 2009

One-Liners

As I mentioned in the previous post I have not written any formal scripts in some time.  I have focused on one-liners recently.  What is a one-liner you ask?  Powershell offers a great deal of tools that can be called up and used right from the Powershell command line. 

I think this is were Windows SysAdmins need to catch on to Powershell.  Scripts are great, especially for repetitive or large tasks but sometimes that is too cumbersome when you need to tackle a quick task or return information quickly. 

How does this apply?  We were recently asked to verify the specific version of Windows installed on a list of machines.  Yes, I had written a vbscript a few years ago to do that but wanted to approach it with Powershell.  Being lazy, I knew WMI would return the information so looking at my existing script I started with a simple wmi query: gwmi win32_operatingsystem, which returns several useful fields of data such as Build Number and Version.  For our use they wanted the publicly known name which I knew from previous of WMI was the Caption field so I was able to expand the one-liner to gwmi win32_operatingsystem | select caption so now I am accessing the same collection of information but now I am selecting just a specific field which is the Publicly known name of the operating system.

caption
-------
Microsoft Windows 7 Ultimate

So, you are sitting there and saying “I can just right click on the computer object and check properties”, but what if you wanted to check a remote computer, now you have to access that computer, with the one-liner I simply add the –computername parameter, as long as I have the necessary credentials I can return the same information from that computer.

Pretty easy right?  Pretty low key example though.  Now consider the ability to do the same type of operations with things like Active Directory and Exchange.  I have found it interesting that while on a conference call in the past, a request for information would be requested and the standard answer was a script, now in many cases a quick one-liner produces the basic information ASAP.  This is so much fun when someone estimates data and you can counter with the actual data, makes planning much more precise.