Automation

 View Only
  • 1.  Get-VM Name- MyVM_000* Older Than 2 days

    Posted Sep 25, 2017 11:26 AM

    Need help identifying VM's older than 2 days, I'm only filtering from a about 10 VMs that all begin with "MyVM_000"



  • 2.  RE: Get-VM Name- MyVM_000* Older Than 2 days

    Posted Sep 25, 2017 11:43 AM

    Found this Function LucD​ Posted, It probably can be trimmed down do what I need.

    function Get-VMCreationTimes {

       $vms = Get-VM MyVM_000*

       $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



  • 3.  RE: Get-VM Name- MyVM_000* Older Than 2 days
    Best Answer

    Posted Sep 25, 2017 12:00 PM

    When you only look for the VMs that were created during the last two days, and remove those from the list, that will probably be faster.

    $vmName = 'MyVM_000*'

    $nrDays = 2

    $start = (Get-Date).AddDays(- $nrDays)

    $entity = Get-VM -Name $vmName

    $events = Get-VIEvent -Entity $entity -Start $start -MaxSamples ([int]::MaxValue)

    $newVM = $events | where{$_ -is [VMware.Vim.VMCreatedEvent]} | %{$_.Vm.Name}

    $entity | where{$newVM -notcontains $_.Name}

    And if you use my Get-VIEventPlus, for example "Get-VIEventPlus -EventType 'VmCreatedEvent'...", that would most probably even be faster.



  • 4.  RE: Get-VM Name- MyVM_000* Older Than 2 days

    Posted Sep 25, 2017 12:06 PM

    Thanks Luc,

    I Managed to trim it down to this but that version you just sent me just seems much simpler..Cheers I'll give it a crack now.

    function Get-VMCreationTimes {

       $vms = Get-VM $VMfilter

       $vmevts = @()

       $vmevt = new-object PSObject

       foreach ($vm in $vms) {

          #Progress bar:

          $foundString = "Found: "+$vmevt.name+""+$vmevt.createdTime

          $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

          $vmevts += $vmevt

       }

       $vmevts | sort createdTime

    }

    Connect-VIServer vc.vsphere.com -username administrator@XX -password XX

    $date = (Get-Date).AddDays(-2)

    $VMfilter = 'MyVM_*'

    Get-VMCreationTimes | where {$_.createdTime -le $date}



  • 5.  RE: Get-VM Name- MyVM_000* Older Than 2 days

    Posted Sep 25, 2017 12:11 PM

    Hey Luc,

    Some not quite working with this cutdown version, it just shows all VM starting with MyVM regardless of changing days, I even changed to AddHours and still showed them all?



  • 6.  RE: Get-VM Name- MyVM_000* Older Than 2 days

    Posted Sep 25, 2017 12:13 PM

    Are you referring to your last script or mine?



  • 7.  RE: Get-VM Name- MyVM_000* Older Than 2 days

    Posted Sep 25, 2017 12:14 PM

    Your cutdown version



  • 8.  RE: Get-VM Name- MyVM_000* Older Than 2 days

    Posted Sep 25, 2017 12:37 PM

    Hey Luc,

    This seems to work, do you see any issues with it

    $vmName = 'MyMainVM_*'

    $nrDays = '20'

    $start = (Get-Date).AddHours(-$nrDays)

    $entity = Get-VM -Name $vmName

    $old = Get-VIEvent -Entity $entity -maxsamples 1000000 -Start $start -Types Info | %{$_.Vm.Name}

    Get-VM $old



  • 9.  RE: Get-VM Name- MyVM_000* Older Than 2 days

    Posted Sep 25, 2017 12:48 PM

    Actually had a bit of trouble with that one too once I got to 7 days but I think I might be close to solving it. I used {$_.DestName} instead of vm.name and filter on clonedVMs

    $vmName = '*'

    $nrDays = '7'

    $start = (Get-Date).AddDays(-$nrDays)

    $entity = Get-VM -Name $vmName

    $old = Get-VIEvent -Entity $entity -maxsamples 1000000 -Start $start -Types Info | where {$_.Gettype().Name -eq "VmBeingClonedEvent"} | %{$_.DestName}

    Get-VM $old



  • 10.  RE: Get-VM Name- MyVM_000* Older Than 2 days

    Posted Sep 25, 2017 12:58 PM

    Strange that you do not see VMCreatedEvent events.

    I would also filter on VmClonedEvent instead of VmBeingClonedEvent, the latter says that the clone started, but not that it it completed successfully.