VM Uptime Script

This is a script to show the uptime of a VM in the Virtual estate. I have put a few different variations of this Script to meet the needs I had.

The script below has is the original script I found on the following site: http://vmware-scripts.blogspot.co.uk/2012/08/script-to-get-uptime-of-each-vm-in.html

$LastBootProp = @{
  Name = ‘LastBootTime’
    Expression = {
      ( Get-Date ) – ( New-TimeSpan -Seconds $_.Summary.QuickStats.UptimeSeconds )
}
}
Get-View -ViewType VirtualMachine -Property Name, Summary.QuickStats.UptimeSeconds | Select Name, $LastBootProp

I then thought I would need to be able to easily range the amount of hours I wanted to show since the last reboot (uptime). This script shows the VMs and their last boot time within the last X amount of hours. NOTE: THE X IS AN INTEGER IN-PUTTED BY THE USER UNDER THE SCRIPTS INSTRUCTIONS.

Write-Host “This Script Displays the VM Names and their Last Boot Times within the last X hours”
Write-Host “For example for VM’s boot time within the last 10 days you should enter 240”
[int]$UserTime = Read-Host “Enter the Number of Hours”
$Hours = $userTime * 3600

$LastBootProp = @{
Name = ‘LastBootTime’
Expression = {
( Get-Date ) – ( New-TimeSpan -Seconds $_.Summary.QuickStats.UptimeSeconds )
}
}
Get-View -ViewType VirtualMachine -Property Name, Summary.QuickStats.UptimeSeconds | Where-Object {$_.Summary.QuickStats.UptimeSeconds -Le $Hours} | Select Name, $LastBootProp | Sort $LastBootProp.Name -Descending

The script basically gets the amount of hours the user inputs and multiplies by 3600 which turns the hours into seconds. Takes that Seconds value away from the current Date/Time. It then gets the VM Name and Last Boot Time and sorts the Last Boot Time.

Below is a very similar script but the other way round. This Script Displays the VM Names and their Last Boot Times greater than X hours. NOTE: THE X IS AN INTEGER IN-PUTTED BY THE USER UNDER THE SCRIPTS INSTRUCTIONS.

Write-Host “This Script Displays the VM Names and their Last Boot Times greater than X hours”
Write-Host “For example for VM’s boot time older than the last 10 days you should enter 240”
[int]$UserTime = Read-Host “Enter the Number of Hours”
$Hours = $userTime * 3600

$LastBootProp = @{
Name = ‘LastBootTime’
Expression = {
( Get-Date ) – ( New-TimeSpan -Seconds $_.Summary.QuickStats.UptimeSeconds )
}
}
Get-View -ViewType VirtualMachine -Property Name, Summary.QuickStats.UptimeSeconds | Where-Object {$_.Summary.QuickStats.UptimeSeconds -Ge $Hours} | Select Name, $LastBootProp | Sort $LastBootProp.Name -Descending

If you wish to keep these data saved in a file I would recommend adding this line to the bottom fo the script:

Export-CSV C:\VMUptime.csv -NoTypeInformation

Please remember the easiest way to make these scripts work are to copy and paste into notepad and to save them as .PS1 files. You can then run these from within your powershell window (Ensure your connected to the vCenter server)

 

 

Leave a Reply

Your email address will not be published.

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