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(-7)),
[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 copy this from your website. I m connect to my vcenter on powercli