VMware Cloud Community
David2021
Contributor
Contributor

Multiple choices with cmdlet where-object

Hello,

I need an help concerning the cmdlet where-object. I have multiple possibilities for a name of a server in a command. I used the cmdlet where-object in my command. I would like to know which one of these 2 propositions is right:

1/ ....| where-object {$_Name -Like "primary_*" -or $_Name -Like "secondary_*"}

2/ ....| where-object {($_Name -Like "primary_*") -or ($_Name -Like secondary_*)}

regards,

David

0 Kudos
4 Replies
LucD
Leadership
Leadership

Afaik, both will work.
The conditional operator precedence lists the 'like' operator before the 'or' operator.

Personally I would prefer a -match and the RegEx or

where-object {$_Name -match "^primary_|^secondary_"}


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

0 Kudos
David2021
Contributor
Contributor

Hello LucD,

Thanks for your return.

Concerning your proposition with -match and RegEx, for this part "^primary_|^secondary_"

The | is the same condition as "-or" ? and I don't need of * after _ as primary_* the begin part of the name?

 

0 Kudos
LucD
Leadership
Leadership

Yes, the vertical bar act as an 'or' in a RegEx.
And the * is not needed, the ^ indicates that the match should start at the beginning of the string.


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

0 Kudos
David2021
Contributor
Contributor

ok - Thanks for your explanation and your help on my post.

regards,

David

0 Kudos