Here is a script for mass DLR L2 bridge creation. I had to bridge a couple of hundred VLAN to VXLAN, and while it was maybe faster to create it by hand I would not have learned anything.
The script is reading from a CSV file where I have all my info. Then loops through the entries and create a distributed port group and then initiates an L2 bridge. The VXLAN had been created post to this operation.
$csv = Import-Csv "D:\temp\VLAN.csv" -Delimiter ";"
Import-Module PowerNSX
get-module -name vmware* -ListAvailable | Import-Module
$cred = get-credential
connect-viserver -server -Credential $cred
foreach ($net in $csv) {
$vdportgroup = ("zitmit-$($net.acl)").ToLower()
$exists = Get-VDSwitch -Name "DSMpls01-EX" | Get-VDPortgroup -Name $vdportgroup -ErrorAction SilentlyContinue
if (!$exists) {
Get-VDSwitch -Name "DSMpls01-EX" | New-VDPortgroup -Name $vdportgroup -VLanId $net.mitvlan -NumPorts 2
$created = Get-VDSwitch -Name "DSMpls01-EX" | Get-VDPortgroup -Name "zitmit-acl-10344"
if (!created) {
Write-Host -ForegroundColor Green "Portgroup created: $vdportgroup"
$vdportgroupId = ($created.Id).Replace("DistributedVirtualPortgroup-","")
$vdportgrpupName = $created.Name
create-nsxl2bridge -aclname $($net.acl) -dvportGroup $($created.key)
}
}
else {
Write-Host -ForegroundColor Yellow "Portgroup have allready been created: $vdportgroup"
#Get-VDSwitch -Name "DSMpls01-EX" | New-VDPortgroup -Name $vdportgroup -VLanId $net.mitvlan -NumPorts 2
}
}
Function create-nsxl2bridge {
param(
[string]$aclname,
[string]$dvportGroup
)
# Login info
$nsxUsername =
$nsxPassword =
# Allow all SSL protocols
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
# Connect to NSX manager
$connection = Connect-NsxServer 10.1.70.5 -Username $nsxUsername -Password $nsxPassword -WarningAction SilentlyContinue
$virtualwire = Get-NsxLogicalSwitch | Where-Object { $_.name -match "$aclname" -and $_.name -notmatch "lan" }
if ($virtualwire.count -gt 1) {
$message = "Something could wrong - $aclname"
write-host $message -ForegroundColor yellow
$message | Out-File C:\log\create-nsxl2bridge.txt -Append
$virtualwire = $virtualwire[0]
}
elseif (!$virtualwire) {
$message = "virtualwire was not found: $($virtualwire.objectId) - acl: $aclname"
write-host $message -ForegroundColor yellow
$message | Out-File C:\log\create-nsxl2bridge.txt -Append
return
}
# Edge info
$edgeId = "edge-1120"
$Type = "Accept: application/xml"
$Header = @{"Authorization" = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($nsxUsername + ":" + $nsxPassword)) }
$nsxUri = "https://10.1.0.4/api/4.0/edges/$edgeId/bridging/config"
# Getting edge config
$currentL2Config = $null
$currentL2Config = Invoke-RestMethod -Uri $nsxUri -Headers $Header -Method GET -ContentType $Type
# Check if already there
foreach ($z in $currentL2Config.SelectNodes("//name"))
{
if ($z.'#text' -match $aclname ) {
write-host "Already exists: $aclname" -ForegroundColor yellow
return
}
}
# Add extra xml node to currentconfig
$handler1 = $null
$handler1 = $currentL2Config.CreateNode('element', "bridge", '')
$attr = $currentL2Config.CreateNode('element', "bridgeId", '')
$attr.InnerText = "$null";
$handler1.AppendChild($attr)
$attr = $currentL2Config.CreateNode('element', "name", '')
$attr.InnerText = "$aclname";
$handler1.AppendChild($attr)
$attr = $currentL2Config.CreateNode('element', "virtualWire", '')
$attr.InnerText = "$($virtualwire.objectId)";
$handler1.AppendChild($attr)
$attr = $currentL2Config.CreateNode('element', "dvportGroup", '')
$attr.InnerText = "$dvportGroup";
$handler1.AppendChild($attr)
# Remove nodes from existing XML
$currentL2Config.SelectNodes("//virtualWireName") | ForEach-Object { $_.ParentNode.RemoveChild($_) }
$currentL2Config.SelectNodes("//isSharedNetwork") | ForEach-Object { $_.ParentNode.RemoveChild($_) }
$currentL2Config.SelectNodes("//dvportGroupName") | ForEach-Object { $_.ParentNode.RemoveChild($_) }
# Add the newly created node to existing XML
$currentL2Config.bridges.AppendChild($handler1)
# PUT edge config
$respons = Invoke-RestMethod -Uri $nsxUri -Headers $Header -Method PUT -ContentType 'application/xml' -Body $currentL2Config
write-host "L2 Created: $($virtualwire.objectId) - acl: $aclname" -ForegroundColor Green
}
Was this post helpful?
Was this article helpful?
YesNo