PowerCLI

 View Only
  • 1.  VM Creation Date - Help running a script

    Posted May 06, 2015 06:52 PM

    Hi Folks,

    I need to know a vm creation date, and apparently the only way is to run a script against the event logs. I found this script: Determining VM creation times from vCenter events « vmdev.info

    I copied the script into notepad and saved as a .ps1 file.

    I am a newbie with powershell and powercli but got it all installed and working. I connected to a host (5.5) and ran the script .\script.ps1. Nothing happened, no error, no results. The cursor simply returned.

    Can someone help me with what I am missing? Or is there another way to determine the creation date? Now that I am started I am very keen to learn powercli. Thanks



  • 2.  RE: VM Creation Date - Help running a script

    Posted May 06, 2015 09:16 PM

    An ESXi node only keeps events for a limited time, the actual archiving is done on a vCenter server.

    Can you run the same script, but then connect to the vCenter ?



  • 3.  RE: VM Creation Date - Help running a script

    Posted May 06, 2015 10:45 PM

    I connected to vcenter and ran the script with the same result. No error, no output.This script is a few years old, do you think it may not be compatible with 5.5?



  • 4.  RE: VM Creation Date - Help running a script
    Best Answer

    Posted May 07, 2015 08:20 AM

    Did you actually call the function (see last line) ?

    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

    }


    Get-VMCreationTimes



  • 5.  RE: VM Creation Date - Help running a script

    Posted May 07, 2015 05:38 PM

    Doh! Yep that was it. I haven't written any kind of code for so long that I spaced it that a function has to be called. And in the heat of the battle was just needing an answer that unfortunately  this did not provide. It is reporting that most of my VM's were created in the past month or two which is not even close. I have a failing application and I am trying to determine if it started happening after moving it to a new VM a few months ago.  Thanks so much for your help and sticking with me.