PowerCLI

 View Only
Expand all | Collapse all
  • 1.  VM uptime?

    Posted May 04, 2011 08:55 PM

    Hi guys, is there a way to get uptime info for all VMs in a cluster?

    Basically a list with VM name, and uptime.

    Thanks!



  • 2.  RE: VM uptime?

    Posted May 05, 2011 12:33 AM

    There are several ways you can do this.

    1) With the Get-Stat cmdlet

    $clusterName = "MyCluster"
    $vms = Get-Cluster -Name $clusterName | Get-VM
    Get-Stat
    -Start (Get-Date) -Entity $vms -Stat sys.uptime.latest -MaxSamples 1 | `
    Select
    @{N="VM";E={$_.Entity.Name}},
        @{N
    ="Uptime";E={New-TimeSpan -Seconds $_.Value}}

    2) Using the BootTime property

    $clusterName = "MyCluster" 
    $now
    = Get-Date
    Get-Cluster -Name $clusterName | Get-VM | where {$_.PowerState -eq "PoweredOn"} |`
    Select
    @{N="VM";E={$_.Name}},     @{N="Uptime";E={New-TimeSpan -Start $_.Extensiondata.Runtime.BootTime.ToLocalTime() -End $now}}

    Unfortunately the BootTime property doesn't always seem to be populated.

    I prefer the Get-Stat method.

    Note that the Uptime is displayed in the default TimeSpan format: days.hours:minutes:seconds.ticks



  • 3.  RE: VM uptime?

    Posted May 05, 2011 09:05 AM

    Hi LucD, thanks for the info.

    When I run the Get-Stat method i get this:

    Unexpected token 'vms' in expression or statement.
    At :line:2 char:27
    + $clusterName = "ClusterA" $vms  <<<< = Get-Cluster -Name $clusterName | Get-VM

    Am I missing something?

    vSphere 4.1 and PowerCLI 4.1.1



  • 4.  RE: VM uptime?

    Posted May 05, 2011 09:09 AM

    There was a problem with <CR><LF> when I copied the lines.

    It's corrected, please try again.



  • 5.  RE: VM uptime?

    Posted May 05, 2011 09:14 AM

    I tried this other code you provided in another thread:

    http://communities.vmware.com/message/1304574


    Get-Cluster <cluster-name> | Get-VM | `
      where {$_.PowerState -eq "PoweredOn"} | `
      Get-Stat -Stat sys.uptime.latest -MaxSamples 1 -Realtime | `
      select Entity, 
             @{Name="Boottime";
              Expression={(Get-Date).AddSeconds(- $_.value).ToString("yy/MM/dd HH:mm:ss")}}

    It gets the job done, any way to get the info to an Excel sheet?

    Thanks!



  • 6.  RE: VM uptime?
    Best Answer

    Posted May 05, 2011 09:21 AM

    Sure, like this

    Get-Cluster <cluster-name> | Get-VM | `
      where {$_.PowerState -eq "PoweredOn"} | `
      Get-Stat -Stat sys.uptime.latest -MaxSamples 1 -Realtime | `
      select Entity,
             @{Name="Boottime";
              Expression={(Get-Date).AddSeconds(- $_.value).ToString("yy/MM/dd HH:mm:ss")}} | `
        Export-Csv "C:\vm-bootime.csv" -NoTypeInformation -UseCulture


  • 7.  RE: VM uptime?

    Posted May 05, 2011 09:25 AM

    Awesome! thanks.



  • 8.  RE: VM uptime?

    Posted May 05, 2011 09:22 AM

    I now get:

    The term ' ' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At :line:5 char:83
    + Get-Cluster -Name $clusterName | Get-VM | where {$_.PowerState -eq "PoweredOn"} |`  <<<<



  • 9.  RE: VM uptime?

    Posted Oct 28, 2021 02:14 PM

    Hello Dears

    i have the same question, but this script only show UP time on VM layer, if the VM rebooted from OS layer is not detected, please is there any way to show uptime even if it is rebooted on OS layer?



  • 10.  RE: VM uptime?

    Posted Oct 28, 2021 02:17 PM

    You will have to do that query inside the Guest OS.



  • 11.  RE: VM uptime?

    Posted Oct 28, 2021 02:18 PM

    Thanks a lot, but i am not OS administrator, i am managing only VMware layers and i was searching a lot to find away but till now no luck..



  • 12.  RE: VM uptime?

    Posted Oct 28, 2021 02:24 PM

    You can eventually look at the VMGuestRebootEvent and VMGuestShutdownEvent, that might give you some idea of the Guest OS uptime.
    But in my experience there are too many special cases where this will not work.



  • 13.  RE: VM uptime?

    Posted Jan 07, 2022 10:04 PM

    use something like ControlUp



  • 14.  RE: VM uptime?

    Posted Jan 07, 2022 10:30 PM

    Really?
    Did you read the VMTN Community Code of Conduct, especially section Spam and shameless commerce
    Since you are working for that company, at least some disclosure would be appreciated



  • 15.  RE: VM uptime?

    Posted Dec 13, 2012 02:57 PM

    hi

    is it possible to have this uptime on this year on % ?

    tks advance



  • 16.  RE: VM uptime?

    Posted Dec 13, 2012 03:45 PM

    it's like this script

    $esx = Get-VMHost 192.168.1.33

    Get-VIEvent -Entity $esx -MaxSamples 99999 -Start (Get-Date).AddDays(-350) | `
    where {"HostConnectionLostEvent","HostConnectedEvent" -contains $_.GetType().Name} | `
    Sort-Object -Property {$_.CreatedTime.DateTime} -Unique | `
    Select @{N="Hostname";E={$_.Host.Name}},
           @{N="Time";E={$_.CreatedTime.ToShortDateString() + " " + $_.CreatedTime.ToShortTimeString()}},
           @{N="Status";E={if($_.GetType().Name -eq "HostConnectedEvent"){"Connected"}else{"Not connected"}}} | Export-Csv c:\Temp\statsexportruntime.csv

    but not for esx but vm and not all date like that but in % of down date :smileydevil:

    :smileyhappy:



  • 17.  RE: VM uptime?

    Posted Dec 13, 2012 04:48 PM

    With the events you will only be using poweron and poweroff events to determine the uptime.

    Is that what you want to measure for uptime ?



  • 18.  RE: VM uptime?

    Posted Dec 14, 2012 11:05 AM

    with that i can only count downtime of esx server. since yesterday i try to calculate in % of time the downtime of esx but i don't found how.



  • 19.  RE: VM uptime?

    Posted Jan 21, 2013 09:47 AM

    Hi Luc

    function Get-VMUptime {
    <#
    .SYNOPSIS  Calculate the VM uptime percentage
    .DESCRIPTION The function will calculate the uptime
        percentage for a VM for a given period of time
    .NOTES  Author:  Luc Dekens
    .PARAMETER VM
        One or more virtual machines. This parameter accepts
        pipeline input.
    .PARAMETER Start
        Start of the interval over which the uptime percentage
        shall be calculated. THe default is 7 days ago.
    .PARAMETER Finish
        End of the interval. The default is 'now'.
    .EXAMPLE
        PS> Get-VMUptime -VM MyVM
    .EXAMPLE
       PS> Get-VM VM | Get-VMUptime -Start $start
    #>
      param(
        [CmdletBinding()]
        [Parameter(
          Position=0,
          Mandatory=$true,
          ValueFromPipeline=$true,
          ValueFromPipelineByPropertyName=$true
        )]
        [PSObject[]]$VM,
        [Datetime]$Start = ((Get-Date).AddDays(-30)),
        [Datetime]$Finish = (Get-Date)
      )
      process {
        $extraStart = $Start.AddDays(-1)
        Get-Stat -Entity $VM -Stat "sys.uptime.latest" -Start $extraStart -Finish $Finish -ErrorAction SilentlyContinue |
        Group-Object -Property {$_.Entity.Name} | %{
          if($_.Group){
            $totalUptime = 0
            $intervalDuration = $_.Group[0].IntervalSecs
            $numberOfSamples = $_.Count - (86400 / $intervalDuration)
            $startInterval = $_.Group[$numberOfSamples - 1].Timestamp - (New-TimeSpan -Seconds $intervalDuration)
            $uptime = New-TimeSpan -Seconds $_.Group[0].Value
            if(($_.Group[0].Timestamp - $uptime) -le $startInterval){
              $totalUptime = $numberOfSamples * $intervalDuration
            }
            else{
              $i = [math]::Floor($_.Group[0].Value/$intervalDuration)
              $totalUptime = $_.Group[0].Value
              $i++
              while($i -lt $numberOfSamples){
                if(0,1 -notcontains $_.Group[$i].Value){
                  $j = $i + [math]::Floor($_.Group[$i].Value/$intervalDuration) + 1
                  if($j -le $numberOfSamples){
                    $totalUptime += $_.Group[$i].Value
                    $i = $j++
                  }
                  else{
                    $partialIntervalValue = $_.Group[$i].Value - $_.Group[$i + 1].Value
                    $completeIntervals = $numberOfSamples - $i - 1
                    $fullIntervalsValue = $completeIntervals * $intervalDuration
                    $totalUptime += ($fullIntervalsValue + $partialIntervalValue)
                    $i = $j
                  }
                }
              }
            }
            New-Object PSObject -Property @{
              VM = $_.Name
              Uptime = [math]::Round(($totalUptime / ($numberOfSamples * $intervalDuration) * 100),2)
              Unit = "percent"
              Start = $startInterval
              Finish = $_.Group[0].Timestamp
            }
          }
          else{
            New-Object PSObject -Property @{
              VM = $_.Name
              Uptime = "no data"
              Unit = ""
              Start = $Start
              Finish = $Finish
            }
          }
        }
      }
    }

    I try with your date script to export this value to csv file... but i have nothing on my csv....blank. do you know why ?

    tks advance



  • 20.  RE: VM uptime?

    Posted Jan 21, 2013 10:21 AM

    Sorry i found How

    $yearStart = Get-Date -Day 1 -Month 12 -Year 2012 -Hour 0 -Minute 0 -Second 0
    $months = 11
    $vms = Get-VM VM*
    &{0..($months-1) | %{
      $start = $yearStart.AddMonths($_)
      $finish = $start.AddMonths(1).AddSeconds(-1)
       Get-VMUptime -VM $vms -Start $start -Finish $finish
    }} |
    Export-Csv C:\scriptvm\uptime\uptime.csv -NoTypeInformation -UseCulture
    on luc website.;)