hi ,
I am trying to get the VM (Guest uptime) up Time', @{Name='UPTime';Expression={$_.ExtensionData.Summary.QuickStats.UptimeSeconds}},
then converting it to
$seconds = $UPTimeInSeconds
# Calculate the number of days, hours, and minutes
$days = [Math]::Floor($seconds / 86400)
$seconds = $seconds % 86400
$hours = [Math]::Floor($seconds / 3600)
$seconds = $seconds % 3600
$minutes = [Math]::Floor($seconds / 60)
$seconds = $seconds % 60
$UPTime = "$days days: $hours hours: $minutes minutes: $seconds seconds"
but it is still wrong, any idea ??
thank you
You can use the New-TimeSpan cmdlet for that
$boot = $vm.ExtensionData.Summary.QuickStats.UptimeSeconds
$now = Get-Date
$span = New-TimeSpan -Start $now.AddSeconds(- $boot) -End $now
$days = $span.Days
$hours = $span.Hours
$minutes = $span.Minutes
$seconds = $span.Seconds
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
the issue is the returned number of seconds does not match the box uptime! any idea why would that happen?
What is the difference you are seeing?
Take into account you are getting UTC times
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The property "UpTimeSeconds" that you're using gets updated only when the VM is powered off and then powered ON again, but not when it is rebooted. Reboot doesn't initiate the whole PowerOff/On cycle.
You can achieve what you're looking for by querying the Guest OS for that info, for example: with the Invoke-VMScript command and then executing commands to the VMs in order to get their boot time. Note: vmware tools need to be running in order to use the command + you need to have access to the guest OS.
You can pass something like this to the -ScriptText parameter
For Windows Machines use: (gcim Win32_OperatingSystem).LastBootUpTime
For Linux machines use: uptime
I don't think part of that remark is correct.
The description of the QuickStats property clearly states
"A set of statistics that are typically updated with near real-time regularity."
So yes, a Guest OS reboot will not reset the value, but a VM restart will.
Not only a VM power off and power on.
In fact, better values are obtained by using the sys.uptime.latest metric or the VMGuestReboot event.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
From the Action UI - Only Shutdown Guest OS, or Power Off VM will reset the UptimeSeconds property. So, Restart Guest OS, and Reset VM will not. Sometimes the description that VMware writes is not accurate, though, it needs to be tested by our selves.
The stat sys.uptime.latest is not updated, I did the action mentioned above and it didn't update it.
Both options above: are tested on vSphere 8.0.1. I don't know with the older/newer versions.
I agree, that the VMGuestReboot event would work if that information is retained to be searched when a VM has booted few weeks ago.
This is exactly what I said, i.e. VM restart, which is noit a VM reset afaik.
From the Action UI - Only Shutdown Guest OS, or Power Off VM will reset the UptimeSeconds property. So, Restart Guest OS, and Reset VM will not. Sometimes the description that VMware writes is not accurate, though, it needs to be tested by our selves.
The sys.uptime.latest works perfectly for a VM that is powered on (in vSphere 6.*, 7.* and 8.*).
It is updated each Realtime interval (20 seconds).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I guess the safest option is to calculate the poweron date time, and subtracted it for $TimeNow. would that covet the users sending restart command to the vm externally, or using the vSphere client , or even request the restarts from the within the OS it selef
As you might have deduced from the recent exchange, a reboot from within the Guest OS will not be covered.
The VM stays powered on during that action.
To capture those, you will have to query inside the Guest OS.
A power off/power on from the VM is covered, although some seem to question that feature.
A restart of the VM, from the Web CLient level, is captured afaik.
In conclusion, the best way is to query the Guest OS.
That should give you the actual Guest OS uptime.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Your script looks almost correct. Ensure that you properly assign the "UPTimeInSeconds" variable and use the correct syntax. The modified code provided should work for calculating VM uptime.