Archive for the ‘IT’ Category

Dump Virtual Machine Info as XML (powershell)

Hi All,

I was recently asked to provide a comprehensive list of VM’s and their currently provisioned hardware. We have a need to duplicate this view in the future, so I decided to script it. My org also does charge-forward, so this dump of information is useful.

I used the script by Arne Fokkema “PowerCLI: Virtual Machine Disk (VMDK) info v2: Analyze data with Excel” as a base.

I initially tried to use the built-in XML export option, however the formatting wasn’t very nice, so I decided to build a custom schema and populate that instead. Using this approach allowed me to include all the detail I wanted, nested appropriately… for easy collation and searching later in excel.

So running through the script, this is how it works.

Firstly, like a good programmer, define the global variables.

#--- Parameters ----------------------------------------------------------------------------------------------
#XML Template
$XMLTemplateFile = "C:/tmp/VMachines_Template2.xml"
#Output XML File
$XMLOutput = "c:/tmp/VMachines.xml"
#Use Shortname?
$UseShortname = "True"
#Get Application details?
$GetAppdocInfo = "False"

Ignore the AppdocInfo flag…. that’s for another day 😉

Now, since we’re creating a custom XML output we need to define the template.

#Define the XML Template
$XMLTemplate = @'
... etc
'@
$XMLTemplate | Out-File $XMLTemplateFile -encoding UTF8

$XMLVMachines = New-Object XML
$XMLVMachines.load($XMLTemplateFile)
$XMLVMachine = @($XMLVMachines.VMachines.VMachine)[0]

The template is defined inline, written out to a temporary template file, then read back in as an array of XML objects (with only one object so far, the template). Once that’s done, we can populate the array by cloning the template and adding data.

Now we have a custom XML object to work with, we can begin collecting the required data. Read the rest of this entry »

The opinions expressed on this site are my own and not necessarily those of my employer.

All code, documentation etc is my own work and is licensed under Creative Commons and you are free to use it, at your own risk.

I assume no liability for code posted here, use it at your own risk and always sanity-check it in your environment.