VCF Making a Network Pool with Powershell!

Hereby my take on a scripted version of creating a networkpool in VCF! Please look at the code before using because there are some assumptions in there that may mess up your ip subnets. This is made for a customer based on their needs and design so test before using at your customer! This one will make a VSAN and vMotion pool, so there is room for improvement getting NFS and iSCSI in if there is a need for it!

#This script will take some input and uses it to generate the JSON file to create a network pool in SDDC Manager
#It will also create the pool for you.

#Continue Function
$options = [System.Management.Automation.Host.ChoiceDescription[]] @('&Yes', '&No')

function Prompt-Continue{
    if(0 -eq $host.UI.PromptForChoice('Create the Network Pool?' , 'Create the Network Pool?' , $Options,0)){
        Write-Host Creating the Network Pool... -fore green
        Write-Host Ending the script.

#Data Input
$clustername = Read-Host "type in the name of the cluster"
[int]$vsanvlanid = Read-Host "type in the vlan id for the vSAN Network"
[int]$vmotionvlanid = Read-Host "type in the vlan id for the vMotion Network"
$vsansubnet = Read-Host "Enter subnet for vSAN network"
$vmotionsubnet = Read-Host "Enter subnet for vMotion network"
$mtu = [int]"9000"

$vsangateway = $vsansubnet.Substring(0,9) + "1"
$vsanstartip = $vsansubnet.Substring(0,9) + "10"
$vsanendip = $vsansubnet.Substring(0,9) + "254"

$vmotiongateway = $vmotionsubnet.Substring(0,9) + "1"
$vmotionstartip = $vmotionsubnet.Substring(0,9) + "10"
$vmotionendip = $vmotionsubnet.Substring(0,9) + "254"

$NetworkPoolName = "network-pool-" + $clustername.Substring(4)

#Checks on SDDC Manager
#prepare additional variables, no input required here
$targetDC = $clustername.Substring(0,3)
$sddcManagerFqdn = $targetDC + ".mydns.local" #Make sure this variable reflects your SDDC Manager FQDN
$VCFAuthURL = "https://" + $sddcManagerFqdn + "/v1/tokens"
$Type = "application/json"

#credentials --> to be fixed and changed to secure credentials
$sddcManagerUser = "USERNAME"
$sddcManagerPass = "PASSWORD"

# Creating JSON for Auth Body
$AuthJSON =
  ""username"": ""$sddcManagerUser"",
  ""password"": ""$sddcManagerPass""

Try {
    $VCFSessionResponse = Invoke-RestMethod -Method POST -Uri $VCFAuthURL -Body $AuthJSON -ContentType $Type
Catch {
    $error[0] | Format-List -Force
    Write-Output "Unable to connect to SDDC Manager: $sddcManagerFqdn"

#Create Session
$SessionHeader = @{"Authorization"= "Bearer " + $VCFSessionResponse.accessToken

#Collect all the network pools
$allNetworkPools = Invoke-RestMethod -Method GET -Uri ("https://" + $sddcManagerFqdn + "/v1/network-pools") -Headers $SessionHeader -ContentType $Type -Body $Params
#Get all the elements fron NW pool JSON output
$allNWPoolsInfo = $allNetworkPools.elements

#Filter for correct pool and check for existence of the pool, if pool exists script will stop
$retrievedNWPoolName = $allNWPoolsInfo | where {$ -like $targetNWPoolName} -Debug
if ($retrievedNWPoolName){
    Write-Warning ("Network Pool with name: " + $targetNWPoolName + " already exists stopping script") -WarningAction Stop
else {
    Write-Host ("Network Pool with name: " + $targetNWPoolName + " not found, proceeding...") -ForegroundColor Green

#Start to build the JSON

$jsonBase = @{}
$networkbase = @()

#vSAN JSON part
$vsanippool = New-Object System.Collections.ArrayList
$vsanippool.Add(@{"start" = $vsanstartip; "end" = $vsanendip;})

$vsannetwork = New-Object System.Collections.ArrayList
$networkbase += $vsannetwork

#vMotion JSON part
$vmotionippool = New-Object System.Collections.ArrayList
$vmotionippool.Add(@{"start" = $vmotionstartip; "end" = $vmotionendip;})

$vmotionnetwork = New-Object System.Collections.ArrayList
$networkbase += $vmotionnetwork

#Put everything in the base
$jsonBase = (@{"name" = $NetworkPoolName ; "networks" = $networkbase})

#End of building JSON

#Export the JSON file
$jsonBase | ConvertTo-Json -Depth 10 | Out-File ("D:\Scripts\VMware\VCF\NetworkPools\$NetworkPoolName.json")
write-host ("NetworkPool file succesfully saved to D:\Scripts\VMware\VCF\NetworkPools\$NetworkPoolName.json") -ForegroundColor Green


#Create the Network Pool
#make the body
$body = $jsonBase | ConvertTo-Json -Depth 10
#Make the pool
Invoke-RestMethod -Method Post -Uri ("https://" + $sddcManagerFqdn + "/v1/network-pools") -Headers $SessionHeader -ContentType $Type -Body $body
Write-Host Network Pool $NetworkPoolName has been created on $sddcManagerFqdn -ForegroundColor Green

Leave a Reply