PowerShell and Software Testing

Windows PowerShell is Microsoft’s new command shell and scripting language. You can think of PowerShell as a replacement for the old cmd.exe shell and .bat files. I have found that PowerShell is extremely useful in a software testing environment. I will be presenting a PowerShell talk and three hands-on labs at the upcoming 2008 Microsoft Management Summit (https://www.mms-2008.com/public/home.aspx) from April 28 – May 2 in Las Vegas. I’ve spoken at MMS before and it is a huge and intersting conference and has all kinds of useful information related to Microsoft based IT topics. This year is already sold out but you mighgt want to consider attending next year. Anyway, here’s a brief snippet from part of my PowerShell talk where I describe how PowerShell makes it much easier to inteact with WMI.
Windows PowerShell provides you with a great interface to WMI functionality. WMI (Windows Management Instrumentation) is the Microsoft implementation of the CIM (Common Information Model) standard. IT administrators can use WMI to retrieve and manipulate a wide range of information about the hardware and software resources on a computer network. Working with WMI using Windows PowerShell is generally quite a bit easier than using than older scripting technologies. In this Lab Exercise you will learn how to access and control Windows services using the get-wmiobject cmdlet. The screenshot below illustrates the key ideas in this exercise.
1. Enter the commands:
(default directory)> set-location \
PS C:\> get-wmiobject -list | more
The key to working with WMI in Windows PowerShell is the get-wmiobject cmdlet. If you supply a -list switch, the get-wmiobject cmdlet will list all the WMI classes available to you — typically over 1,000 of them. Many Windows-related WMI class names begin with "Win32_" so if you pipe the output of "get-wmiobject -list" to "select-string Win32_" you can view just those classes. Notice that in this special case, you pipe to the select-string cmdlet rather than the more usual select-object cmdlet.
2. Enter the commands:
PS C:\> $s = get-wmiobject -class win32_service
PS C:\> $s.count
The WMI class which is most often used to control Windows services is the Win32_Service class. The command above fetches information about all the services on your host machine and stores them into a collection named $s. As a general rule of thumb, when you issue a command using the get-wmiobject cmdlet, you will get either a single object (for example, with the Win32_SystemTimeZone class), or a collection of objects (as with the Win32_Service class). You can use the Count property to determine how many objects you have. Note that Windows PowerShell has a built-in get-service cmdlet you can use to control services on a host machine. However, the get-wmiobject cmdlet can access remote machines as well as local machines. For example, the command "get-wmiobject -class win32_service -computername Hercules" will fetch information about all services running on a machine named Hercules.
3. Enter the commands:
PS C:\> $a = get-wmiobject -class win32_service -filter "name=’alerter’"
PS C:\> $a
In most situations you want to get a reference to one particular WMI object — in this case we want a reference to the Alerter service. The command above shows a Windows PowerShell idiom to do this. The -filter switch parameter of the get-wmiobject cmdlet can select just a particular object from a WMI class with multiple objects.
(etc.) PowerShellAndWMI
This entry was posted in Software Test Automation. Bookmark the permalink.