Hyper-V, RegEx, Resources, Scripting, Virtualization, Windows
comments 2

VBScript to parse GetVirtualHarddiskInfo() output

As mentioned in my earlier post, Msvm_ImageManagementService.GetVirtualHarddiskInfo() doesnot return the embedded instance of Msvm_VirtualDiskInfo class. Instead, it just returns one big string as shown here

instance of Msvm_VirtualHardDiskInfo {Type = 3;FileSize = 2405406208;MaxInternalSize = 32212254720;InSavedState = TRUE;InUse = FALSE;};

Parsing this for individual values of Type, FileSize, MaxInternalSize, InSavedState and InUse will be a very tedious job if you have no clue about using regular expressions. Regular expressions could be quite intimidating because of their cryptic nature. Many people don’t even look at RegEx because of the same reason. I was also in the same state until last week. But, when I came across this limitation of GetVirtualHarddiskInfo(), I got a copy of Beginning Regular Expressions by Andrew Watt and started learning regular expressions. I just completed the reading and here is the outcome of what I just learned.

This script parses the output of Msvm_ImageManagementService.GetVirtualHarddiskInfo() and gets the individual value. As you can see in the script, the regular expression I had used is (?:Type = )([A-Za-z0-9]*)(?=\;). The only capturing group in this RegEx is ([A-Za-z0-9]*) . This one will contain the value you are retrieving.

Regular expressions are a really powerful way of manipulating or parsing text. I realized what I had missed all these days. Last but not least, there is whole new category (RegEx) because of this post. You will see more in this category soon