VMware Cloud Community
rahulkamboj
Contributor
Contributor

How to check VM poweroff/shutdown date ?

Can anybody help me , I am trying see the VM poweroff/shutdown date but i can see only 30 days report through the below script . I have a around 196 VMs which is powered off and i want to see the power off date . Below script is working but only showing the last 30 days report. Is anybody in this group who can help me , how i can check these VMs date.

Connect-VIServer -Server XXX-vcenter1 -User administrator@vsphere.local -Password XXXXXXXXXXXX

$Report = @()

$VMs = get-vm |Where-object {$_.powerstate -eq "poweredoff"}

$Datastores = Get-Datastore | select Name, Id

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

where {$_ -is [VMware.Vim.VmPoweredOffEvent]} |

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

$lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1

$vm = Get-VM -Name $_.Name

$row = '' | select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PowerOFF

$row.VMName = $vm.Name

$row.Powerstate = $vm.Powerstate

  $row.OS = $vm.Guest.OSFullName

$row.Host = $vm.VMHost.name

$row.Cluster = $vm.VMHost.Parent.Name

$row.Datastore = ($Datastores | where {$_.ID -match (($vm.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name

    $row.NumCPU = $vm.NumCPU

$row.MemMb = $vm.MemoryMB

$row.DiskGb = ((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)

$row.PowerOFF = $lastPO.CreatedTime

  $report += $row

}

$report | Sort Name | Export-Csv -Path "C:\XXXXX\Powered_Off_VMs.csv" -NoTypeInformation -UseCulture

disconnect-viserver * -confirm:$false

0 Kudos
1 Reply
Vijay2027
Expert
Expert

I think it's issue with how retention policy of tasks and events are set.

By default, in vcenter 6 and later it's set to 30 days. 

0 Kudos