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
Pingback: VMware Cloud on AWS – VM Creation Date available in vSphere API
I AM LOOKING TO GET A SCRIPT WHERE I CAN FIND VM NAME TYPE OF SERVICE AND LOG ON TO
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
Add the below line in script to take out the output
| Export-Csv -path “c:\reports\vminventory.csv”
Put the below path in script to take the output
| Export-Csv -path “c:\reports\vminventory.csv”
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 |
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
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
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!
Hi Glenn, I was just trying your script and it didn’t bring up and results – any idea ?
Thanks
Haim
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.
Do we have any chance to collect which we have removed the vm’s from VCenter inventory through powercli ?
Hi Alan,
Suppose I want to run this query for specific Cluster in my DC , in that case how can I do this ..
Pingback: vCloud Directory Management cluster disaster recovery | vcdx56
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?
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…
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.
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
The output command doesnt output anything… not sure why.
Is there a way to output this to HTML? or a CSV?
Actually I would do….
command(s) | Out-File c:\path\filename.txt
command(s) > \path\to\file.name
How can I output the results to a text file?
thanks
@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.
Can we modify the script to show ALL VMs built in last time period given?
@Glenn Sizemore
Great idea with the regex, didnt think of that. Thanks for the comment. Love your blog.
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