jessem
Enthusiast
Enthusiast

List of ESXi hosts and uptime (days)

Ok, I know it's possible but need some help on the follow-thru.

I am trying to get a list of hosts in a vcenter with the following columns.

datacenter |  cluster | hosts name | uptime in Days

All I have so far is:

Get-VMHost | Get-View | select @{N="Uptime"; E={(Get-Date) - $_.Summary.Runtime.BootTime}}


but that just gives me it in boot time when I need just the number of days.


Also, I need to get this in a .csv.  This is what I am coming up with but something is way off here....I am so new to this still and can't put the pieces together.  Any help is appreciated.


Get-Cluster |

Select Name,

  @{N="Datacenter";E={Get-Datacenter -Cluster $_ | Select -ExpandProperty Name}},

  @{N="Hosts";E={$_.ExtensionData.Host.Count}},

  @{N="Uptime"; E={$_.ExtensionData.Summary.Runtime.BootTime $end).days}},

    Select -ExpandProperty Sum

  }} | Export-Csv c:\report2.csv -NoTypeInformation -UseCulture

10 Replies
LucD
Leadership
Leadership

Not sure what you want to do here.

The BootTime property is available on ESXi hosts, not on clusters.

In any case, if you want to have the number of days for a VMHost, you could do.

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jessem
Enthusiast
Enthusiast

So this script work, but when trying to output to a csv file, nothing happens.  This is what I am using...

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

Export-Csv c:\uptime.csv -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership

Looks like you might have forgotten the pipe-symbol ('|') at the end of the Select cmdlet.

Get-VMHost | Select Name,
 
@{N="Uptime"; E={New-Timespan -Start $_.ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}} |
Export-Csv c:\uptime.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jessem
Enthusiast
Enthusiast

Great, yes, I forgot.  And where can I insert the "sort name" for the hosts?

0 Kudos
LucD
Leadership
Leadership

In fact anywhere between the Get-VMHost and the Export-Csv.

Get-VMHost | 
Sort-Object -Property Name |
Select Name,
 
@{N="Uptime"; E={New-Timespan -Start $_.ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}} |
Export-Csv c:\uptime.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

esxi1979
Expert
Expert

How to add 1 more col. ie date when we run the uptime, that will be helpful for records...

0 Kudos
LittleNickey
Enthusiast
Enthusiast

Not sure if I understand you, but do you mean the date when the script was run? In that case you will see that on the file when it was created, but if you really want to add it as a column you can add "get-date". This will of course show the same date on every line.

Get-VMHost |
Sort-Object -Property Name |
Select Name,
 
@{N="Uptime"; E={New-Timespan -Start $_.ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}},

  @{N="Date when script was run"; E={Get-Date}} |
Export-Csv c:\uptime.csv -NoTypeInformation -UseCulture

Take a look at the "-Format" parameter for Get-Date if you want to customize the output of get-date.

-- Oskar
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

Hello LucD,

Is possible to add user who reboot/shutdown host in this script?

Thanks

0 Kudos
LucD
Leadership
Leadership

There is a HostShutdownEvent, but afaik there is nothing similar for start of an ESXi node.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
trodiere_ait
Contributor
Contributor

Hi Jessem, I know it's long time since you've asked but I use the following function (paste it in your PowerShell window first before executing your code.

function getTimeSpanFormatted($timespan)

{

  $timeTakenStr=""

  if ($timespan.Days -gt 0)

  {

    $timeTakenStr += "$($timespan.days) days "

  }

  if ($timespan.Hours -gt 0)

  {

    $timeTakenStr += "$($timespan.Hours) hrs "

  }

  if ($timespan.Minutes -gt 0)

  {

    $timeTakenStr += "$($timespan.Minutes) min "

  }

  if ($timespan.Seconds -gt 0)

  {

    $timeTakenStr += "$($timespan.Seconds) sec "

  }

  return $timeTakenStr

}

# pass a UTCTime source and it will convert to the locale (UTC+Locale Windows Timezone)

function Convert-UTCtoLocal([parameter(Mandatory=$true)][String]$UTCTime)

{

  try {

    $TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone);

    $LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ);

    return $LocalTime;

  }

  catch {

    return $null;

  }

}


Here is an example:

Note that UCT is directly time read from the Host, where as the 'Locale' is applying the local windows pc's Timezone information.

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

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

  @{N="Last Boot (Locale)";E={Convert-UTCtoLocal -UTCTime $_.ExtensionData.Sumary.Runtime.BootTime}},

  @{N="Uptime (Locale)";E={getTimeSpanFormatted((Get-Date) - (Convert-UTCtoLocal -UTCTime $_ExtensionData.Summary.Runtime.BootTime))}}

here is an example output:

Name                    Last Boot (UTC)        Last Boot (Locale)     Uptime (Locale)

----                    ---------------        ------------------     ---------------

server1.mydomain2.local 22/09/2018 11:18:03 AM 22/09/2018 9:18:03 PM  169 days 17 hrs 21 min 11 sec

server2.mydomain2.local 15/09/2018 2:49:27 PM  16/09/2018 12:49:27 AM 176 days 13 hrs 49 min 47 sec

server3.mydomain2.local 26/09/2018 12:35:57 PM 26/09/2018 10:35:57 PM 165 days 16 hrs 3 min 17 sec

server4.mydomain2.local 22/09/2018 1:34:27 PM  22/09/2018 11:34:27 PM 169 days 15 hrs 4 min 48 sec

0 Kudos