Generate system uptime report of VMs using PowerCLI

It is possible to return the system uptime of your VMs by retrieving the last statistical information for the metric ‘sys.uptime.latest’ in realtime using PowerCLI. For Example, to return the metric for the virtual machine ‘VM1’ we could run the following command:

Get-Stat -Entity VM1 -Stat sys.uptime.latest -Realtime -MaxSamples 1

This will return the uptime value in seconds, we can convert this using the New-Timespan cmdlet to convert the value, by using the value property returned below and invoking the cmdlet agaisnt this value.

$Uptime = Get-Stat-Entity $VM.Name -Stat sys.uptime.latest -Realtime -MaxSamples 1
$Timespan = New-Timespan- Seconds $Uptime.Value

For Example, if the value returned was ‘4235900’, invoking the New-TimeSpan value would return the following:

Days : 49
Hours : 0
Minutes : 38
Seconds : 20
Milliseconds : 0
Ticks : 42359000000000
TotalDays : 49.0266203703704
TotalHours : 1176.63888888889
TotalMinutes : 70598.3333333333
TotalSeconds : 4235900
TotalMilliseconds : 4235900000

So based on the above we could provide a more detailed and understandable duration for the up time of the VM, For Example, if I wanted to include Days, Minutes and Seconds I could run the following :

Get-Stat -Entity VM1 -Stat sys.uptime.latest -Realtime -MaxSamples 1
$Timespan = New-Timespan -Seconds $Uptime.Value
"" + $Timespan.Days + " Days, " + $Timespan.Hours + " Hours, " + $Timespan.Minutes + " Minutes"

This would return the following output, based on the new timespan value returned.

49 Days, 0 Hours, 38 Minutes

We can also include the Powered On time of the VM as well using the Get-VIEvent by filtering the ‘FullFormattedMessage’ for the string ‘powered on’ and returning the created time for the last event.  By default, the Get-VIEvent, will return only 100 events (‘MaxSamples’), depending how long the VM has been powered on for the filter may not return the event to which we are looking for.

An option could be to set the MaxSamplesSize to the maximum value, one disadvantage of this is that it can depending on your environment take a while to return the value required.

From the above we can build a collection of VMs and run the script block agaisnt each VM in the collection and output to a file.

Connect-VIServer server1.domain.local 

$VMs = Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"}
$Output = ForEach ($VM in $VMs)

    { 
    "" | Select @{N="Name";E={$VM.Name}},
    @{N="Powered On";E={$Event = Get-VM $VM.Name | Get-VIEvent -MaxSamples [int]::MaxValue | Where-Object {$_.FullFormattedMessage -like "*powered on*"} | Select-First 1 
    $Event.CreatedTime}},
    @{N="Up Time";E={$Timespan = New-Timespan -Seconds (Get-Stat -Entity $VM.Name -Stat sys.uptime.latest -Realtime -MaxSamples 1).Value
    "" + $Timespan.Days + " Days, "+ $Timespan.Hours + " Hours, " +$Timespan.Minutes + " Minutes"}}
    } 
    
$Output | Export-Csv -Path "D:\Output\VMUptimeReport.csv "-NoTypeInformation
Advertisements

One thought on “Generate system uptime report of VMs using PowerCLI

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 )

Google+ photo

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

Connecting to %s