VMware Cloud Community
prashantpote
Contributor
Contributor

PowerCLI script VM creation date and user info

Any power CLI script to get VM creation task details with date and user who created that VM.
7 Replies
kwhornlcs
Enthusiast
Enthusiast

Been a while since I've worked on it, but check vmCreationNotes in the PowerCLI example scripts here: PowerCLI-Example-Scripts/Scripts at master · vmware/PowerCLI-Example-Scripts · GitHub

Reply
0 Kudos
LucD
Leadership
Leadership

Sure, try like this.

Note1: this might run a while, since it will fetch quite a few events

Note2: you can adapt the value in $vmName to pick a specific set of VMs

Note3: the sample script that was pointed to in the other answer only goes back 1 day

$vmName = '*'

$eventTYpes = 'VmCreatedEvent', 'VmClonedEvent', 'VmDeployedEvent', 'VmRegisteredEvent'


Get-VM -Name $vmName |

   ForEach-Object -Process {

   Get-VIEvent -Entity $_ -MaxSamples ([int]::MaxValue) |

   where { $eventTYpes -contains $_.GetType().Name } |

   Sort-Object -Property CreatedTime -Descending |

  Select -First 1 |

   ForEach-Object -Process {

   New-Object PSObject -Property ([ordered]@{

   VM = $_.VM.Name

   CreatedTime = $_.CreatedTime

   User = $_.UserName

   EventType = $_.GetType().Name

   })

   }

   }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

golddiggie
Champion
Champion

We're looking to gather this same information for a set of VMs we get in a report (daily) that changes. I'm hoping that our 'scripting guy' can add your code to the existing script, targeting only the VMs it's reporting on and add the information in additional columns.

If he cannot get it to work, I'll be creating a thread of my own asking for some assistance. I suspect LucD would come up with what needs to be tweaked in <10 minutes of the thread being created. Smiley Wink

LucD
Leadership
Leadership

If your VMs names are for example in a .txt file, one on each line, you could change the 1st line into

$vmName = Get-Content -Path .\vmnames.txt


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
prashantpote
Contributor
Contributor

Thanks for reply. I tried above script but not getting output as my VM is created in 2017

Please suggest how can I get older vm creation events by script.

Reply
0 Kudos
LucD
Leadership
Leadership

In vSphere 6.7 the creation date is available, see William's VM Creation Date now available in vSphere 6.7

For VMs created in other vSphere versions that property is not available I'm afraid.

You could try looking at the timestamp on a VM's folder, but that is very unreliable since a VM can be moved, renamed...

If you didn't archive the events, or kept the extracted data somewhere, I'm afraid you're out of luck.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
prashantpote
Contributor
Contributor

Thanks for your support
Reply
0 Kudos