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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
$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 } |