1

Closed

Add Get-VMSummaryInformation

description

I wrote the following to get a few additional statistics (I also posted it on MSDN documentation). It could use some cleanup but may be helpful for someone. Strange how the API keeps the same information (number of cpus, uptime, etc.) in a bunch of different places....
 
function Get-VirtualSystemManagementService
{
return gwmi -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization"
}
 
function Get-VirtualMachine([string] $Name="")
{
$query = "select * from Msvm_ComputerSystem where ElementName='" + $Name + "'"
return gwmi -namespace "root\virtualization" -query $query
}
 
function Get-VirtualMachineSettingData([string] $Name="")
{
$virtualMachine = Get-VirtualMachine($Name)
$query = "ASSOCIATORS OF {" + $virtualMachine.__Path + "} WHERE resultClass = Msvm_VirtualSystemsettingData"
return gwmi -query $query -namespace "root\virtualization"
}
 
function Get-VirtualMachineSummaryInformation([string] $Name="")
{
$requestedProperties = 0,1,2,3,4,5,6,7,100,101,102,103,104,105,106,107,108,109
$service = Get-VirtualSystemManagementService
$settings = Get-VirtualMachineSettingData($Name)
$summaryList = $service.GetSummaryInformation($settings.__PATH, $requestedProperties)
foreach($summary in $summaryList.SummaryInformation)
{
    return $summary
}
}
 
Usage:
 
$results = Get-VirtualMachineSummaryInformation("WinXPx32")
 
Displaying Output:
 
"Name: " + $results.Name
"ElementName: " + $results.ElementName
"MemoryUsage: " + $results.MemoryUsage
"GuestOperatingSystem: " + $results.GuestOperatingSystem
"HealthState: " + $results.HealthState
"Heartbeat: " + $results.Heartbeat
"Notes: " + $results.Notes
"NumberOfProcessors: " + $results.NumberOfProcessors
"ProcessorLoad: " + $results.ProcessorLoad
"UpTime: " + $results.UpTime
 
Sample Output:
 
Name: B0C19E9D-C09F-46FD-B548-D8249032A485
ElementName: WinXPx32
MemoryUsage: 2048
GuestOperatingSystem: Microsoft Windows XP
HealthState: 5
Heartbeat: 2
Notes:
NumberOfProcessors: 1
ProcessorLoad: 0
UpTime: 5865112

file attachments

Closed Jan 31, 2009 at 12:49 AM by jamesone

comments

jamesone wrote Aug 26, 2008 at 11:50 PM

I will add the code to the next release. I've reworked it somewhat and it now looks like this .
Filter Get-VirtualMachineSummaryInformation
{param ($vm , $Server=".")
if ($vm -eq $null) {$vssd=Get-VMSettingData $_ $Server}
else {$vssd=Get-VMSettingData $vm $server}
$settingPath = @()
$vssd | foreach-object {$settingPath += $_._Path ; $Server=$.__Server}
$arguments=@($SettingPath, @(0,1,2,3,4,100,101,102,103,104,105,106,107,108), $null)
$VSMgtSvc=Get-WmiObject -computerName $Server -NameSpace "root\virtualization" -Class "MsVM_virtualSystemManagementService"
$result=$VSMgtSvc.psbase.InvokeMethod("GetSummaryInformation",$arguments)
if ($Result -eq 0) {$arguments[2] | foreach-object {
    $SiObj = New-Object -TypeName System.Object
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "Name"             -Value $_.name
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "ElementName"      -Value $_.elementname
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "CreationTime"     -Value $_.CreationTime
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "EnabledState"     -Value $_.EnabledState
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "EnabledStateText" -Value (convert-vmState($_.EnabledState))
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "Notes"            -Value $_.Notes
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "CPUCount"         -Value $_.NumberOfProcessors
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "CPULoad"          -Value $_.ProcessorLoad
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "CPULoadHistory"   -Value $_.ProcessorLoadHistory
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "MemoryUsage"      -Value $_.MemoryUsage
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "Heartbeat"        -Value $_.Heartbeat
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "HeartbeatText"    -Value @{2="OK"; 6="Error"; 12="No Contact";13="Lost Communication"}[[int]$_.Heartbeat]
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "Uptime"       -Value $_.UpTime
Add-Member -inputObject $siObj -MemberType ScriptProperty -Name "UptimeFormatted"  -Value {if ($This.uptime -gt 0) {([datetime]0).addmilliseconds($This.UpTime).tostring("hh:mm:ss")} else {0} }
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "GuestOS"          -Value $_.GuestOperatingSystem;
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "Snapshots"        -Value $_.Snapshots.count
Add-Member -inputObject $siObj -MemberType NoteProperty   -Name "Jobs"             -Value $_.AsynchronousTasks 
$siObj } }
else {"Get Summary info :" + $ReturnCode[[string]$result] | Out-host }
}

12055 wrote Aug 27, 2008 at 9:42 PM

Cool, I was wondering what a good work around would be for the fact that the results were not a proper object.

jamesone wrote Jan 31, 2009 at 12:49 AM

This has become Get-VMSummary in V1.00a