VMware Cloud Community
txarls_89
Contributor
Contributor
Jump to solution

Filtering using where-object and variable

Hi!

I want to filter my DRS rules using some specific parameters, the problem that i have is that when using the cmdlet "where-object' it doesn't filter anything.

All my DRS rules in cluster are composed like this:

exposition_cpd_VM_category_ = internet_london_VM_gold

$clusters= "Get-Cluster"

foreach($cluster in $clusters)

{

$log= Get-Cluster $cluster |Get-DrsRule |Where-Object {$_.Name -like *$exposition_$cpd_VM_$category_} | Select Name,Enabled,Type

Write-Host $log

}

Anyone can help me?

Best regards

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, think I got it.

Your issue is with variable substitution in a string.

Try like this, I used a RegEx expression with an exact match

$exposition= 'internet'

$cpd = 'london'

$category= 'silver'


foreach($cluster in Get-Cluster){

   Get-DrsRule -Cluster $cluster | Where-Object {$_.Name -match "^${exposition}_${cpd}_VM_${category}$"} |

  Select Name,Enabled,Type

}


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

It would help if you showed the name of at least one of those rules.

From you line "exposition_cpd_VM_category_ = internet_london_VM_gold" I can't really determine how a DRS rule is named.

Neither from the operand in the Where-clause I'm afraid.


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

0 Kudos
txarls_89
Contributor
Contributor
Jump to solution

Hi LucD!

I will try to explain better Smiley Happy

All the rules are composed as follows " exposition_cpd_VM_category" , then, the rules are called like this

internet_london_VM_gold

-  internet_london_VM_silver

-  internet_london_VM_bronze

-  intranet_london_VM_gold

intranet_london_VM_silver

-  intranet_london_VM_bronze

Then for example ,i want to filter the rules that are

$exposition= internet

$cpd=london

$category= silver

$clusters= "Get-Cluster"

foreach($cluster in $clusters)

{

$log= Get-Cluster $cluster |Get-DrsRule |Where-Object {$_.Name -like *$exposition_$cpd_VM_$category_} | Select Name,Enabled,Type

Write-Host $log

}

If this script would working, the result would be this rule

"internet_london_VM_silver"

I hope it is understood now

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, think I got it.

Your issue is with variable substitution in a string.

Try like this, I used a RegEx expression with an exact match

$exposition= 'internet'

$cpd = 'london'

$category= 'silver'


foreach($cluster in Get-Cluster){

   Get-DrsRule -Cluster $cluster | Where-Object {$_.Name -match "^${exposition}_${cpd}_VM_${category}$"} |

  Select Name,Enabled,Type

}


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

0 Kudos
txarls_89
Contributor
Contributor
Jump to solution

Thanks LucD is working Smiley Happy, great job!

0 Kudos