2 minute read

One of the fundamental tasks of an NSX-T deployment is creating new segments.

In a previous post we created a snippet to add new VLAN Backed segments using the NSX-T Policy API. This time we will create a quick snippet to add Overlay segments.

As in the previous post we will not connect the new segment to any T0/T1, we will do that in a later post covering overlay and vlan segments.

Quick reminder of the PowerCLI NSX-T Policy API CMDlets

The list of NSX-T Policy API CMDlets is not massive.

NSX-T Policy API CMDlets

Based in the number of cmdlets available could give the impression that you will not be able to do a lot of things with them, however these four cmdlets are powerful enough to create, remove or modify any object in the NSX-T Manager.

Quick snippet to create a new overlay segment

As mentioned, this post will focus in a quick code snippet to allow us to create multiple overlay segments in NSX-T using the NSX-T Policy API.

The cmdlet Get-NsxtPolicyService is the main key to all of it.

Information needed to setup our snippet

Transport Zone

To be able to create the segment in the right transport zone, we will need to collect the Transport Zone ID and the easiest way to retrieve it is through the simplified UI.

NSX-T Transport Zone ID

Now that we have the Transport Zone ID we can build the variable that will give us the path for the transport zone object.



Since it is a quick code snippet we could keep the list of variables on the top to reduce the need of editing the functional part of the snippet.

# Segment information
$segmentIDPrefix = "POD01-"
$segmentIDSuffix = "-Overlay-TZ-01"

# Transport Zone

# Segment Individual Name
$segmentIDs = @("segmentA", "segmentB", "segmentC", "segmentD")

Main code snippet body

The main body of the code snippet has two sections:

  • Foreach cycle to go through our Segment Individual ID list
    • Segment creation within the Foreach cycle

Foreach cycle to go through our Segment Individual ID list

This code snippet assumes that you are already connected to the NSX-T Manager using:

Connect-NsxtServer -Server "vcenter.lab" -User "admin" -Password "MyAwesomePassword"
Foreach ($segmentID in $segmentIDs) {
  # create SegmentID information using the predefined prefix + Segment Individual ID + suffix from the list
  $segmentID = $segmentIDPrefix + $segmentID + $segmentIDSuffix

  # Pull the current segment information
  $segmentList = Get-NsxtPolicyService -Name com.vmware.nsx_policy.infra.segments

  # Creating a new segment object
  $newSegmentSpec = $segmentList.Help.patch.segment.Create()
  $newSegmentSpec.id = $segmentID
  $newSegmentSpec.transport_zone_path = $transportZone

  # Create the segment
  $segmentList.patch($segmentID, $newSegmentSpec)


Code Snippet run

New Overlay Segments List - Simplified UI