WMI, PowerShell - How well do they gel?

WMI, PowerShell - How well do they gel?

  • Comments 3

< Kapil Mathur is in the process of learning PowerShell to work with WMI and wrote up his findings to date – jps>

Automating admin scenarios using PowerShell

The preferred scripting language for the WMI world has been vbscript. But if you are a windows administrator writing WMI scripts, chances are that you have been hearing another name lately- PowerShell.

 To compare the scripting experience in between the two, I tried out my hand at automating some admin scenarios by converting "vbscript" scripts into "PowerShell" scripts.

I took two scripts from Alain Lissoir's book "Leveraging Windows Management Instrumentation (WMI) Scripting" (http://www.lissware.net ) and rewrote them into PowerShell. The scripts covered the following areas -  

  1. Inventory – viewing dcom application settings (getting the data, extracting useful properties and displaying/storing them somewhere.)
  2. Configuration - This script based tool is a complex network configuration script which can configure 15 different network settings like enableDHCP, enableStaticIP, setDNSDomain etc.

     

So here's my experience with PowerShell vis-à-vis vbscript  so far –

  1. Inventory scripts are about, getting some data (fromWMI in our case), extracting the relevant data (specific instances and properties) and then either displaying them or storing them somewhere. In all these tasks, PowerShell does pretty well. Let me demonstrate -
    1. Display - Alain had written a displayFormattedPropertyFunction() to display formatted properties (length ~100 lines.) This whole formatting thing is encapsulated and taken care of by wmi adapter object and cmdlets like fomat-list,format-table. So user gets all formatting absolutely free!
    2. If there is a need to export these objects somewhere, in vbs you have to get all the properties of the object and write them one by one in xml or csv for example. In PowerShell, you just call cmdlets like export-csv, export-xml after extracting the relevant data using select-object. Zero parsing!
  2. For the network configuration scenario Length of vbscript ~ 500, length of PowerShell script ~ 200 . Agreed you can't take numbers at face value but it is plenty of motivation for me to switch to PowerShell. And I am yet to learn many powerrshell tricks.
  3. String processing abilties - Inspite of all the hype around the ability to handle objects, there will be cases where string parsing would still be needed (like a script which takes SDDL as input and then does something on it will need to parse the string.)  So how does PowerShell fare here? PowerShell has excellent string parsing abilities due to inherent support of .net string object and select-string cmdlet. So if you take a comma separated input, all you do is call $value.split(",") and you get an array of strings.
  4. Array support – Some WMI methods take arrays as inputs, so this is important to us. Arrays in PowerShell are neat. I don't have to think about allocating some memory before and then reallocate space using redim (in vbscript). I just go on adding to my array using a '+' operator! Eg. –

    $IPAddresses = "125.125.125.125","200.200.200.200"

           $IPAddresses = $ IPAddresses + "250.250.250.250"

Or if you want to create a string array of some size and initialize it, you write -  [string[]]$IPAddresses = @("cool")*$ParsedValue.length

  1. Comparison with wsf – One of the good things about the wsh world is wsf format. You can define named and typed paramters or include other scripts in your script at no cost. Well, PowerShell matches wsf's features and there is no additional xml file to define as well. Just use the name of the parameter inside the script (param($EnableDHCP)) as the named parameter (somescript.ps1 –EnableDHCP <value>).
  2. Dynamic Typecasting – Two examples for this –
    1. In vbscript to convert to int, I use var2= cint(var1). In PowerShell, I simply assign the type $var2 = [int]var1
    2. To get a wmiclass object all I do is - $NetworkClass = [WmiClass]\\.\root\cimv2:Win32_NetworkAdapterConfiguration

In another post, I'll cover the wmi based monitoring story with PowerShell.

Kapil Mathur

Leave a Comment
  • Please add 8 and 6 and type the answer here:
  • Post
  • 1) I fail to see how: "$var2 = [int]var1" is any better (or quicker) than "var2=cint(var1)".

    2) String processing: In powershell you do '$value.split(",")' which, to be honest, isn't much of an improvement on 'var2 = Split(var1,",")' that you get is VBScript.

    3) My only problem with fomat-list and format-table is that the output of these isn't always as readable as I would like.

  • ps> 3) My only problem with fomat-list and format-table is that the output of these isn't always as readable as I would like.

    Can you give an example and/or offer suggestions of what we should do better?

    Thanks!

    Jeffrey Snover [MSFT]

    Windows Management Partner Architect

    Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell

    Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  • Last week the powershell team posted some of my thoughts on how well wmi and poeershell work together

Page 1 of 1 (3 items)