GuilhermeStela
Enthusiast
Enthusiast

Get VM's creation date

Jump to solution

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?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

View solution in original post

0 Kudos
33 Replies
tigerdeccan
Enthusiast
Enthusiast
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
}
GuilhermeStela
Enthusiast
Enthusiast

if i want write to a file i could use in wich line?

this one?

write-progress -activity $foundString -status $searchString -percentcomplete $percentComplete

0 Kudos
GuilhermeStela
Enthusiast
Enthusiast

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?

0 Kudos
nl26348
Contributor
Contributor

When I run this script I get no result.

Anyone idea?

0 Kudos
tigerdeccan
Enthusiast
Enthusiast

check if you have logs in Vcenter database .

0 Kudos
LucD
Leadership
Leadership

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

nl26348
Contributor
Contributor

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.

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
nl26348
Contributor
Contributor

Ok thx! The Database Retention Policy for tasks and events is 30 days.

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
nl26348
Contributor
Contributor

Yes, but we have several vm's created within 30 days.

0 Kudos
GuilhermeStela
Enthusiast
Enthusiast

well, now i'm afraid that someone has misconfigured it....

see the atachment pls.  With this checkboxes unmarked what is the pattern?

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
GuilhermeStela
Enthusiast
Enthusiast

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!

0 Kudos
falconbuban
Contributor
Contributor

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

0 Kudos
GuilhermeStela
Enthusiast
Enthusiast

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

0 Kudos
falconbuban
Contributor
Contributor

Thank you GuilhermeStela. After running the script you gave, only got this ">>". Attached here is the retention setting on the DB of the vcenter. Please advice.

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
falconbuban
Contributor
Contributor

Hi Luc, thank you for your reply. Below is the result of your script:

Count Name                      Group

----- ----                      -----
   73 UserLogoutSessionEvent    {VMware.Vim.UserLogoutSessionEvent, VMware.Vim.UserLogoutSessionEvent, VMw...
   65 UserLoginSessionEvent     {VMware.Vim.UserLoginSessionEvent, VMware.Vim.UserLoginSessionEvent, VMwar...
    2 NoAccessUserEvent         {VMware.Vim.NoAccessUserEvent, VMware.Vim.NoAccessUserEvent}
   60 AlreadyAuthenticatedSe... {VMware.Vim.AlreadyAuthenticatedSessionEvent, VMware.Vim.AlreadyAuthentica...
    1 BadUsernameSessionEvent   {VMware.Vim.BadUsernameSessionEvent}

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
}

0 Kudos