VM inventory function with Powershell on SCVMM 2012

Had a plan to do a blog post last week but was so busy studying for the VCP 5 test. I wrote the test on friday and i passed 🙂

I have created a little powershell function, this to get the inventory information about the virtual machines running in hosts or clusters that are connected to a System Center Virtual Machine Manager. This function collects number of configured vCPU´s, Memory, information about the connected virtual harddisks. This kind of information could be handy as in the case with one customer that has not yet implemented a host based backup solution and still use agents in their VM´s, so if they need to recover machines it is good to know what size and configuration it had if it is lost by some reason. This report can also be handy for some info on how much your datastores are filled. As you can se i both report the maximum size and the current (as dynamic disks are not allocating the whole size at creation time)

The function both export a csv and a HTML report

After importing it to excel it can look something like this:

And the html output is very simple but can be put on a internal web for IT production

I have used the Get-SCVirtualDisk to get the Bus Type and LUN id as this information is not available in Get-SCVirtualHarddisk, this information can be good if i lost the virtual machine configuration and want to connect the virtual harddisks to the right controller.

the function looks like this: (if you copy this you have to move the first # up to the <, the blog syntax highlighter does something and move it down :-( )

function Get-VMdata{
<#
.SYNOPSIS
Get the configuration data of the VMs in Hyper-V via SCVMM 2012
 
.DESCRIPTION
Use this function to get all VMs configuration in case of disaster or just statistics
 
.PARAMETER  xyz 
 
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-02-13
#>
param (
	$VMHostGroup = "All Hosts",
	[Parameter(ValueFromPipeline=$True)][Alias('ClusterName')]
	$VMHostCluster = $null,
	$VMHost = $null,
	[string]$CSVFile = "VMdata.csv",
    [string]$HTMLReport = "VMdata.html"
    )
	
	$report = @()
	if ($VMHostCluster -eq $null){
		$VMHosts = (Get-SCVMHostGroup -Name $VMhostGroup).AllChildHosts
	}else{
			$VMHosts = (Get-SCVMHostCluster -Name $VMHostCluster).Nodes
	}
	$VMs = $VMHosts | Get-SCVirtualMachine
	
	foreach ($VM in $VMs) {
		$VHDs = $VM | Get-SCVirtualDiskDrive
		$i = "1"
		foreach ($VHDconf in $VHDs){ 
			if($i -eq "1"){
				$data = New-Object PSObject -property @{
					VMName=$VM.Name
			        vCPUs=$VM.CPUCount
			        MemoryGB= $VM.Memory/1024
					VHDName = $VHDconf.VirtualHardDisk.Name
					VHDSize = $VHDconf.VirtualHardDisk.MaximumSize/1GB
					VHDCurrentSize = [Math]::Round($VHDconf.VirtualHardDisk.Size/1GB)
					VHDType = $VHDconf.VirtualHardDisk.VHDType
					VHDBusType = $VHDconf.BusType
					VHDBus = $VHDconf.Bus
					VHDLUN = $VHDconf.Lun
					VHDDatastore = $VHDconf.VirtualHardDisk.HostVolume
				}
				$i= "2"
			}else{
				$data = New-Object PSObject -property @{
					VMName=""
			        vCPUs=""
			        MemoryGB= ""
					VHDName = $VHDconf.VirtualHardDisk.Name
					VHDSize = $VHDconf.VirtualHardDisk.MaximumSize/1GB
					VHDCurrentSize = [Math]::Round($VHDconf.VirtualHardDisk.Size/1GB)
					VHDType = $VHDconf.VirtualHardDisk.VHDType
					VHDBusType = $VHDconf.BusType
					VHDBus = $VHDconf.Bus
					VHDLUN = $VHDconf.Lun
					VHDDatastore = $VHDconf.VirtualHardDisk.HostVolume
				}
			}
			$report +=$data	
		}
	}
	$report | Select-Object VMName,vCPUs,MemoryGB,VHDName,VHDSize,VHDCurrentSize,VHDType,VHDBusType,VHDBus,VHDLUN,VHDDatastore | Export-Csv -Path $CSVFile -NoTypeInformation -UseCulture
	$report | Select-Object VMName,vCPUs,MemoryGB,VHDName,VHDSize,VHDCurrentSize,VHDType,VHDBusType,VHDBus,VHDLUN,VHDDatastore | ConvertTo-HTML | Out-File $HTMLReport
}

and i can run it in some different ways, as you see in this screenshot, if i do not give any parameters it will use VMHostGroup “All Hosts” as default

Comments

Jess
Reply

Fantastic Script and massive time saver! Thanks for sharing. Jess

Chris
Reply

I tried running the script but it does not do anything except drop back to a prompt. Any suggestions? I am new to PowerShell

Niklas
Reply

you should find a vmdata.csv and a vmdata.html file where you ran the script with the info@Chris

salman
Reply

that’s script run but no .csv or .html file not create

Gary
Reply

Any update on this as I have exactly the same issus as salman.

Mark
Reply

I have the same problem, does this need to be added to the Get commandlet ?

Wayne
Reply

For those unable to run the script, make sure you’re running it from the SCVMM PowerShell prompt. That was my issue. Works perfectly after that!

Nismoau
Reply

Thanks! Massive time saver!

zoher
Reply

i tried above script but it doesnt work with scvmm 2012 sp1, any updated script is required?

Niklas
Reply

I will check and get back to you.. it was a while ago I used that one

Lars
Reply

@Niklas
great script – any update on SP1 support?

Cheers

CHris
Reply

How do we add info on VLAN tags associated to VM’s NIc ?

ad5
Reply

Any developments on a script with SP1 support?

Manu
Reply

Any info if this will run on SCVMM 2012 R2

Kent
Reply

I just tried this script from my 2012R2 installation, and it completed.
However it took some time before the files were populated with data.
So run the script and give it time to complete.

Kent
Reply

Just an update, so i used this again today and for some reason it woudnt work. i tested all the commands and they worked, so i decided it must have been the creation of the files. once i changed the following
[string]$CSVFile = “c:\VMdata.csv”
[string]$HTMLReport = “c:\VMdata.html”

It worked again

roy
Reply

Just another point to mention. If you use [PSCustomObject] @{…} instead of New-Object PSObject -property @{…}, the data is already ordered. This saves you having to use Select-Object later on. Other way is to do what you did, but with [ordered] in front of the @.

Bernard
Reply

Can I use to generate certain VM starting with say DB – # into the inventory and the memory allocation is dynamic or static

Chris
Reply

Just wondering if anyone got this to work with SCVMM 2012 R2 SP1? I’ve tried a few changes to the script as recommended in other comments, but so far the script just returns to the prompt immediately with no output files created and no errors printed to the SCVMM PowerShell window, so no info to troubleshoot with.

Kevil
Reply

Thank you for this, worked like a charm on our SCVMM 2012 R2

Michael
Reply

HI,
Can the script be automated to run? Script works fine when I run it manually from ISE.

Leave a comment

name*

email* (not published)

website