4 Replies Latest reply on Sep 12, 2019 7:01 PM by nettech1

    PowerCLI wrong uptime is returned for sys.uptime  ?

    nettech1 Hot Shot

      When I run Get-Stat -Entity VM01-Stat sys.uptime.latest -Realtime -MaxSamples 1 I get 1738728 seconds which translates to 20 days. The server (Windows 2012R2) VM01 has been rebooted less than 4 hours ago.

       

      Please correct me if I am wrong but it looks like sys.uptime.latest returns the VM uptime and not the OS uptime. If that's the case is there a way to get the OS uptime instead of the VM uptime?

       

      Thanks

        • 1. Re: PowerCLI wrong uptime is returned for sys.uptime  ?
          jterli Enthusiast
          VMware Employees

          The powercli command Get-Stat provides the VM level details and not the guest related events.

          As far as i know, reading the GOS events is a way to get this data.

          You can refer to a sample script : Find Last Used time of VMs in a VCenter - Samples - VMware {code}

          1 person found this helpful
          • 2. Re: PowerCLI wrong uptime is returned for sys.uptime  ?
            nettech1 Hot Shot

            jterli,

             

            Thanks for your post, i am not sure if the link you provided is exactly what I was looking for, but it certainly helped me get on the right path. I will test the examples I found in the next few days, thanks again.

             

            https://code.vmware.com/forums/2530/vsphere-powercli#593211

            • 3. Re: PowerCLI wrong uptime is returned for sys.uptime  ?
              nettech1 Hot Shot

              This is exactly what I was looking for.

               

              Last OS Boot & Up time is returned for all VMs in the cluster. Needs some tweaking, but this is a very good starting point!

               

              Connect-VIServer prism01

              $stat = 'sys.osuptime.latest'

              $now = Get-Date

              $vms = Get-VM

               

              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))}}

              • 4. Re: PowerCLI wrong uptime is returned for sys.uptime  ?
                nettech1 Hot Shot

                Here is what I came up with

                 

                $smtpName = "SMTPSERVER"

                $vcenter =  "VCENTER"

                $subj = "Server Uptime Report"

                $rcptFrom = "FROM@EMAIL.COM"

                $rcptTo = "TO@EMAIL.COM"

                $uID = "VCENTERUSERNAME"

                 

                 

                $pass = Get-Content "C:\Scripts\topsecret.txt" | ConvertTo-SecureString

                $cred = New-Object System.Management.Automation.PsCredential($uID,$pass)

                 

                 

                 

                 

                $head = "<style>"

                $head = $head + "BODY{background-color:white; font-size:10pt;font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif} "

                $head = $head + "table {margin-top:3px;border-collapse: collapse;width: 100%;} "

                $head = $head + "table td, table th {border: 1px solid #ddd;text-align: left; padding: 7px; valign:top; font-size:10pt; height:10px; line-height:normal} "

                $head = $head + "table tr:nth-child(even){background-color: #f2f2f2} "

                $head = $head + "table tr:hover {background-color: #ddd;} "

                $head = $head + "table th {padding-top: 3px;padding-bottom: 3px;background-color: #AB0634;color: white;} "

                $head = $head + "table tr {line-height: 3px;} "

                $head = $head + "</style>"

                 

                 

                 

                Connect-VIServer $vcenter -Credential $cred

                 

                 

                 

                $stat = 'sys.osuptime.latest'

                $now = Get-Date

                $vms = Get-VM | Where-Object {$_.powerstate -eq 'PoweredOn'}

                $msgbody = Get-Stat -Entity $vms -Stat $stat -Realtime -MaxSamples 1 | Select @{N='VM Name';E={$_.Entity.Name}},@{N='Last OS Boot Time';E={$now.AddSeconds(- $_.Value)}},@{N='Uptime in Days';E={[math]::Floor($_.Value/(24*60*60))}} | Sort-Object -Property @{Expression = "Uptime in Days"; Descending = $True} | ConvertTo-HTML -Head $head | Out-String

                 

                 

                 

                 

                send-MailMessage -To $rcptTo -from $rcptFrom -Subject $subj -BodyAsHtml $msgbody -SmtpServer $smtpName

                 

                 

                 

                 

                 

                To encrypt your vCenter password use this  (Get-Credential).Password | ConvertFrom-SecureString | Out-File "C:\Scripts\topsecret.txt" and enter your password