As an VMware admin always seen in vCenter most of VMs in shutdown state, wanted to know from how many days/months these shutdown vms are in shutdown state so we can delete these unused VMs which help in reclaiming storage space. If any one have script pls share it. (Note : shutdown vm from vcenter console )
Script can give the output of VM1 is shutdown from so and so date.
Thanks in Advance
ps 🙂
Unless you shut them down from vCenter you won't see an event for the power off. I guess you could analogously look for something like the memory usage state change, or parse through the VM log. I think to concretely get this you'd have to go through the OS log. Or maybe going forward make a note on the machine description about shutdown date.
Good luck!
You do have to parse the events. This should get you started on what you're looking for:
$vmlist = Get-VM | Where {$_.PowerState -eq 'PoweredOff'}
ForEach ($vm in $vmlist){
$vm | Get-VIEvent -Types Info | Where {$_.fullFormattedMessage -match "Powered Off"} | `
Sort-Object -property createdTime | select -last 1 | %{
Write-Host $_.vm.name $_.createdTime | Out-Default}
}
The above assumes you've already done connect-viserver etc.
But this will only give you the VMware events, they won't give you OS power operations, hence my comment about looking for analogous events.
From my testing - vcenter records a state change whether the shut down comes from console/RDP session or right-click within the VM client. Event retention could be a problem depending on how busy the environment is - say if weeks or months go by.
Afaik these are two different events, capture both.
Something like this.
You'll notice that a GuestOS shutdown generates both types of events.
So ultimately you should only look for the VmPoweredOffEvent
$vms = Get-VM | where{$_.PowerState -eq 'PoweredOff'}
Get-VIEvent -Entity $vms -Start (Get-Date).AddDays(-1) -MaxSamples ([int]::MaxValue) |
where{$_ -is [VMware.Vim.VmGuestShutdownEvent] -or $_ -is [VMware.Vim.VmPoweredOffEvent]} |
Select @{N='Date';E={$_.CreatedTime}},
@{N='VM';E={$_.Vm.Name}},
@{N='User';E={$_.UserName}},
@{N='EventType';E={$_.GetType().Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference