How to Check VM OS Uptime and ESXi Hosts Uptime Using PowerCLI – VirtuallyThatGuy

Another quick blog post about checking OS Uptime for VMs and ESXi Hosts using powercli

## Get VM OS Uptime Using Powercli

$stat​​ =​​ 'sys.osuptime.latest'

$now​​ =​​ Get-Date

$vms​​ =​​ Get-cluster​​ *​​ |​​ Get-VM​​ "*LAB-*" |?{$_.name –notlike “*esxi*”}

Get-Stat​​ -Entity​​ $vms​​ -Stat​​ $stat​​ -Realtime​​ -MaxSamples​​ 1​​ |​​ 

Select​​ @{N='VM';E={$_.Entity.Name}}, ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ 

@{N='LastOSBoot';E={$now.AddSeconds(-​​ $_.Value)}},​​ 

@{N='UptimeDays';E={[math]::Floor($_.Value/(24*60*60))}}​​ |​​ ft​​ -a ​​​​ 

 

## Get VMHost Uptime Using Powecli

Get-VMHost​​ |​​ Select​​ Name, ​​​​ 

@{N="Last Boot (UTC)";E={$_.ExtensionData.Summary.Runtime.BootTime}},

@{N="Uptime​​ Days"; E={New-Timespan​​ -Start​​ $_.ExtensionData.Summary.Runtime.BootTime​​ -End​​ (Get-Date)​​ |​​ Select​​ -ExpandProperty​​ Days}}​​ |​​ ft​​ -a ​​​​ 

 

## With Last Boot Time and Local Time - Uptime​​ 

Get-Vmhost ​​​​ |​​ sort-object​​ Name​​ |​​ Select-Object​​ Name,

 ​​​​ @{N="Last Boot (UTC)";E={$_.ExtensionData.Summary.Runtime.BootTime}},

 ​​​​ @{N='LocalTime';E={[datetime]::SpecifyKind($_.Date,'Utc').ToLocalTime()}},

 ​​​​ @{N="Uptime Days"; E={New-Timespan​​ -Start​​ $_.ExtensionData.Summary.Runtime.BootTime​​ -End​​ (Get-Date)​​ |​​ Select​​ -ExpandProperty​​ Days}}​​ |​​ ft​​ -a ​​​​ 

 

​​ 

## Get VM OS Uptime Using Powercli
$stat = 'sys.osuptime.latest'
$now = Get-Date
$vms = Get-cluster * | Get-VM "*LAB-*" |?{$_.name –notlike “*esxi*”}
Get-Stat -Entity $vms -Stat $stat -Realtime -MaxSamples 1 | 
Select @{N='VM';E={$_.Entity.Name}},                                                           
@{N='LastOSBoot';E={$now.AddSeconds(- $_.Value)}}, 
@{N='UptimeDays';E={[math]::Floor($_.Value/(24*60*60))}} | ft -a  

## Get VMHost Uptime Using Powecli
Get-VMHost | Select Name,  
@{N="Last Boot (UTC)";E={$_.ExtensionData.Summary.Runtime.BootTime}},
@{N="Uptime Days"; E={New-Timespan -Start $_.ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}} | ft -a  

## With Last Boot Time and Local Time - Uptime 
Get-Vmhost  | sort-object Name | Select-Object Name,
  @{N="Last Boot (UTC)";E={$_.ExtensionData.Summary.Runtime.BootTime}},
  @{N='LocalTime';E={[datetime]::SpecifyKind($_.Date,'Utc').ToLocalTime()}},
  @{N="Uptime Days"; E={New-Timespan -Start $_.ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}} | ft -a  

You May Also Like

About the Author: VirtuallyThatGuy

Leave a Reply

Your email address will not be published. Required fields are marked *