PowerShell: Hyper-V VM Inventory


Having a clean inventory of all virtual machines is a good practice to avoid virtual machine sprawl. I created a script to send me to the Monthly VM Inventory automatically, this includes the Guest name, status, OS, vCPU, RAM, host and notes.

The results look like this:

JC-VM-01
STATUS: Running
VCPU: 1
RAM: 2048 M
OS: Windows Server 2008 R2 Enterprise
HOST: JC-HOST-01
NOTES: Server owner: Jackie Chen

Here is the code:

cls
cd “C:\Scripts”
$hosts = Get-Content Hyperv_Hosts.txt
$date = Get-Date
$n = 0
$i = 0
$j = 0
$k = 0
$l = 0

$preface = “Any statements can be added here.
$data1 = $null
$data2 = $null
$data3 = $null
$data4 = $null

Foreach ($hostname in $hosts)
{
    $service = gwmi -class “Msvm_VirtualSystemManagementService” -namespace “root\virtualization” -ComputerName $hostname
    $VM = gwmi -ComputerName $hostName -namespace “root\virtualization” -query “Select * from Msvm_ComputerSystem where Caption=’Virtual Machine'”

    Foreach ($_ in $VM)
    {
       $settings =  gwmi -ComputerName $hostname -namespace “root\virtualization” -query (“ASSOCIATORS OF {” + $_.__Path + “} WHERE resultClass = Msvm_VirtualSystemsettingData”)
          $VM_EnableState = $service.GetSummaryInformation($settings.__PATH, 100)
       $VM_Notes = $service.GetSummaryInformation($settings.__PATH, 3)
       $VM_GuestOS = $service.GetSummaryInformation($settings.__PATH, 106)
       $VM_VCPU = $service.GetSummaryInformation($settings.__PATH, 4)
       $VM_RAM = gwmi -ComputerName $hostname -namespace “root\virtualization” -query (“select * from Msvm_MemorySettingData where InstanceID Like ‘%” + $_.Name + “%'”)
       If ($VM_EnableState.SummaryInformation[0].EnabledState -eq “2”)
               {
              $status = “Running”
              $i = $i + 1
              $data1 = $data1 + “`n`n” + $_.ElementName + “`n” + “STATUS: ” + $status + “`n” + “VCPU: ” + $VM_VCPU.SummaryInformation[0].NumberOfProcessors + “`n” + “RAM: ” + $VM_RAM.reservation + ” M” + “`n” + “OS: ” + $VM_GuestOS.SummaryInformation[0].GuestOperatingSystem + “`n” + “HOST: ” + $hostname + “`n” + “NOTES: ” + $VM_Notes.SummaryInformation[0].Notes
            }
       If ($VM_EnableState.SummaryInformation[0].EnabledState -eq “3”)
               {
              $status = “Stopped”
              $j = $j + 1
              $data2 = $data2 + “`n`n” + $_.ElementName + “`n” + “STATUS: ” + $status + “`n” + “VCPU: ” + $VM_VCPU.SummaryInformation[0].NumberOfProcessors + “`n” + “RAM: ” + $VM_RAM.reservation + ” M” + “`n” + “OS: ” + $VM_GuestOS.SummaryInformation[0].GuestOperatingSystem + “`n” + “HOST: ” + $hostname + “`n” + “NOTES: ” + $VM_Notes.SummaryInformation[0].Notes
            }
       If ($VM_EnableState.SummaryInformation[0].EnabledState -eq “3276”)
               {
              $status = “Saved”
              $k = $k + 1
              $data3 = $data3 + “`n`n” + $_.ElementName + “`n” + “STATUS: ” + $status + “`n” + “VCPU: ” + $VM_VCPU.SummaryInformation[0].NumberOfProcessors + “`n” + “RAM: ” + $VM_RAM.reservation + ” M” + “`n” + “OS: ” + $VM_GuestOS.SummaryInformation[0].GuestOperatingSystem + “`n” + “HOST: ” + $hostname + “`n” + “NOTES: ” + $VM_Notes.SummaryInformation[0].Notes
            }
       If ($VM_EnableState.SummaryInformation[0].EnabledState -eq “32769”)
               {
              $status = “Suspended”
              $l = $l + 1
              $data4 = $data4 + “`n`n” + $_.ElementName + “`n” + “STATUS: ” + $status + “`n” + “VCPU: ” + $VM_VCPU.SummaryInformation[0].NumberOfProcessors + “`n” + “RAM: ” + $VM_RAM.reservation + ” M” + “`n” + “OS: ” + $VM_GuestOS.SummaryInformation[0].GuestOperatingSystem + “`n” + “HOST: ” + $hostname + “`n” + “NOTES: ” + $VM_Notes.SummaryInformation[0].Notes
            }
       $n = $n + 1
    }
}

$body = “$preface” + “`n`n” + “Till Now (” + “$date” +”),” + ” The Total Hyper-V VM numbers are ” + $n + “.” + “`n” + $i + ” are running, ” + “`n” + $j + ” are stopped, ” + “`n” + $k + ” are saved, ” + “`n” + $l + ” are suspended.” + “`n`n” + “**********Running VM**********” + “$data1” + “`n`n” + “**********Stopped VM**********” + “$data2” + “`n`n” + “**********Saved VM**************” + “$data3” + “`n`n” + “**********Suspended VM**********” + “$data4”

#Write-host $body
Send-MailMessage -To jackie.chen@test.com -From Hyper-V_Monitor@test.com -Subject “Monthly Virtual Machine Inventory” -SmtpServer smtp.test.com -Body $body

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s