VM Creation Date & Time from Powercli




Most of the times we have several requirement when we talk about IT environment like designing , deployment , compliance check or for Security auditing the environment.

Somewhere during security auditing we require to provide several information to security team to get successful audit.

One of them is the compliance of Virtual machine auditing of creation date and time.

Here into this post we will explore how to get the creation date and time of virtual machine hosted into the vCenter or ESXi.

To get the details we will use VMware Powercli to extract the details.

By default there is no function added into Powercli to get such details, so here we will add a function of vm creation date.

Below is the function which needed to be copy and paste into the Powercli.

=======================================================================

function Get-VMCreationTime {
   $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
}

=======================================================================

  •   Now connect to vCenter server by using Connect-VI server command. 
             Example:

     C:\PS>Connect-VIServer -Server 192.168.0.1 -User Administrator
     -Password passwd

       Name                                    Port
       ----                                    ----
       192.168.0.1                             443


  •   Run below command, which will give you all the VM details into the vCenter for its          creation date and time.

      Get-VM | Get-VMcreationTime

        This will give the output all the VM’s creation details.





        Happy Sharing …  :)




Popular posts from this blog

Could not connect to one or more vCenter Server systems: https://FQDN:443/sdk

Changing the FQDN of the vCenter appliance (VCSA)