Virtu-Al.Net

Virtually everything is poshable

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

, ,

8 Responses to “PowerCLI: One-liners last 10 VMs created and removed”

  • 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

  • Virtu-Al says:

    @Glenn Sizemore
    Great idea with the regex, didnt think of that. Thanks for the comment. Love your blog.

  • vuong says:

    Can we modify the script to show ALL VMs built in last time period given?

  • Virtu-Al says:

    @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.

  • Angus says:

    How can I output the results to a text file?

    thanks

  • twodeer says:

    command(s) > \path\to\file.name

  • Alan says:

    Actually I would do….

    command(s) | Out-File c:\path\filename.txt

  • Justin says:

    The output command doesnt output anything… not sure why.

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

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>