CarlosDionizio
Contributor
Contributor

Report With date creation VM

Hi,

Using this script: Get VM's creation date

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

}

And executing:

. .\Get-VMCreationTimes

Get-VMCreationTimes

This return only:

CreateTime

-----------

11/08/2014 16:25:23

27/05/2014 06:49:56

25/02/2014 10:41:50

01/04/2014 12:38:07

I need :

Vm-Name                         createdTime                 CreateBy

-----------                                  --------------              ---------------

Vm1                          11/08/2014 16:25:23               User1

VM2                          27/05/2014 06:49:56               User2

Vm3                         25/02/2014 10:41:50                Usernnn

Any wrong to this script or has a newer in CLI 5.1 Release 2 build 1012425 to get this information?

Tks Carlos

0 Kudos
2 Replies
Wh33ly
Hot Shot
Hot Shot

I tried your script as it is pasted above and I get for example:

(I changed name/IP/Created by) but they are filled with the correct information.

createdTime                                             name                                                    IPAddress                                              createdBy                                           
-----------                                             ----                                                    ---------                                              ---------                                           
25-7-2014 13:15:12                                      VMServer01                                           {127.0.0.1}                                        Domain\User

The return you post says "CreateTime", but this isn't any property in the script where did it came from ?

If you can get the information 1 by 1 it should work when you combine them Smiley Wink

0 Kudos
LucD
Leadership
Leadership

I think the line

$vmevt = new-object PSObject

should be inside the ForEach loop.


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

0 Kudos