VMware Cloud Community
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Get-VM Name- MyVM_000* Older Than 2 days

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

Nicholas
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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.


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

View solution in original post

Reply
0 Kudos
9 Replies
nicholas1982
Hot Shot
Hot Shot
Jump to solution

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

Nicholas
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

Reply
0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

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}

Nicholas
Reply
0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

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?

Nicholas
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you referring to your last script or mine?


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

Reply
0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Your cutdown version

Nicholas
Reply
0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

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

Nicholas
Reply
0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

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

Nicholas
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

Reply
0 Kudos