# # Sources: http://ict-freak.nl/2009/10/11/powercli-virtual-machine-disk-vmdk-info-v2-analyze-data-with-excel/ # http://powershell.com/cs/blogs/tobias/archive/2009/02/02/xml-part-2-write-add-and-change-xml-data.aspx # # Notes : Written to export VM objects from vCenter for reports. # # Author : Doug Youd # #------------------------------------------------------------------------------------------------------------- #--- 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" #--- Main ---------------------------------------------------------------------------------------------------- #Define the XML Template $XMLTemplate = @' '@ $XMLTemplate | Out-File $XMLTemplateFile -encoding UTF8 $XMLVMachines = New-Object XML $XMLVMachines.load($XMLTemplateFile) $XMLVMachine = @($XMLVMachines.VMachines.VMachine)[0] #Get list of VM and ESXHost objects for vCenter $vms = get-view -ViewType VirtualMachine | Where-Object {-not $_.config.template} $ESXHosts = get-view -ViewType HostSystem #Get all the Portgroups (for the VLAN ID) $PortGroups= @() foreach($ESXHost in $ESXHosts){ foreach($ESXHostPortGroup in $ESXHost.config.network.portgroup){ $PortGroups += $ESXHostPortGroup.spec } } foreach($vm in $vms){ $Temp = "" | Select-Object FileName, DataStore, capacityinGB, Match, DevType, vNicPg, VMDiskTotalGb #Define a custom VMachine PSObject $VMachine = $XMLVMachine.Clone() #Populate Basic VM Info If($UseShortname -eq "True"){ $Temp.Match = $vm.Name -match '^[a-zA-Z0-9\-]*' If($Temp.Match) { $VMachine.VMName = [String]$matches[0] }else { $VMachine.VMName = [String]$vm.Name } }else { $VMachine.VMName = [String]$vm.Name } $VMachine.VMMem = [String]$vm.summary.config.memorySizeMB $VMachine.VMCpu = [String]$vm.summary.config.numCpu $VMachine.VMVNics = [String]$vm.summary.config.numEthernetCards $VMachine.VMVDisks = [String]$vm.summary.config.numVirtualDisks $VMachine.OSVersion = [String]$vm.summary.config.guestFullName #VM Device Info foreach($dev in $vm.config.hardware.Device){ $Temp = "" | Select-Object FileName, DataStore, capacityinGB, Match, DevType, vNicPg, VMDiskTotalGb $PortGroup = "" #vDisk Info if($dev.GetType().Name -eq "VirtualDisk"){ #Create a new VDisk XML Object $VDisk = (@($VMachine.VDisks.VDisk)[0]).Clone() #Populate the data into the XML object from the vCenter object $vm $VDisk.vDiskLabel = [String]$dev.DeviceInfo.Label #Just want the Datastore shortname $Temp.FileName = $dev.Backing.FileName $Temp.Match = $Temp.FileName -match '^\[.*\]' $Temp.DataStore = $matches[0] $VDisk.DataStore = [String]$Temp.DataStore #Calculate the Disk Size in GB $Temp.CapacityInGb = [int]($dev.CapacityInKB / 1048576) $VMachine.VMDiskTotalGb = [String]([int]$VMachine.VMDiskTotalGb + [int]$Temp.CapacityInGb) $VDisk.CapacityInGb = [String]$Temp.CapacityInGb #Append the VDisk to the VDisks $VMachine.VDisks.AppendChild($VDisk) > $null } #vNIC Info if(($dev.GetType().Name -eq "VirtualPCNet32") -or ($dev.GetType().Name -eq "VirtualVmxnet") -or ($dev.GetType().Name -eq "VirtualVmxnet2") -or ($dev.GetType().Name -eq "VirtualVmxnet3") -or ($dev.GetType().Name -eq "VirtualE1000")){ #Create a new VDisk XML Object $VNic = (@($VMachine.VNics.VNic)[0]).Clone() $VNic.VnicLabel = $dev.DeviceInfo.Label #Match the VNic to Portgroup and get the VlanId $PortGroup = @($PortGroups | Where-Object {$_.name -eq $dev.DeviceInfo.summary})[0] $VNic.VnicVlan = [String]$PortGroup.VlanId #Append the VNic to the VNics $VMachine.VNics.AppendChild($VNic) > $null } } #Clear blank devices $VMachine.VDisks.VDisk | Where-Object {$_.VDiskLabel -eq ""} | ForEach-Object {[Void]$VMachine.VDisks.RemoveChild($_)} $VMachine.VNics.VNic | Where-Object {$_.VNicLabel -eq ""} | ForEach-Object {[Void]$VMachine.VNics.RemoveChild($_)} #Add VM to VMachines array $XMLVMachines.VMachines.AppendChild($VMachine) > $null } #Clear blank VMs $VMachines.VMachines.VMachine | Where-Object {$_.VMName -eq ""} | ForEach-Object {[Void]$VMachines.VMachines.RemoveChild($_)} #Export to XML $XMLVMachines.Save($XMLOutput)