Basic inventory of HyperV virtual machines using PowerShell

Here at Basefarm we operate at a large scale with thousands of servers running for our customers. Quite often a customers asks for a list of machines with various properties for each machine.

Most of the time the customer want this information in an simple format so that they can use it internally. In this blogpost I will show how you can get the information about memory, CPU count etc for a set of Hyper-V machines from Virtual Machine Manager via PowerShell.

First off, start a PowerShell command line and load the PowerShell Snap-In for Virtual Machine Manager.

Add-PSSnapin -Name Microsoft.SystemCenter.VirtualMachineManager

Now we can work with the commands made available to us by the Snap-In, if you want to find all the commands that are available issue:

Get-Command -Module Microsoft.SystemCenter.VirtualMachineManager

So let’s begin by loading information about all our machines from the VMM host into a variable named $VMs

$VMs = Get-VM -VMMServer hyperv-vmm01.sth.basefarm.net

What the above command does is to load all of the VMs on the host HYPER-V-01.mydomain.com into the variable $VMs. This means we will only do one call to the server which avoid generating unnecessary load.

Now let’s check how many machines we have:

$VMs.Count

And now that we know we have machines to query, let’s find out what attributes exists (things we can get into our output)

$VMs | Get-Member -MemberType Property

For example, to find all macines that are powered off:

$VMs | where { $_.Status -eq 'PowerOff' } | select VMHost, name , Memory, CPUCount , Status

The above example adds some complexity to the command, but it is to filter so we only see machines that have the status is ‘PowerOff’.

Now let’s get what we wanted from the beginning, a list of machines for a specific customer. The list should include name of the VM host, VM name, memory, number of CPUs and current status.

$VMs | where { $_.Name -Match 'CUST*' } | select VMHost, name , Memory, CPUCount , Status

This will list all machines who’s name begins with ‘CUST’. So we now have found what we wanted!

But instead of copying & pasting this we want to write the result to a CSV file so we can send that to the customer. Let’s make that easier by getting the output of the above command into a variable named $result

$result = $VMs | where { $_.Name -Match 'LFO*' } | select VMHost, name , Memory, CPUCount , Status

Now our ‘report’ is stored in the $result variable and we can use standard PowerShell to export it to a CSV file:

$result | Export-Csv -NoTypeInformation -Delimiter ';' .\report.csv

Now our report is available in a CSV file on the file ‘report.csv’ (in the current directory)

A very basic way of getting your Hyper-V inventory out!

About Joakim Westin

Curious, autodidact, living among ones and zeros.
This entry was posted in Data center and tagged , , . Bookmark the permalink.

4 Responses to Basic inventory of HyperV virtual machines using PowerShell

  1. here is my powershell script to detailed inventory vhost and vm’s on it

    http://stephanco.blogspot.com/2013/05/detailed-report-of-your-hyper-v-server.html

    works great for datacenter 2012 stand alone servers

  2. I created a new project called pshvm.codeplex.com and it is the first all powershell script free hyper-v manager that includes a replacement for vmconnect and mstsc. It is meant to be run from the console. I included my reporting.codeplex.com project so now you can manage and report for free.

  3. Hi there.

    Excellent guide for using Powershell. One of the best I have ever seen.

    I have been juggeling around adjusting this to my needs. How can I get the script to show me, the maximum size of the diskdrives on each VM-guest. Or just som diskrelated infos at all.

    When i use this:
    $result = $VMs | where { $_.Name -Match ‘SRV*’ } | select Name , DynamicMemoryMaximumMB , CPUCount , VirtualHardDisks, Status

    Iget this
    “Name”;”Memory”;”CPUCount”;”VirtualSCSIAdapters”;”Status”
    “SRV00OMGW02VM”;”512″;”1″;”Microsoft.SystemCenter.VirtualMachineManager.VirtualSCSIAdapter[]”;”Running”

    And when i try this. (With [] after VirtualHardDisk))
    $result = $VMs | where { $_.Name -Match ‘SRV*’ } | select Name , DynamicMemoryMaximumMB , CPUCount , VirtualHardDisks[], Status

    I get this:
    “Name”;”DynamicMemoryMaximumMB”;”MemoryAssignedMB”;”Memory”;”CPUCount”;”VirtualDiskDrives[c:]”;”Status”
    “SRV00OMGW02VM”;;”512″;”512″;”1″;;”Running”

    So I cannot figure out how to get information regardring disks. Strange.

    Would appreciate, if you could help. That would make this script absolute fantastic. (And save my day)

    Regards,
    Thomas

  4. MJ Almassud says:

    Thank you sir.

    MJ