VMware Cloud Community
nicolasbouyssy
Contributor
Contributor
Jump to solution

PowerCLI: take an action on VM that match several requirements

Hi all,

I'm pretty new to powerCLI and I'm searching a way to take an action on some vms in my cluster that are matching several parameters.

Example : find all VMs that are powered on, have their NIC disconnected and not in the "to delete" folder. , then reset them.

I don't think the get-vm command gives me the possibility to have several search conditions though a kind of AND operator (I hope I'm wrong)

The only way I see is to use something like this:

$vm= get powered on VMs

foreach $vm

          get NIC status

               if NIC status ok

                    Do nothing

               if NIC status not ok

                    check if the vm is in the todelete folder

                             if vm in to delete folder

                                   do nothing

                             if vm is not in delete folder

                                   reset it.

What do you think of this experts? If I can get all this in one command it would be far more easier for me

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can use the Where-Object cmdlet to filter the output of the get-VM cmdlet. Like this:

Get-VM | `

Where-Object {

  $_.PowerState -eq "PoweredOn" `

  -and $_.Folder -ne "to delete" `

  -and -not ($_ | Get-NetworkAdapter).ExtensionData.Connectable.Connected

}


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can use the Where-Object cmdlet to filter the output of the get-VM cmdlet. Like this:

Get-VM | `

Where-Object {

  $_.PowerState -eq "PoweredOn" `

  -and $_.Folder -ne "to delete" `

  -and -not ($_ | Get-NetworkAdapter).ExtensionData.Connectable.Connected

}


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
nicolasbouyssy
Contributor
Contributor
Jump to solution

Wonderful ! Thanks a lot

Reply
0 Kudos