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
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}
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.
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))}}
Here is what I came up with
$smtpName = "SMTPSERVER"
$vcenters = @("VCENTER1","VCENTER2")
$subj = "VM 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>"
$msgbody = "";
Connect-VIServer $vcenter -Credential $cred
foreach ($vcenter in $vcenters) {
Connect-VIServer $vcenter -Credential $cred
$stat = 'sys.osuptime.latest'
$now = Get-Date
$vms = Get-VM | Where-Object {$_.powerstate -eq 'PoweredOn' -and $_.Uid.Substring($_.Uid.IndexOf('@')+1).Split(":")[0] -eq $vcenter}
$msgbody_local = 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))}} | ConvertTo-HTML -Fragment | Out-String
$msgbody = $msgbody + "<h5>" + "vCenter: " +$vcenter+"</h5>"+$msgbody_local
}
$msgbody = "<html><head>"+$head+"</head><body>"+$msgbody+"</body></html>"
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