Get The List of All VMs in Virtual Machine Manager and Their Virtual Disks #VMM #SCVMM #HyperV #PowerShell

4 min read

This post was edited/updated on June 2, 2016.

(The update includes a new version of the PowerShell VMM Reporting Script that populate the percentage of the free disk space for all Virtual Machines, the script has been published to TechNet, you can find the link at the bottom of this post).

In today’s blog post I will show you how to get the list of all Virtual Machines in System Center Virtual Machine Manager including their virtual disks.

If you select any host managed by VMM and you look into one of the VM hardware configuration, you will see a similar figure as below:

Get-SCVMVirtualDisk-01

The most concern hardware configuration is the virtual hard disk of the VM such as type (dynamic, fix or differencing), Maximum disk size, the current disk space used, and the storage location.

The challenging question is, what if you have a large VMM deployment, and you want to track all virtual machines and enumerate their virtual disks?

Unfortunately SCVMM on it’s own doesn’t have any reporting capability to accomplish this task Sad smile

Luckily enough PowerShell will come to the rescue once again Smile

I have created the following tool that will help you to get the list of all VMs in VMM server Or all VMs in a particular VMM Host Group and enumerate all their virtual drives, and then send you a nice formatted email report Winking smile

<#
//-----------------------------------------------------------------------

//     Copyright (c) {https://charbelnemnom.com}. All rights reserved.

//-----------------------------------------------------------------------
 .SYNOPSIS
Get the list of all Virtual Machines in SCVMM including their disks.

.DESCRIPTION
Get the list of all Virtual Machines in Virtual Machine Manager and enumerate all their drives.

.NOTES
File Name : Get-SCVMVirtualDisk.ps1
Author    : Charbel Nemnom
Version   : 3.0
Date      : 02-June-2016
Requires  : PowerShell Version 3.0 or above
OS        : Windows Server 2012 or 2012 R2
Product   : System Center Virtual Machine Manager 2012 R2

.LINK
To provide feedback or for further assistance please visit:
https://charbelnemnom.com

.EXAMPLE
./Get-SCVMVirtualDisk -VMMServerName <VMMServerName>
This example will get all Virtual Machines and their Virtual Disks from VMM <VMMServerName>, then send email report.
.EXAMPLE
./Get-SCVMVirtualDisk -VMMServerName <VMMServerName> -HostGroupName <HostGroupName>
This example will get all Virtual Machines and their Virtual Disks from a particular VMM Host Group <HostGroupName>, then send email report.
#>

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true,HelpMessage='VMM Server Name')]
    [Alias('VMMServer')]
    [String]$VMMServerName,
      
    [Parameter(HelpMessage='VMM Host Group Name')]
    [Alias('GroupName')]
    [String]$HostGroupName   
  )

# Variables
$filedate   = get-date
$FromEmail  = "[email protected]"
$ToEmail1   = "[email protected]"
$tableColor = "WhiteSmoke"

# Establish Connection to SMTP server
$smtpServer = "smtp.mail.net"
$smtpCreds  = new-object Net.NetworkCredential("username", "password")
$smtp       = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.UseDefaultCredentials = $false
$smtp.Credentials = $smtpCreds

# HTML Style Definition
$report = "<!DOCTYPE html  PUBLIC`"-//W3C//DTD XHTML 1.0 Strict//EN`"  `"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd`">"
$report = "<html xmlns=`"http://www.w3.org/1999/xhtml`"><body>"
$report = "<style>"
$report = $report + "TABLE{border-width:2px;border-style: solid;border-color: #C0C0C0 ;border-collapse: collapse;width: 100%}"
$report = $report + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: #C0C0C0 ;text-align: left}"
$report = $report + "TD{border-width: 2px;padding: 0px;border-style: solid;border-color: #C0C0C0 ;text-align: left}"
$report = $report + "TD{border-width: 2px;padding: 0px;border-style: solid;border-color: #C0C0C0 ;text-align: left}"
$report = $report + "H1{font-family:Calibri;}"
$report = $report + "H2{font-family:Calibri;}"
$report = $report + "Body{font-family:Calibri;}"
$report = $report + "</style>"
$report += "<center><p style=""font-size:12px;color:#BDBDBD"">Get-SCVMVirtualDisk - ScriptVersion: 3.0 | Created By: Charbel Nemnom - CDM MVP | Feedback: https://charbelnemnom.com</p></center>"

# Report Header
$report = $report + "<h1>Virtual Machines and Virtual Hard Disks Report on: $($VMMServerName)</h1>"

If ($HostGroupName)
{
    # Report Title
    $report = $report + "<h2>Data for VMM Host Group: $($HostGroupName) : $($filedate)</h2>"
    $hostGroups = Get-SCVMHostGroup -Name $HostGroupName
    Foreach ($hostGroup in $hostGroups.AllChildHosts)
        {
        $SCVMs  = Get-SCVirtualMachine -VMMServer $VMMServerName -VMHost $HostGroup | select Name
        }
}
Else
    {
    $SCVMs=Get-SCVirtualMachine -VMMServer $VMMServerName | select Name
    }

Write-Verbose "Generating Report"
Foreach ($SCVM in $SCVMs)
{
Write-Verbose "Checking VM: $SCVM Virtual Hard Disks"
$report = $report + "<style>TH{background-color:Indigo}TR{background-color:$($tableColor)}</style>"
$report = $report + (Get-SCVirtualMachine $SCVM.Name | Select @{Label="Host Name";Expression={$_.VMHost}},@{Label="VM Name";Expression={$_.Name}},@{Label="Computer Name";Expression={$_.ComputerName}},@{Label="VM Generation";Expression={$_.Generation}} | ConvertTo-HTML -as Table -Fragment)
$report = $report + "<style>TH{background-color:Blue}TR{background-color:$($tableColor)}</style>"
$report = $report + (Get-SCVirtualMachine $SCVM.Name | Get-SCVirtualDiskDrive | Select @{Label="VHD Name";Expression={$_.VirtualHardDisk}},@{Label="Controller Type";Expression={$_.BusType}},Lun | ConvertTo-HTML -as Table -Fragment)
$report = $report + "<style>TH{background-color:DarkGreen}TR{background-color:$($tableColor)}</style>"
$report = $report + (Get-SCVirtualMachine $SCVM.Name | Get-SCVirtualHardDisk | Select @{Label="VHD Type";Expression={$_.VHDType}},@{Label="VHD Location";Expression={$_.Location}}, `
@{Label="Max Disk Size (GB)";Expression={($_.MaximumSize/1GB)}},@{Label="Disk Space Used (GB)";Expression={"{0:N2}" -f ($_.Size/1GB)}}, `
@{Label="Disk Space Used (%)";Expression={[math]::Round((($_.Size/1GB)/($_.MaximumSize/1GB))*100)}}, ` 
@{Label="Free Disk Space (GB)";Expression={"{0:N2}" -f (($_.MaximumSize/1GB) - ($_.Size/1GB))}} | ConvertTo-HTML -as Table -Fragment) + " <br>"
 
$report = $report + " <br>"
}

# Finalizing Report
Write-Verbose "Finalizing Report"
$report = $report + "</body></html>"

# Send Email
Write-Verbose "Sending e-mail"
$email = new-object Net.Mail.MailMessage
$email.Priority = [System.Net.Mail.MailPriority]::High
$email.Subject = "$VMMServerName - Virtual Machines and Virtual Hard Disks Report: $($filedate)"
$email.From = new-object Net.Mail.MailAddress($FromEmail)
$email.IsBodyHtml = $true
$email.Body =  $report
$email.To.Add($ToEmail1)
$smtp.Send($email)

Get-SCVMVirtualDisk-02

And here you go the report in your inbox.

Get-SCVMVirtualDisk02

A couple of areas that could definitely be improved though, would have to check the disk space used versus the maximum disk size, and then highlight the virtual disk that reaching its limits, etc…

You can download a copy of this tool from TechNet here.

This is version 3.0, do you want additional features? please feel free to leave a comment below.

Enjoy your day!

Cheers,
-Charbel

About Charbel Nemnom 579 Articles
Charbel Nemnom is a Cloud Architect, Swiss Certified ICT Security Expert, Microsoft Most Valuable Professional (MVP), and Microsoft Certified Trainer (MCT), totally fan of the latest's IT platform solutions, accomplished hands-on technical professional with over 17 years of broad IT Infrastructure experience serving on and guiding technical teams to optimize the performance of mission-critical enterprise systems. Excellent communicator is adept at identifying business needs and bridging the gap between functional groups and technology to foster targeted and innovative IT project development. Well respected by peers through demonstrating passion for technology and performance improvement. Extensive practical knowledge of complex systems builds, network design, business continuity, and cloud security.

4 Comments

  1. Howdy Charbel,

    Nice blog and great scripts – how do I download/copy these scripts – or do you just put em up for reference?

    Cheers
    e

  2. I am getting this error.

    PS C:\CloudPack\Scripts> ./Get-SCVMVirtualDisk -VMMServerName scvmm01.cbagroupcloud.com
    At C:\CloudPack\Scripts\Get-SCVMVirtualDisk.ps1:91 char:301
    + … $_.Size/1GB)}},,@{Label=”Free Disk Space (GB)”;Expression={“{0:N2}” -f (($_.Maxi …
    + ~
    Missing expression after ‘,’ in pipeline element.
    At C:\CloudPack\Scripts\Get-SCVMVirtualDisk.ps1:91 char:301
    + … $_.Size/1GB)}},,@{Label=”Free Disk Space (GB)”;Expression={“{0:N2}” -f (($_.Maxi …
    + ~
    Missing argument in parameter list.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingExpression

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.