VMware Cloud Community
Hazy2k18
Contributor
Contributor

Simple weekly Script

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.

0 Kudos
4 Replies
LucD
Leadership
Leadership

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

0 Kudos
Hazy2k18
Contributor
Contributor

Thanks for the quick reply i will give this a go
0 Kudos
Hazy2k18
Contributor
Contributor

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) |

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos