PowerCLI: One-liners last 10 VMs created and removed

I was asked if it was easy enough to get a list of the last 10 VMs created in a virtual Infrastructure, the answer is yes, its a one-liner !

To list the last 10 VMs created, cloned or imported use the following:

Get-VIEvent -maxsamples 10000 |where {$_.Gettype().Name-eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmBeingClonedEvent" -or $_.Gettype().Name-eq "VmBeingDeployedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage -First 10

 

Or if you would like to get the last 5 VMs removed from your VI use the following:

Get-VIEvent -maxsamples 10000 | where {$_.Gettype().Name -eq "VmRemovedEvent"} | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage -First 19

As requested, here is how you can get a list of the VM’s created over the last 14 days:

Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(14) | where {$_.Gettype().Name-eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmBeingClonedEvent" -or $_.Gettype().Name-eq "VmBeingDeployedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage

And a list of the VMs removed over the last 14 days:

Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(14) |where {$_.Gettype().Name-eq "VmRemovedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage

26 thoughts on “PowerCLI: One-liners last 10 VMs created and removed

  1. Pingback: VMware Cloud on AWS – VM Creation Date available in vSphere API

  2. pushpendra singh

    Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(–14) |where {$_.Gettype().Name-eq “VmRemovedEvent”} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage

    No output for this script..any suggestion

  3. Sunil Rawat

    Add the below line in script to take out the output

    | Export-Csv -path “c:\reports\vminventory.csv”

  4. Sunil Rawat

    Put the below path in script to take the output

    | Export-Csv -path “c:\reports\vminventory.csv”

  5. Edward

    Wondering if someone wouldn’t mind helping me finish this powerCLI task? the Command works as intended but I’m really looking for additional output. like cluster name and host name, and just single column with VMname within? any help to a newbie is appreciated

    Get-Cluster “Cluster-A”,”Cluster-B”,”Cluster-C” |
    Get-VM |
    Get-VIEvent -Username User -types Error -Start 07/11/2017 -Finish (get-date) |
    where {$_.Message -match “NVRAM: write failed”}|
    select CreatedTime,FullFormattedMessage |

  6. Jerry

    I really like script
    Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(–14) | where {$_.Gettype().Name-eq “VmCreatedEvent” -or $_.Gettype().Name-eq “VmBeingClonedEvent” -or $_.Gettype().Name-eq “VmBeingDeployedEvent”} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage

    Can you please modify this so that the resut can be exported as a Excel Document

  7. hemantmpatel

    Hi Alan,

    Can I use the same script if i want to monitor total deployment time VM take like start time and end time. So i can monitor VM deployment performance along with the VM created list daily.
    Thanks in advance.
    Hemant Patel

  8. Neil

    Got exactly the same scripts from the ‘VMware Community PowerPack’ for PowerGUI but got no results. Increased the “-maxsamples 10000” value to 1000000 and got the results back. Yes the script took longer but got the information I needed!

  9. Chris King

    It is likely that the maxsamples is not enough to find the events you seek. I had to go to 100000 to get the last 10 vms created. Even using the -types info did not seem to help as most events seem to be info type.

  10. Ravi

    Do we have any chance to collect which we have removed the vm’s from VCenter inventory through powercli ?

  11. CCD

    Hi Alan,

    Suppose I want to run this query for specific Cluster in my DC , in that case how can I do this ..

  12. Pingback: vCloud Directory Management cluster disaster recovery | vcdx56

  13. Pete

    Alan,
    I don’t believe that this takes into account VMs that are being removed from inventory vs VMs that are truly deleted from disk. Any ideas how I could differentiate these?

  14. Sazzy

    When I executed this command…it executes successfully without any error but I doesn’t show the result even I tried exporting it but csv file was blank. Any help here…

  15. Ben Liebowitz

    How can I modify this to get more than 14 days? I’ve tried increasing the # from 14 to 120 to get the last 4 months but it isn’t returning the required info.

  16. Hemant Patel

    Hi,
    This is really a great script. Actually to test i entered 365 days report for VM creation but it only give me 4 VM’s created. So my question is IS it depend on vCenter server database from where it get the events. What changes i have to do in my vCenter server for correct reporting through this script?

    Regards,
    Hemant Patel

  17. Justin

    The output command doesnt output anything… not sure why.

    Is there a way to output this to HTML? or a CSV?

  18. Virtu-Al

    @vuong
    This is one of the things included in my Daily Report script but I have also altered the post above to show how to do this.

  19. Glenn Sizemore

    Very cool, I really need to go back and look at all the new cmdlets. Half the stuff I still do with Get-View have real cmdlets now! I would suggest using the -match operator… Slightly faster and easier to read.

    Get-VIEvent -maxsamples 10000 |
    where-Object {$_.Gettype().Name -match “(VmCreatedEvent|VmBeingClonedEvent|VmBeingDeployedEvent)“} |
    Sort-object CreatedTime -Descending |
    Select-Object CreatedTime, UserName, FullformattedMessage -First 10

    ~Glenn

Leave a Reply to hemantmpatel

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.