VMware Cloud Community
Myriam2016
Contributor
Contributor

Report - Powered Off VMs Over xx Days

Hello,

Does any one has a query  or power cli script which will generate a report of powered off VMs for xx days.

Thanks

0 Kudos
1 Reply
LucD
Leadership
Leadership

You could try something like this

$NumberOfDays = 7

$now = Get-Date

$vmOff = Get-Vm | where{$_.PowerState -eq 'PoweredOff'} | Select -ExpandProperty Name

Get-VIEvent -MaxSamples ([int]::MaxValue) |

where{$_ -is [VMware.Vim.VmPoweredOffEvent] -and $vmOff -contains $_.Vm.Name} |

Group-Object -Property {$_.Vm.Name} |

select Name,

    @{N='Daysoff';E={[int]($now - ($_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1 -ExpandProperty CreatedTime)).TotalDays}} |

where{$_.Daysoff -ge $NumberOfDays}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos