James O'Neill's blog

Windows Platform, Virtualization and PowerShell with a little Photography for good measure.

A little more on PowerShell

A little more on PowerShell

  • Comments 6
  • Likes

I've been showing PowerShell on the roadshow, and Steve warning me about it becoming the "Look-how-clever-I-am-with-PowerShell show". Actually, I quite like the idea of a "Look-how-clever-PowerShell-makes-you show". Working on some of bits and pieces of PowerShell for managing hyper I've found a couple of new corners which I thought I'd share.

I wanted to show the tree of snapshots taken of a Hyper-v Virtual Machine ... and let the user choose one of them.  I ended up with a semi-generic "Choose-Tree" function, and that sent me back to the choose functions I've written about before. I wanted to have something totally generic like this

   choose-list (Get-WmiObject win32_diskdrive) @("DeviceID", "Model")

ID DeviceID           Model
-- --------           -----
0 \\.\PHYSICALDRIVE0 Hitachi HTS721010G9SA00 ATA Device
1 \\.\PHYSICALDRIVE1 Generic USB Card Reader USB Device
2 \\.\PHYSICALDRIVE2 Multi Flash Reader USB Device

Which one ?:

The idea is simple enough, pass the function an array of data and other array of the field names I want displayed, put a counter alongside them and let the user enter the index into the array for the item they want.

This led me to using variables and parameters in places I haven't done before One was using a variable to hold a field name. Like this

   $foo="TotalPhysicalMemory"
   (Get-WmiObject win32_computersystem).$foo

Very useful if I need to pass a parameter to a function to tell it which property to use from the objects it was passed. Building a tree for example, which field is displayed, which gives a node's parent, and so on. Then I started looking at using an array of strings to hold the field names, like this

   $foo=@("TotalPhysicalMemory","Model")
Get-WmiObject win32_computersystem | format-table -property $foo

That works very nicely too. Because Powershell lets us mix types of item in an array we can put in hash tables which store custom fields. So a design formed... Pass data and Fields as parameters, add the counter custom-field that I was already using in my choose functions to the Fields array (put it an array and join it with the array of fields passed to the function), and use that in a format-table command. Since some of Choose functions had multiple selections and some single I had a "Multiple" parameter and that changes the prompt / selection at the end. So my generic choose-from a-list function ends up as half a dozen lines.

   function choose-List
   {Param ($Data , $fieldList , [Switch]$Multiple)
    $global:Counter=-1
    $fieldlist=@(@{Label="ID"; expression={ ($global:Counter++) }}) + $fieldList
    $data | format-table -autosize -property $fieldList | out-host
    if ($Multiple) { $Data[ [int[]](Read-Host "Which one(s) ?").Split(",")] }
    else           { $Data[        (Read-Host "Which one ?")           ] }
   }

Each "choose" function - choose VM, Choose network etc then becomes one line.

Technorati Tags: ,
Comments
  • I really enjoyed the manchester roadshow, I'm now  going to have a play with powershell with the ideas you gave so keep it up!

    - Andy C

  • Manchester was great - and I particularly liked the look how clever you can be with Powershell.

    Can you share more of the stuff that you did with Hyper-V and VMs. I'm particularly interested in the ability to create VMs on the fly ...

    Keep up the good work

    Thanks

  • Tim, Andy, Glad you enjoyed the roadshow. They're take a lot of work (and not just from the guys on stage) so it's always good to hear that people appreciate it.

    Tim, I'm putting up the bits a little at a time so a few posts back you'll find one on creating hard disks. And in the next few minutes I'll post one about snapshots.

    Then will come the generic process for adding and modifying resources.

    I'm also trying to find out why one key bit of code (which creates the New VM) only works in powershell 2.0 - probably the way I've written it. I'll make the whole thing available in one piece and if anyone's keen to be a beta tester than can mail me and I'll let them have a copy. I'm probably at about V0.6 or V0.7 right now. No remote support, no remove NIC, or SCSI controller, no change NIC connection, no import/export of configuration information. No expand or Merge of VHD files. No changing of bios type settings for the VM. Apart from those everything else is working.

  • In my last post I explained how snapshots work and gave a little bit of PowerShell for creating a one

  • Please excuse the bad pun... When I first wrote the function I posted to display the state of virtual

  • If you've read my post on adding disks to a Virtual machine , the techniques here should already feel

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment