VMware Cloud Community
nareshunik
Enthusiast
Enthusiast

Script to find when and who created a VM

Need a script to find when&who created a VM in "ClusterA"&"ClusterB"

0 Kudos
30 Replies
nareshunik
Enthusiast
Enthusiast

The scripts works.... you are great.... Smiley Happy

It gives me output for vm created last 10days.. if i need for the VM it was created for the last 400days and Only for two cluster "ClusterA" and "ClusterB", export in csv or xls...

That will great.. Smiley Happy

0 Kudos
LucD
Leadership
Leadership

Try the attached script


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

0 Kudos
nareshunik
Enthusiast
Enthusiast

I get the below error message

Get-View : 3/19/2012 7:41:50 AM    Get-View        View with Id  'HostSystem-ho
st-6712' was not found on the server(s).
At C:\users\cz3m7g\desktop\scripts\VM_created _date\who-created-vm.ps1:15 char:
28
+     $row.VMHost = (Get-View <<<<  $_.Host.Host).Name
    + CategoryInfo          : ObjectNotFound: (:) [Get-View], VimException
    + FullyQualifiedErrorId : Core_GetView_WriteNotFoundError,VMware.VimAutoma
   tion.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Get-VIEvent : 3/19/2012 9:02:36 AM    Get-VIEvent        The object has already
been deleted or has not been completely created
At C:\users\cz3m7g\desktop\scripts\VM_created _date\who-created-vm.ps1:8 char:1
2
+ Get-VIEvent <<<<  -Start $start -MaxSamples $eventNr | Where-Object {$_.GetTy
pe().Name -eq "VmCreatedEvent"} | % {
    + CategoryInfo          : NotSpecified: (:) [Get-VIEvent], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio
   n.ViCore.Cmdlets.Commands.GetEvent

0 Kudos
LucD
Leadership
Leadership

It looks like you found some events for VMs that were created on ESX(i) hosts that do not exist anymore.


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

0 Kudos
nareshunik
Enthusiast
Enthusiast

yes,

we have only ESXi servers. No esx. is it possible to get the Vm created date?

0 Kudos
abirhasan
Enthusiast
Enthusiast

What is the final soulution!!!!!

Waiting for the solution ?

abirhasan   
0 Kudos
a_p_
Leadership
Leadership

@abirhasan

One of the characteristics an IT person needs is patience. So just be patient an monitor this thread!

André

0 Kudos
rogue33
Contributor
Contributor

Thanks man. The script is very valuable.

Jeff Jones
0 Kudos
justinsmith
Enthusiast
Enthusiast

Script worked great for me...

Is there a way to output the data to excel or a csv file? It produces a high number and I cant see all the info....

Thanks!

And when I say script, Im referring to this one:

# How many days in the past to start from 
$start = (Get-Date).AddMonths(-1)
$events = "VmCreatedEvent","VMClonedEvent","VMDeployedEVent"
$eventNr = [int]::MaxValue

$report = @()
Get-VIEvent -Start $start -MaxSamples $eventNr |
    Where-Object {$events -contains $_.GetType().Name} | % {
    $row = "" | Select Date, Msg, User, Cluster, VMHost 
    $row.Date = $_.createdTime  
    $row.Msg = $_.fullFormattedMessage  
    $row.User = $_.userName  
    $row.Cluster = (Get-View $_.computeResource.computeResource).Name  
    $row.VMHost = (Get-View $_.Host.Host).Name  
    $report += $row
}
$report
0 Kudos
RvdNieuwendijk
Leadership
Leadership

To export the ouput of the script to a .csv file you can change the last line of the script into:

$report | Export-CSV -Path VMInfo.csv -NoTypeInformation -UseCulture
Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
esxi1979
Expert
Expert

I am running the scripts.. in my env its taking  more than 1 hr to get the o/p

Is the same for all of you ?

I need the o/p to a html .. i tried "| ConvertTo-Html | Set-Content c:\scripts\test.html".. but html does not have the info i need ...

my script

PowerCLI C:\Windows\system32> $CDT = Get-Date

PowerCLI C:\Windows\system32> $vm = Get-VM -Location ABC

PowerCLI C:\Windows\system32> $vm | Get-VIEvent -Types Info -Start $CDT.AddDays(-30) -Finish $CDT |Where { $_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent"} | Select UserName, CreatedTime, FullFormattedMessage | Format-Table -AutoSize |fl * | ConvertTo-Html | Set-Content c:\scripts\test.html

My 1 more need is .. next is to get the VM's cpu/memory/disk info in same table

can anyone help for the html & extended attributes needed info pls

thanks in advance

0 Kudos