I am looking for a way to report on VM's created in the last 7 days which have not been put into a folder. I can do a quick get-VM | Select Name, Folder which gives me all VMs and the folder names but I want to filter this to previous 7 days and only VMs which are not in a folder.
Try something like this
$start = (Get-Date).AddDays(-7)
Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) |
where{$_ -is [VMware.Vim.VmCreatedEvent] -and -not $_.Template} |
ForEach-Object -Process {
$event = $_
Get-View -Id $event.vm.vm -Property Name,Parent -ErrorAction SilentlyContinue |
where{(Get-View -Id $_.Parent -Property Name).Name -eq 'vm'} |
Select Name,
@{N='Created';E={$event.CreatedTime}},
@{N='User';E={$event.UserName}}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD for the suggestion when i try this i get the following error
Get-VIEvent : 13/09/2018 14:33:48 Get-VIEvent Error in deserializing body of reply message for operation 'ReadPreviousEvents'.
At line:7 char:1
+ Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) |
That's a known bug, upgrade to the latest 10.2.0.
In that version the bug is fixed.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference