PowerCli 13 Create NSX Security Groups

Submitted by Robin van Altena on Fri, 02/24/2023 - 07:54
 
 
Follow your favourite author

Leave us your email address and be the first to receive a notification when Robin posts a new blog.

PowerCli 13 Create NSX Security Groups
Fri 24 Feb, 2023
A few months ago, in November 2022, VMware released PowerCLI version 13. PowerCLI 13 contains a lot of new features and updates. I usually don’t read through the entire release notes, but somehow one line caught my attention:
“The Get-NsxtPolicyService, Get-NsxtGlobalManagerService, and Get-VmcSddcNetworkService cmdlets have been deprecated. Use the cmdlets from the VMware.Sdk.Nsx.Policy module instead.”
Since I’m using the Get-NsxtPolicyService quite a lot in scripts to import and export NSX Distributed Firewall configuration, I decided to have a closer look.
Textarea

As stated in the release notes for PowerCLI 13 the Get-NsxtPolicyService cmdlet will be deprecated. The cmdlet will be replaced by the VMware.Sdk.Nsx.Policy in PowerCLI 13. The VMware.Sdk.Nsx.Policy module was already introduced in PowerCLI 12.6, in May 2022. The Get-NsxtPolicyService cmdlet is still working in PowerCLI 13, as I will show later. For now you will see a message that the cmdlet is deprecated. But as always it is better to be prepared for the change and update your scripts in advance.

Image
Get-NsxtPolicyService deprecated message in PowerCLI 13
Textarea

The people at VMware have already written a nice blog about the changes and some basic examples. They also provide some examples in a script. So, I will not go to deep into that, but in this blog I wanted to provide some additional examples and point out some of the changes.

The first change that I like a lot is the new method for connecting. With the Get-NsxtPolicyService cmdlet it took quite a while to get connected to the NSX Manager. In a normal environment around a minute, but in lab environment, with limited resources, it can easily exceed 5 minutes.

The new connect cmdlet is now called Connect-NsxServer, the difference is in the T. The old cmdlet was called Connect-NsxtServer. The new cmdlet is much faster in connecting to the NSX manager, in my lab within a minute. For me that is a great improvement.

This is a result of the new direction VMware has chosen for the new VMware.Sdk.Nsx.Policy module. Previously the only cmdlet was Get-NsxtPolicyService and with the new VMware.Sdk.Nsx.Policy module the options have been split into smaller cmdlets.

So, let’s talk about the new cmdlets. The complete list of cmdlets for the new VMware.Sdk.Nsx.Policy module can be viewed with:

Get-Command –Module VMware.Sdk.Nsx.Policy

But is it is also possible to search for cmdlets to create a group:

Get-Command -Module VMware.Sdk.Nsx.Policy -name Invoke-*group*

Image
List the group cmdlets in the VMware.Sdk.Nsx.Policy module
Textarea

As you can see this is still a large list, so not very useful for now. Even with the new Get-NsxOperation cmdlet the list is quite long when searching for a specific command, like creating a group. But to me this is easier than the previous method for Get-NsxtPolicyService:

$groupfunc = Get-NsxOperation -Method GET -Name '*group*'
$groupfunc.Name

Image
List the group cmdlets with the new Get-NsxOperation cmdlet
Textarea

I always find examples are much more useful. In the PowerCLI 12.6 blog VMware wrote an example for creating a dynamic group, based on Tags:

Code (new)

$cond = Initialize-Condition -ResourceType Condition -Id $GroupName `
          -MemberType $MemberType -Value $Value -Key $Key `
          -Operator $Operator
$group = Initialize-Group -DisplayName $GroupName -Expression @($cond)
$createdGroup = Invoke-PatchGroupForDomain -DomainId default `
                  -Group $group -GroupId $GroupName

Textarea

First a condition is created, then the group is initialized and finally the group is created. The structure of the groups in NSX hasn’t changed. It is still based on different types of expressions. So, it is useful to know how the relationships for Security Groups work. The following tree helps with this. I will refer to it when we start to create different types of groups.

Image
Security group parent-child relation between the Expression and Conditions
Textarea

Back to the example for creating a dynamic group. With the previous method we had similar steps, but I found the commands a little more complicated to figure out:

Code (new)

$GroupData = Get-NsxtPolicyService -Name com.vmware.nsx_policy.infra.domains.groups
$GroupExpSpec = $GroupData.Help.patch.group.expression.Element.nested_expression.Create()
    $ExpSpec = $GroupData.Help.patch.group.expression.Element.condition.Create()
    $ExpSpec.member_type = $groupmember_type
    $ExpSpec.value = $Tag
    $ExpSpec.key="Tag"
    $ExpSpec.operator = "EQUALS"
    $GroupExpSpec.expressions.add($ExpSpec) | Out-Null
$GroupSpec = $GroupData.Help.patch.group.Create()
     $GroupSpec.display_name = $Groupid
$GroupData.patch($Domain, $Groupid, $GroupSpec)

Textarea

Creating a Security Group based on an IP-address or set

 

Let’s get started, because although the information above gives a nice introduction, it doesn’t show anything new. We will use the same steps to create a Security group with IP-addresses as members. As you can see in the relations tree above, there is an IPAddressExpression. So, we are going to create that first. We will need to provide the IP-addresses as an array to the expression. With the expression we will initialize and create the group. Similar to the example.

Code (new)

$ipexpress = Initialize-IPAddressExpression -IpAddresses $ArrayIPSets -ResourceType "IPAddress"
$group = Initialize-Group -DisplayName $GroupName -Expression @($ipexpress)
$createdGroup = Invoke-PatchGroupForDomain -DomainId "default" -Group $group -GroupId "Name or ID"

Textarea

You can retrieve additional information for the Initialize-IPAddressExpression cmdlet with:

Get-Help Initialize-IPAddressExpression

Image
Get-Help for the Initialize-IPAddressExpression cmdlet
Textarea

The complete code can look something like this:

Code (new)

$Groupname = 'IP-Addresses'
$IPSets =  '10.20.30.40,11.21.31.41'
$ArrayIPSets = @($IPSets.Split("{,}"))

$ipexpress = Initialize-IPAddressExpression -IpAddresses $ArrayIPSets -ResourceType "IPAddress"
$group = Initialize-Group -DisplayName $GroupName -Expression @($ipexpress)
$createdGroup = Invoke-PatchGroupForDomain -DomainId "default" -Group $group -GroupId $Groupname

Textarea

After connecting to the NSX manager and running this code you should get the following result:

Image
Creating a IP-address group in NSX with PowerCLI 13
Textarea

Creating a Security Group based on Active-Directory groups

 

In the next example we will create a Security group based on Active-Directory groups. This group can be used for the Identity based firewall. Thus the Active-Directory needs to be configured for the Identity based Firewall.

Image
Identity Firewall AD configuration
Textarea

Again the steps are relatively similar to the example above. But as shown in the relations tree above, we need to use the IdentityGroupExpression. 

Code (new)

$idexpress = Initialize-IdentityGroupExpression -IdentityGroups $ArrayIDGroups -ResourceType IdentityGroup
$group = Initialize-Group -DisplayName $GroupName -ExtendedExpression @($idexpress)
$createdGroup = Invoke-PatchGroupForDomain -DomainId $Domain -Group $group -GroupId $Groupid

Textarea

And again you can use the Get-Help command to get additional information about the Initialize-IdentityGroupExpression cmdlet. The difference between the IdentityGroupExpression and the IPAddressExpression is that the IdentityGroupExpression is an ExtendedExpression. You need to specify this when initializing the group.

Before creating the IdentityGroupExpression we need to retrieve some additional information about the Active Directory group. Like the name, domain base name, and distinguished name. This information needs to be placed in an array, similar to the IP-addresses. You can use the following as a guidance:

Code (new)

$Groupname = 'ID-Group'
$ADGroupName = 'vCenter-Administrators'

$Domain = "redlogic.local"
$DNBase = "DC=redlogic,DC=local"

$ADDSGroup = Get-ADGroup -Server $Domain -Filter "name -eq '$ADGroupName'"
$DNGroup = $ADDSGroup.DistinguishedName

$IDGroup = Initialize-IdentityGroupInfo -DistinguishedName $DNGroup -DomainBaseDistinguishedName $DNBase

$ArrayIDGroups = @()
$ArrayIDGroups += $IDGroup

Textarea

The groups can also be added based on their SID, but I’m simply reusing the logic I already build before and have all the information already available.

When combining this the result should look something like this:

Image
Creating an AD-group based Security group in NSX with PowerCLI 13
Textarea

Another thing that I like about the new PowerCLI 13 modules is that the errors give a more clearer perspective on the issue. For example if you don’t use the ExtendedExpression to create the group with Active-Directory groups. The error will look like this:

Image
Creating an AD-group based Security group with the wrong expression
Textarea

Of course there is still a learning curve in figuring out how to script with the PowerCLI 13 modules, but it is much better than the PowerCLI 12 methods.

In another blog I will provide some addition examples for creating services, since creating services isn’t included in the examples provided by VMware.

Hopefully, you have enjoyed reading this blog. If you have any questions or would like to see some more, please use the message option below.

Questions, Remarks & Comments

If you have any questions and need more clarification, we are more than happy to dig deeper. Any comments are also appreciated. You can either post it online or send it directly to the author, it’s your choice.
Let us know  

 
 
Questions, Remarks & Comments

Message Robin directly, in order to receive a quick response.

More about RedLogic