If you manage a VMware environment it might be helpful to know how to get ESXi server uptime. You may notice that there is a simple way to check uptime via the browser, but that only works on a handful of hosts.
What if you wanted to check 1000? 10,000? Doing this manually is not going to scale very well so today we’re going to share how to get ESXi Server uptime using Powershell and PowerCLI.
Table Of Contents
Requirements
In order to make this work, there are a couple of requirements needed so let’s list them out here.
- PowerCLI module
- Connection to vCenter or ESXi host
Get ESXi Server Uptime Using Powershell and PowerCLI
Ideally, if you want to get the ESXi uptime status for more than 1 host, you’ll want to connect to a vCenter server. However, if you’re only checking 1 host, this script will work just fine.
With that said, let’s get to the script.
Function Get-EsxiUptime { <# .SYNOPSIS This script will get the current uptime for VMware ESXi Hosts .NOTES Name: Get-EsxiUptime Author: theSysadminChannel Version: 1.0 DateCreated: 2021-Aug-21 .LINK https://thesysadminchannel.com/get-esxi-server-uptime-using-powershell-and-powercli/ - #> [CmdletBinding()] param( [Parameter( Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0 )] [Alias('Server', 'EsxiHost')] [string[]] $VMHost ) BEGIN { if (-not ($Global:DefaultVIServer)) { Write-Error "Unable to continue. Please connect to a vCenter Server." -ErrorAction Stop } } PROCESS { foreach ($EsxiServer in $VMHost) { try { $Server = Get-VMHost $EsxiServer -ErrorAction Stop if ($Server.PowerState -eq 'PoweredOn') { $LastBoot = $Server.ExtensionData.Summary.Runtime.BootTime.ToLocalTime() $Uptime = (Get-Date) - $LastBoot [PSCustomObject]@{ VMHost = $Server.Name LastBoot = $LastBoot UpTime = ([String]$Uptime.Days + " Days " + $Uptime.Hours + " Hours " + $Uptime.Minutes + " Minutes") ConnectionState = $Server.ConnectionState } } else { [PSCustomObject]@{ VMHost = $Server.Name LastBoot = 'Unknown' UpTime = 'Unknown' ConnectionState = $Server.ConnectionState } } } catch { Write-Error $_.Exception.Message } } } END {} }
Script Parameters
-VMHost
DataType: string/array
Alias: Server, EsxiHost
Description: Specify the ESXi server you would like to query.
Example 1 – Specify a list of servers separated by a comma
PS C:\> Get-EsxiUptime -VMHost esxi1.ad.thesysadminchannel.com, esxi2.ad.thesysadminchannel.com
Example 2 – Using the pipeline
PS C:\> Get-VMHost esxi* | Get-EsxiUptime
Conclusion
I suppose it would have helped if I had ESXi1 host powered on, but I thought it would be good to show what the expected output is for a machine that’s offline. Hopefully this script to get Esxi Uptime using Powershell and PowerCLI was helpful for you to see how quickly and easily you can scale this across multiple servers. I personally like how it receives pipeline input so you can use the native Get-VMHost command that you’re probably already familiar with.
If you liked this script, be sure to check out a similar script to Get Reboot History Using Powershell. It can definitely come in handy for Windows environments.