VMware Cloud Community
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Get-vm where-object -like not returning results

Hi all,

I'm working on a fairly complicated script which will help with host patching.  I'd like to do as much of it myself as I possible can but would really like a push in the right direction. Here's the scenerio:

We have a complicated environment. Certain VMs we need to inform the SME's before we vMotion them, and we also have VMs which cannot be vMotioned because they reside on shared disks or have a DRS override configured. I would like to write a script that does the following:

Get a list of all hosts which need to be upgraded to 7.0.3
From that list of hosts, list the cluster and identify all clusters in which DRS is NOT set to fully automated
After getting the list of hosts, get a list of all VMs which reside on those hosts, from that list:
Check to see if there is a DRS override for the VM and the Automation Level is set to Partially Automated or Manual
Check the Disk type for a type which will prevent vMotion such as shared disks
Check for VM names which contain "PRD" "CORE" or "FIN"
Check the folder that the VM resides in for  "Group1" "Group2" or "Group3"

Once I have all this information I want to produce a report which contains all this information so I can work with SME's to schedule maintenance on the hosts where required, schedule vMotions for out of hours, etc.

 

 

It didn't take long for me to hit a snag. I have several VMs in a folder called "Group1-communications" and "Group1-Infra" however when I use the following command:

get-cluster MYCLUSTER| get-vm | Where-Object {$_.folder.name -ccontains "Group1"} | Select Name,Folder

I am getting no results. It does work if I use the full folder names: eg

get-cluster MYCLUSTER| get-vm | Where-Object {$_.folder.name -contains "Group1-communications"} | Select Name,Folder
or 
get-cluster MYCLUSTER| get-vm | Where-Object {$_.folder.name -contains "Group1-Infra"} | Select Name,Folder

Not sure what I'm doing wrong. Instead of using -contains I have tried:

-like
-ccontains

I want to get all VMs that have Group1 anywhere in the folder name.

Thanks in advance LucD 🙂

 

 

 

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm not sure how you did the -like test, but the -ccontains "... test for the existence of one item in a collection, array or hashtable".
And $_.folder.name is ot one of those, it is a String.

What you could do is use the -match operator which uses a RegEx expression, something like this

get-cluster MYCLUSTER| get-vm | Where-Object {$_.folder.name -match "^Group1"} | Select Name,Folder

The ^ states that the string on the left side should start with "Group1"


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

View solution in original post

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

I'm not sure how you did the -like test, but the -ccontains "... test for the existence of one item in a collection, array or hashtable".
And $_.folder.name is ot one of those, it is a String.

What you could do is use the -match operator which uses a RegEx expression, something like this

get-cluster MYCLUSTER| get-vm | Where-Object {$_.folder.name -match "^Group1"} | Select Name,Folder

The ^ states that the string on the left side should start with "Group1"


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

Reply
0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Hi LucD,

About the ^ in 

"^Group1"

 

Where can I find more statements like this? Specifically I'd like to be able to find Group1 even anywhere in the name, such as "PatchingGroup1" Or "TeamGroup1Messaging" etc. If I could see all the options like ^ I could probably find the right one that I need. 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

A good site to learn, and test, RegEx expressions is RegExr.
But there are many more.
I personally use RegExBuddy to test my RegEx expressions.
It's not free, but imho worth it's money.

The ^ in a RegEx expression means "Start of string or line"
If you leave the ^ out, the expression "Group1" will return true for any string that contains "Group1" in whatever position.


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