VMware Cloud Community
ctech930
Contributor
Contributor
Jump to solution

Get-View -Filter HashTable expression Help

Need help with using get-view's -filter hashtable.    I have a script that I am using get-view to get clusters, I am providing a method to limit the results to a specific cluster in the event the person running the script only needs to run it against a single cluster.   My problem is when we have two clusters who's name are almost the same (the first part is identical then a space and more text) the filter will return both clusters..  I believe you can use a regex express with the -filter hastable but I am not exactly sure how to do that or how to build that expression.    Below is an example of the script and example of the clusters names.

Clusters in Environment:

NLX-TST-001

NLX-TST-001 (BETA)

NLX-TST-002

NLX-TST-003

NLX-TST-INF-001

Key parts of script related to my issue.

$viserver = "nlxtxtvc01"

$clustername = "NLS-TST-001"

$ClusterFilter = @{"name"=$clustername}

$clustersview = get-view -viewtype clustercomputeresource -server $viserver -filter $clustername

When I run the get-view as listed above the $clustersview will contain 2 results:

NLX-TST-001

NLX-TST-001 (BETA)

When what I want it to only return NLX-TST-001.      Now I know I could test to see if $clustersview contains more than 1 result and then using a loop or other matching mechanism filter to only the one I want.  But I believe since the -filter will allow you to use regex this should be able to be done right?   Possibly having to have two criteria in the regex express using to match the name...... Unfortunately I am not that experience with using or building regex expressions and I have mostly used very simple -Filters in the past..

Any Help will be greatly appreciated.

@Get-View

@PowerCli

@Regex

@-Filter

@hashtable

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Change the RegEx to this

$clustername = "NLS-TST-001$"

The dollar sign tells RegEx that the match has to end with characters you specified, it's an end-of-line in a way


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

Change the RegEx to this

$clustername = "NLS-TST-001$"

The dollar sign tells RegEx that the match has to end with characters you specified, it's an end-of-line in a way


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

ctech930
Contributor
Contributor
Jump to solution

Thanks!  That did it.. and just had to make a small modification since the $clustername variable is actually populated by user input I just did a $clustername = $clustername+"$"

0 Kudos