concept: we have a policy that we hold decommissioned VM for 30 days. so I created a resource pool called decommissioned and gave it no resource. I then put all the machine that are to be removed in there. so I wrote a some powercli to remove them after 30 days. but I am stumped on getting the VM name for the remove-vm statement from the VIevent information. I can see the name in the fullformattedmessage, but it has other information with it.
# set my resource pool
$rpName = 'Decommissioned'
# Set my Aging
$PoweredOffAge = (Get-Date).AddDays(-30)
$Output = @{}
#f find my VM that are in the resource pool and are currently powered off
$PoweredOffvms = Get-ResourcePool -Name $rpName | Get-VM | where {$_.PowerState -eq "PoweredOff"}
# run the power off vm against the VIevent date and see how old they are
$EventsLog = Get-VIEvent -Entity $PoweredOffvms -Finish $PoweredOffAge -MaxSamples ([int]::MaxValue) | where{$_.FullFormattedMessage -like "*is powered off"}
# Delete VM that meet the criteria of 30 days old and powered off and in the resource pool decommissioned
Remove-VM ??? -DeletePermanently -WhatIf
any help would be greatly appreciated.
thanks in Advance
Scott
You're capturing the VmPoweredOffEvent, the VM displayname is in the $event.VM.Name.
Something like this
Get-VIEvent -Entity $PoweredOffvms -Finish $PoweredOffAge -MaxSamples ([int]::MaxValue) |
where{$_ -is [VMware.Vim.VmPoweredOffEvent]} | %{
# Delete VM that meet the criteria of 30 days old and powered off and in the resource pool decommissioned
Remove-VM $_.VM.Name -DeletePermanently -WhatIf
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Lucd,
You are always so Helpful. Thank you.