Hello,
I am seeking a solution, preferably through the use of PowerCLI, to generate a comprehensive list of all virtual machines (VMs) along with their cumulative uptime since their creation. By cumulative uptime, I refer to the entire duration of time for which the VM was powered on, as opposed to its current uptime.
$Folders = Get-Folder -Name "Folder1", "Folder2"
ForEach ($Folder in $Folders) {
$VMs = Get-VM -Location $Folder
$VMs | Select Name, PowerState, @{N="Uptime";E={($UptimeEvents = Get-VIEvent -Entity $_ -Start (Get-Date).AddYears(-1) -MaxSamples ([int]::MaxValue) -EventType "VmPoweredOnEvent" | Sort-Object CreatedTime -Descending) | ForEach-Object {if ($_.FullFormattedMessage -match "duration:\s+(\d+):(\d+):(\d+)") {($Matches[1] * 3600) + ($Matches[2] * 60) + $Matches[3]} else {0}}; $UptimeEvents | Measure-Object -Sum | Select-Object -ExpandProperty Sum/3600}} | Sort-Object Uptime -Descending
}Nevertheless, the 'Uptime' column appears to be empty, despite attempting slightly altered versions of the aforementioned script.
Thanks,
I don't know that guy personally but he is quite good at scripting. I assume, he will be able to assist. @LucD
In my logical mind - how do you want to calculate total uptime when the system anyway will be reset and each time when VM is powered off or restarted?
I don't know if it's possible.
The purpose of collecting this data is to determine an estimated cost for migrating the mentioned virtual machines (VMs) to the cloud, based on their usage patterns. As many of these VMs do not need to run continuously, determining their average runtime would be useful in estimating the potential costs associated with their migration.
Thread moved to the PowerCLI Community
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Afaik, the Get-VIEvent does not have an EventType parameter.
My Get-VIEventPlus function does have an EventType parameter, perhaps you mean that function?
But I'm a bit confused with the FullFormattedMessage you are using in your script.
I'm not aware that the VMPoweredOnEvent has a 'duration' message in the FullFormattedMessage property.
Could you perhaps share such an event?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
