vmware-archive / powernsx

PowerShell module that abstracts the VMware NSX-v API to a set of easily used PowerShell functions
173 stars 90 forks source link

Passing a Variable into a Security-Group Dynamic Criteria #594

Closed eaphilipp closed 5 years ago

eaphilipp commented 5 years ago

Hello,

I am building a menu driven function to build dynamic criteria inside a NSX Security Group. My issue is, the menu is populated with NSX Security Tags with the name that starts with APP-. This works great. I make a selection and I want that select to be used in dynamic criteria within a NSX Security group that gets created higher in the script which also works.

The Menu works like this:

  1. APP-SCRIPTTEST 2. APP-ANSIBLE
    1. APP-GITLAB 4. APP-FILE
    2. APP-PRINT 6. APP-THYCOTIC
    3. APP-REDFOREST 8. APP-ARCGIS
    4. APP-TRIDION 10. APP-MADDWEBAPP

So If I chose 1. I want the condition to include APP-SCRIPTTEST in the criteria.

This is the menu part of the script:

$script:c = Get-NsxSecurityTag | Where-Object { $_.Name -like 'APP-*' } | select name $menu = for ($i = 0; $i -lt $script:c.Count; $i += 2) {

$obj = [ordered]@{ Odd = "$($i+1). $($script:c[$i].Name)" } if ($script:c[$i + 1]) { $obj.Add('Even', "$($i+2). $($script:c[$i+1].Name)") } New-Object PSObject -Property $obj } $menu | Format-Table -HideTableHeaders $choice = Read-Host -Prompt "Please Select The Security Tag (1..$($script:c.Count))"

This is the part of the script that builds the dynamic criteria. $script:a is the Security Group which gets built earlier in the script. $script:q is a String that also gets inputted earlier in the script, this also works. The part that does work is highlighted in bold. passing the selected NSX Security Tag into the criteria.

Get-NsxSecurityGroup $script:a | Add-NsxDynamicMemberSet -DynamicCriteriaSpec (New-NsxDynamicCriteriaSpec -key VMName -Condition starts_with -Value $script:q) -CriteriaOperator ALL

Get-NsxSecurityGroup $script:a | Get-NsxDynamicMemberSet -Index 1 | Add-NsxDynamicCriteria -Key SecurityTag -Condition equals -value Get-NsxSecurityTag $script:c[$choice - 1].name

Get-NsxSecurityGroup $script:a | Get-NsxDynamicMemberSet -Index 1 | Add-NsxDynamicCriteria -Entity (Get-NsxSecurityGroup -name $x)

I have tried this a couple ways:

Get-NsxSecurityGroup $script:a | Get-NsxDynamicMemberSet -Index 1 | Add-NsxDynamicCriteria -Key SecurityTag -Condition equals -value Get-NsxSecurityTag $script:c[$choice - 1].name

Get-NsxSecurityGroup $script:a | Get-NsxDynamicMemberSet -Index 1 | Add-NsxDynamicCriteria -Key SecurityTag -Condition equals -value Get-NsxSecurityTag ($script:c[$choice - 1].name)

Get-NsxSecurityGroup $script:a | Get-NsxDynamicMemberSet -Index 1 | Add-NsxDynamicCriteria -Key SecurityTag -Condition equals -value $script:c

Any ideas? I am guessing I am just missing something very obvious. But this script is very large almost 800 lines and I am starting to see double. :-) As always thanks for the help.

eaphilipp commented 5 years ago

This is the error I am getting:

Add-NsxDynamicCriteria : Cannot validate argument on parameter 'DynamicMemberSet'. Object specified does not contain an index property. Specify a valid Dynamic Member Set. At H:\PS-Scripts\NSXManagement.ps1:325 char:154

alagoutte commented 5 years ago

What do you have on $c ?

Get-NsxSecurityGroup $script:a | Get-NsxDynamicMemberSet -Index 1 | Add-NsxDynamicCriteria -Key SecurityTag -Condition equals -value (Get-NsxSecurityTag $c)

don't work ?

eaphilipp commented 5 years ago

I apologize, this was actually an error in my code. I was pretty sure I had everything right and when it didn't work. I tried a few different variations I thought posting here would help. I tried a few more things yesterday after taking a break from it over the weekend and got it working....

$c = Get-NsxSecurityTag | Where-Object { $_.Name -like 'APP-*' } | select name $menu = for ($i = 0; $i -lt $c.Count; $i += 2) { $obj = [ordered]@{ Odd = "$($i+1). $($c[$i].Name)" }

if ($c[$i + 1])

{ $obj.Add('Even', "$($i+2). $($c[$i+1].Name)") } New-Object PSObject -Property $obj } $menu | Format-Table -HideTableHeaders $choice = Read-Host -Prompt "Please Select The Security Tag (1..$($script:c[1].Count))"

$tagname = Get-NsxSecurityTag $c[$choice - 1].name

Get-NsxSecurityGroup $script:a | Get-NsxDynamicMemberSet -Index 1 | Add-NsxDynamicCriteria -Key SecurityTag -Condition equals -value $($tagname.name)

Thanks for the help!