10 Replies Latest reply on Mar 10, 2019 9:45 PM by trodiere_ait

    List of ESXi hosts and uptime (days)

    jessem Enthusiast

      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

        • 1. Re: List of ESXi hosts and uptime (days)
          LucD Guru
          vExpertUser ModeratorsCommunity Warriors

          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: List of ESXi hosts and uptime (days)
            jessem 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

            • 3. Re: List of ESXi hosts and uptime (days)
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: List of ESXi hosts and uptime (days)
                jessem Enthusiast

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

                • 5. Re: List of ESXi hosts and uptime (days)
                  LucD Guru
                  vExpertCommunity WarriorsUser Moderators

                  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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: List of ESXi hosts and uptime (days)
                    esxi1979 Hot Shot

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

                    • 7. Re: List of ESXi hosts and uptime (days)
                      LittleNickey 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.

                      • 8. Re: List of ESXi hosts and uptime (days)
                        antoniogemelli Enthusiast

                        Hello LucD,

                         

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

                         

                        Thanks

                        • 9. Re: List of ESXi hosts and uptime (days)
                          LucD Guru
                          vExpertUser ModeratorsCommunity Warriors

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

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: List of ESXi hosts and uptime (days)
                            trodiere_ait Lurker

                            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