A few weeks back I posted an article on matching Windows and VMware disks. Unfortunately this would work only if you could remotely query WMI information from that VM. If you have any VM that’s isolated or behind a firewall, you are out of luck. This bothered me, so I started looking for a uniform method to retrieve WMI information. I already knew that it was possible to run PowerShell scripts inside a guest using the Invoke-VMScript cmdlet, but unfortunately I don’t have PowerShell installed in all my VMs yet. When reading through the change log of PowerCLI 4.0 Update 1, I noticed the following enhancement of the Invoke-VMScript cmdlet:
|feature||Enhanced Invoke-VMScript to support BAT (Windows) and BASH (Linux) scripts.|
When it’s possible to run BATCH scripts inside a guest, it’s probably possible to run executables directly too. So I took this for a test and it turned out that it was working indeed. View the following piece of code to execute the “dir c:\” command inside a VM.
Connect-VIServer vmwsv392 $VM = Get-VM ( Read-Host "Enter VM name" ) $ESXHost = $VM | Get-VMHost $HostCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter ESX host credentials for $ESXHost", "root", "") $GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $VM", "", "") Invoke-VMScript "dir c:\" -vm $VM -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat"
When running this script on my environment, I receive a nice directory listing of the c: drive on that VM. Nice!
Let’s take this method one step further. As of Windows 2003 there’s a wmi command-line utility available to us called “wmic.exe”. After fiddling around with all the available options of this command, I figured out a way to get WMI info in csv format. The syntax I need to provide me that information is:
wmic path <WMI_class> get <property_list> /format:csv
For example I can retrieve a list of diskdrives and their sizes using:
wmic path win32_diskdrive get caption,deviceid,size /format:csv
The next challenge I faced, was the fact that the info is returned as a string. I needed a way to convert the string value to an array, preferably an array of objects. All I was looking for was the output of the Import-Csv cmdlet, but unfortunately this cmdlet doesn’t accept data on the pipe. The solution I found to this is rather simple, just dump the string value to an intermediate temporary file and import that file using Import-Csv.
To do this properly we first have to cut off the first 2 characters of the string (There’s an extra carriage return and line feed character at the beginning) or else we can’t import the file using Import-Csv, because it expects the header to be on the first line.
I’ve modified the previous example code a bit as shown below:
Connect-VIServer vCenterServer01 $VM = Get-VM ( Read-Host "Enter VM name" ) $ESXHost = $VM | Get-VMHost $HostCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter ESX host credentials for $ESXHost", "root", "") $GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $VM", "", "") $Out = Invoke-VMScript "wmic path win32_diskdrive get caption,deviceid,size /format:csv" -vm $VM -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat" $FileName = [System.IO.Path]::GetTempFileName() $Out.Substring(2) > $FileName $In = Import-Csv $FileName Remove-Item $FileName
When you look at the output window, you clearly see the differences between the
$in object can easily be used inside PowerShell scripts.
Imagine the possibilities!
- Setting custom attributes with VMware PowerCLI Tweet Last week I wanted to extend my vCenter with some extra custom attributes on my VMs. This would extend the usability of the Export List feature for reporting purposes....
- Match VM and Windows harddisks using PowerCLI Tweet Today I was asked to extend a disk of a Windows virtual machine. Normally this is a standard procedure and finished within minutes. The hardest part of the procedure...
- PowerCLI 4.0 Update 1- Another leap forward Tweet Last week VMware released PowerCLI 4.0 Update1. According to the PowerCLI blog this new version contains over 60 new cmdlets as well as greatly improved performance. One great thing...
- Export and import customization profiles using Powershell Tweet One great thing in the automation of VM deployments is the use of customization profiles. These profiles are stored inside the vCenter Server database. However, when you loose the...
- $Null or Nothing? Tweet When looking at a lot of PowerCLI scripts available I noticed that people tend to forget to validate their output. For example let’s look at the following piece of...