Pages

Monday, July 17, 2017

Get uptime of ESX hosts from one or more Virtual Center




Get uptime of ESXi hosts from one or more Virtual Center

Here is a small script which can get number of days for which ESXi hosts are up in your Virtual Center.

########################################################################################
##The script is used for getting ESX Hosts uptime information from Virtual Center(S). ##
##It gets Uptime information of ESX hosts from Virtual Center and shows it on console ##
##It also exports the uptime and overall status of ESXi hosts in CSV.                 ##  
########################################################################################

##Set initial Variables for use in the Script.
$Path = "C:\TEMP"
$DateF = Get-Date -Format yyyy-MM-dd-HH-mm
$LoginUsr = "UserName"
$LoginPWD = "Password"

#Get Today's Date calculation to find out number of days the Host has been up.
$Today = Get-Date

##List of Virtual Center Servers to get detials from.
$VirtualCenters = "VC1.domain.com", "vc2.domain.com"

##Loop through each Virtual center defined in above array of VCs.
$VirtualCenters | foreach {
    $VCName = $_
    Connect-VIServer $VCName -User $LoginUsr -Password $LoginPWD -AllLinked:$True

    $Result = Get-VMHost |select Name, @{N="Uptime"; E={(New-TimeSpan $_.ExtensionData.Summary.Runtime.BootTime $Today).Days}},`
     @{N="OverAllStatus"; E={$_.extensionData.OverAllStatus}},`
     @{N="VCName"; E={$VCName}}
   $Result | Format-Table -AutoSize
   $Result | Export-Csv -NoTypeInformation -UseCulture $Path\ESXHost-Uptime-$Datef.csv
  }

##End of Code