VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

Filtering unwanted VMs from an Array Object

I have an array object named $report

$report has a list of VMs.  Each VM has a large selection of both native and custom properties.  These VMs come from either "vcenter1", "vCenter2" or "vcenter-old".  vC

However, there are a few VMs in $report that need to be purged out, based on the following critierion.

1.  If the VMs exist in the vCenter named "vcenter-old" AND they are powered off, they need to be removed from the $report array variable.

OR

2.  If the VMs were deleted from "vcenter-old" during the past month, based on the vCenter event logs showing that VMname having been removed, and no longer existing in that vCenter, they need to be removed from the $report variable.

How could I accomplish this or could I?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Get all the events for the interval, then filter (with a Where-clause) on the $_.VM.Name property


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

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

I assume the required properties are all in the array ?

Then you could use a Where-clause, something like this

$newarray = $array | Where {($_.vCenter -ne "vcenter-old" -or $_.PowerState -ne "poweredoff")

It uses a Boolean expression (not (A and b) is equivalent to not A or not B).

For the 23nd condition, are those data available in the record ?


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

TheVMinator
Expert
Expert
Jump to solution

For the 2nd condition, I was thinking that I would try to see if there was a "VMremoved" event in the old vcenter.  The scenario is that I'm running a report to see what virtual machines were created last month.  However, at the same time, we have a migration going on.  While new VMs are being created in the new vCenters, VMs from the old vCenter are being migrated to the new vCenters.  So when I run my report on new VMs created during the last month, I don't want to include VMs that are not in fact new to the company, but were just migrated from the old vCenter.  When they are migrated, they generate an event in the new vCenter.  This makes it look like they are new VMs when I run get-vieventplus and pull those type of events.  In fact they are not new VMs, but migrated VMs.  This is the problem I am trying to solve.  Perhaps with that background you can recommend a better way to solve the problem.

The only way I could think of to solve the problem, was to create some code that queries the old vCenter server with something like get-vieventplus , and looks for events that represent a VM being removed from that vCenter or deleted.  Then look at those events, and get the name of the VM that was removed.  If that name also appears in my $report variable, that comprises all the new VMs last month, then I know it was in fact a migration, not a newly provisioned VM.  If my code determines it to in fact be a migration, then I remove it from my $report array variable.  Then I know I have a true representation of only newly provisioned VMs.  (The other scenario that would indicate that it was migrated, not newly provisioned, is that it is powered off in the old vcenter - the snippet you recommended would handle that scenario - thanks for that).

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The scenario you are thinking of would work as far as I can tell.

Let's list the steps:

  • Get the list of "removed" VMs from the old vCenter
  • If you can connect to the old and new vCenter at the same time, store those removed VMs in an array, otherwise store them in a CSV file
  • In the new vCenter get all the created VMs, but store these names in a hash table

          $hashTab = @{}

          Get-VIEventPlus -EventType "VMCreatedEvent" .... | %{

               $hashTab.Add($_.VM.Name,$_)

          }

  • Get the removed VMs from the old vCenter
  • Check if the removed VM is present in the hash table, if yes, remove the name from the hash table

        

          $removedVM | %{

               if($hashTab.ContainsKey($_.Name){

                    $hashTab.Remove($_.Name)

               }

          }

  • The hash table should now only contain new VM created on the new vCenter, and none of the migrated VMs from the old vCenter

          $hashTab.Keys

Does that help ?


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

TheVMinator
Expert
Expert
Jump to solution

OK - I think that solves part of the equation by using the hash table.  How would I get the list of removed vms from the old vCenter?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

With Get-VIEventPlus and look for the VmRemovedEvent entries.


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

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

OK thanks.  In the case of new vms, there were 3 different event types that could represent a new vm such as created, cloned, etc.  In the case of removing vms, I can think of at least 2 ways that a VM could be removed.  It could be removed by removing it from vcenter, or by deleting it from disk.  Perhaps there are other ways?  Does VmRemovedEvent cover every kind of way to get a VM out of vCenter, or are there other possible event types I need to look for as well?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Best would be to test this, remove a VM, then get all the events for the last 5 minutes.

From that collection of events you should be able to determine which kind of events are created by all the different ways a VM can be removed


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

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

Ok thanks.  Should I get all the events in the last 5 minutes using powercli or just use the GUI? (If powerCLI, how do I do select just the events that reference the name of the VM I removed?)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Get all the events for the interval, then filter (with a Where-clause) on the $_.VM.Name property


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

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

( Thanks for the help - This post is getting divergent from the original so I'll mark it correct and repost for items not related to the original question)

0 Kudos