There has been something bugging me lately about PowerShell. Actually, this something has probably always bugged me. But, when I saw this series: “Hyper-V WMI Using PowerShell Scripts” on the Virtualization Team’s blog. I for some reason became annoyed.
Here is the thing. In my opinion PowerShell has become stuck in a WMI Quagmire. For some reason, the default approach has become to “PowerShell-ish” a script that uses WMI to complete a systems management task. In other words, rather than creating a set of cmdlets, WMI becomes the vehicle to complete the tasks at hand.
Now, don’t get me wrong, I like WMI. In fact, I often use WMI within my own scripts. However, WMI is a pain in the butt to work with (yes, PowerShell drastically improves this experience) and I’m a scripting geek. On the other hand, PowerShell is supposed to be for the IT-Pro. I know a lot of IT-Pro’ers, and they don’t like working with WMI even within PowerShell.
For example, with the Hyper-V examples that I reference earlier, there is a posting entitled: “Hyper-V WMI Using PowerShell – Part 4 and Negative 1“. In this posting, two examples are given that show how to shutdown a virtual machine using WMI. The first example uses PowerShell, and is only three lines long.
$Vm = Get-WmiObject -Namespace rootvirtualization -Query “Select * From Msvm_ComputerSystem Where ElementName=’Vista'”
$ShutdownIC = Get-WmiObject -Namespace rootvirtualization -Query “Associators of {$Vm} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_ShutdownComponent”
$ShutdownIC.InitiateShutdown(“TRUE”, “Need to shutdown”)
The second example uses C# and is about 40 lines long. Clearly, the PowerShell method is a huge improvement. Nevertheless, that doesn’t mean there isn’t room for even more improvement. Looking at the PowerShell example, I still see a jumbled mess where a set of cmdlets would greatly improve an Administrators experience. Perhaps something like:
$VM = get-hvvm -name “myvm123”
stop-hvvm $VM -shutdown
It seems simple enough, however, for some reason the MS product groups and other third-parties seem insistent on forcing PowerShell + WMI on the masses. Rather than developing a robust set of cmdlets that Administrators could use to manage their systems. We are handed a WMI provider. :
Luckily, not all is lost with Hyper-V. It seems that someone has taken on the torch to create a set of cmdlets functions. You can get those functions here: “PowerShell management Library for Hyper-V“.




