It's possible to get the exact creation time of a VM?
I'm seeing here in the tasks and events but ain't found. Maybe some PowerCli cmdlet exclusive for reports?
Those settings mean that there is no retention period.
All events and tasks are kept in the vCenter database forever.
It could be that your VMs are created in a special way and that this method does not generate the eventtype the script is looking for.
A way to find out, create a new VM like you normally do.
Then collect all the events for the last hour and the list the eventtypes.
Something like this
Get-VIEvent -Start (Get-Date).AddHours(-1) -MaxSamples ([int]::MaxValue) |
Group-Object -Property {$_.GetType().Name}
Perhaps the list will shed a light why you don't see your VM creation dates.
If you see a lot of TaskEvents, we will have to look at the type of tasks.
Like this
$events = Get-VIEvent -Start (Get-Date).AddHours(-1) -MaxSamples ([int]::MaxValue) |
where {$_.GetType().Name -eq "TaskEvent"} |
Group-Object -Property {$_.Info.DescriptionId}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
function Get-VMCreationTimes { $vms = get-vm $vmevts = @() $vmevt = new-object PSObject foreach ($vm in $vms) { #Progress bar: $foundString = " Found: "+$vmevt.name+" "+$vmevt.createdTime+" "+$vmevt.IPAddress+" "+$vmevt.createdBy $searchString = "Searching: "+$vm.name $percentComplete = $vmevts.count / $vms.count * 100 write-progress -activity $foundString -status $searchString -percentcomplete $percentComplete $evt = get-vievent $vm | sort createdTime | select -first 1 $vmevt = new-object PSObject $vmevt | add-member -type NoteProperty -Name createdTime -Value $evt.createdTime $vmevt | add-member -type NoteProperty -Name name -Value $vm.name $vmevt | add-member -type NoteProperty -Name IPAddress -Value $vm.Guest.IPAddress $vmevt | add-member -type NoteProperty -Name createdBy -Value $evt.UserName #uncomment the following lines to retrieve the datastore(s) that each VM is stored on #$datastore = get-datastore -VM $vm #$datastore = $vm.HardDisks[0].Filename | sed 's/\[\(.*\)\].*/\1/' #faster than get-datastore #$vmevt | add-member -type NoteProperty -Name Datastore -Value $datastore $vmevts += $vmevt #$vmevt #uncomment this to print out results line by line } $vmevts | sort createdTime }
if i want write to a file i could use in wich line?
this one?
write-progress -activity $foundString -status $searchString -percentcomplete $percentComplete
I guess the createdTime is wrong....
createdTime name IPAddress createdBy
----------- ---- --------- ---------
23/2/2012 10:19:18 MyVM {x.x.x.x}
This VM was created a long time ago and i'm having the same problem for another VMs.
Maybe i don't have sufficient event logs?
When I run this script I get no result.
Anyone idea?
check if you have logs in Vcenter database .
Note that by default the Get-VIEvent cmdlet only returns 100 events.
That could explain why you are missing some creation dates.
Change this
Get-VIEvent $vm
into
Get-VIEvent $vm -MaxSamples ([int]::MaxValue)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Get-VIEvent $vm -MaxSamples ([int]::MaxValue) gave the same empty black result.
I see logs in the vcenter database. The statistics level is 1. The day interval duration 1 day is deselected.
The retention of tasks and events is controlled by the Database Retention Policy (<Administration><vCenter Server Settings>).
If you have retention active for a limited number of days, the events and tasks have been removed.
This has nothing to do with the Statistics.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok thx! The Database Retention Policy for tasks and events is 30 days.
That means you can't find creation dates for VMs that were created more than 30 days ago.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, but we have several vm's created within 30 days.
Those settings mean that there is no retention period.
All events and tasks are kept in the vCenter database forever.
It could be that your VMs are created in a special way and that this method does not generate the eventtype the script is looking for.
A way to find out, create a new VM like you normally do.
Then collect all the events for the last hour and the list the eventtypes.
Something like this
Get-VIEvent -Start (Get-Date).AddHours(-1) -MaxSamples ([int]::MaxValue) |
Group-Object -Property {$_.GetType().Name}
Perhaps the list will shed a light why you don't see your VM creation dates.
If you see a lot of TaskEvents, we will have to look at the type of tasks.
Like this
$events = Get-VIEvent -Start (Get-Date).AddHours(-1) -MaxSamples ([int]::MaxValue) |
where {$_.GetType().Name -eq "TaskEvent"} |
Group-Object -Property {$_.Info.DescriptionId}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks again Luc, those scripts helped me to find events in some Vms here.
I realize that we migrate three structures to a unique vCenter recently.. so the logs apoint to the migration`s day. 😃
thanks everybody for the help!
If I save this script and run (for example: [vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI>.\filename.ps1), nothing follows.
I there anything else I missed here? I verified that vcenter is keeping all the task and events forever (on db retention).
Thank you in advance.
Bobby
Los Angeles, CA
If you run the script like this?
Get-VIEvent -Start (Get-Date).AddHours(-1) -MaxSamples (::MaxValue) | Group-Object -Property {$_.GetType().Name}
This will get every events you have saved in your Vcenter Database. If there is no results maybe you have to check the Database Retention Policy anda make sure that is marked to save in wich period you are trying to query.
The script above will get the events from yesterday.
Kind Regards
The forum software mangled that line, it should be
Get-VIEvent -Start (Get-Date).AddHours(-1) -MaxSamples ([int]::MaxValue) | Group-Object -Property {$_.GetType().Name}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc, thank you for your reply. Below is the result of your script:
Count Name Group
looks like we have events saved on the vcenter DB but if I run the script and run (for example: [vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI>.\filename.ps1) I got nothing.
filename.ps1 contains the lines below:
function Get-VMCreationTimes { $vms = get-vm $vmevts = @() $vmevt = new-object PSObject foreach ($vm in $vms) { #Progress bar: $foundString = " Found: "+$vmevt.name+" "+$vmevt.createdTime+" "+$vmevt.IPAddress+" "+$vmevt.createdBy $searchString = "Searching: "+$vm.name $percentComplete = $vmevts.count / $vms.count * 100 write-progress -activity $foundString -status $searchString -percentcomplete $percentComplete $evt = get-vievent $vm | sort createdTime | select -first 1 $vmevt = new-object PSObject $vmevt | add-member -type NoteProperty -Name createdTime -Value $evt.createdTime $vmevt | add-member -type NoteProperty -Name name -Value $vm.name $vmevt | add-member -type NoteProperty -Name IPAddress -Value $vm.Guest.IPAddress $vmevt | add-member -type NoteProperty -Name createdBy -Value $evt.UserName #uncomment the following lines to retrieve the datastore(s) that each VM is stored on #$datastore = get-datastore -VM $vm #$datastore = $vm.HardDisks[0].Filename | sed 's/\[\(.*\)\].*/\1/' #faster than get-datastore #$vmevt | add-member -type NoteProperty -Name Datastore -Value $datastore $vmevts += $vmevt #$vmevt #uncomment this to print out results line by line } $vmevts | sort createdTime }