PowerCLI – Create routed vCloud Director Org Network

While answering a VMware Community Thread  I was finally motivated to do get some work done on my vCloud Director PowerShell Module. The new feature should be able to create a routed vCloud Director Org network and connect it to a Edge Gateway in one step. The requirement from the community thread was the ability to connect a Org Network to an existing Edge Gateway, but creating and connecting the network in one step makes more sense in my opinion.

Create routed vCloud Director Org Network - Terminal

The result of this command is a new Org Network, which is connected to an existing Edge Gateway. Per default the network is created as a “Shared” network, that means that all OrgVDCs within this organization can connect to the network. The New-MyOrgNetwork function has an optional parameter -Shared:$False which changes this behavior.

Create routed vCloud Director Org Network - Result

Tested versions:

  • VMware vCloud Director 8.20 / 9.1
  • VMware PowerCLI 6.5.1 / 6.5.4
  • PowerShell 5.0 / 5.1

Why is this function neccessary?

Unfortunately, the New-OrgNetwork Cmdlet, which should be able to do the job seems to have two problems:

  • Network can not be connected ta an Edge Gatway
  • The Cmdlet only works with older vCloud Director versions:

You can run this Cmdlet only against vCloud Director 1.5.x environments.

Similar to how I already did it for the Edge Gateway creation my own function must be created to fit my requirements.

Create routed vCloud Director Org Network per script

That is the excerpt of the new feature from my vCloud Director PowerShell Module:

Function New-MyOrgNetwork {
    <#
    .SYNOPSIS
        Creates a new Org Network with Default Parameters

    .DESCRIPTION

    .NOTES
        File Name  : New-MyOrgNetwork.ps1
        Author     : Markus Kraus
        Version    : 1.1
        State      : Ready

    .LINK
        https://mycloudrevolution.com

    .EXAMPLE
        New-MyOrgNetwork -Name Test -OrgVdcName "Test-OrgVDC" -OrgName "Test-Org" -EdgeName "Test-OrgEdge" -SubnetMask 255.255.255.0 -Gateway 192.168.66.1 -IPRangeStart 192.168.66.100 -IPRangeEnd 192.168.66.200

    .EXAMPLE
        New-MyOrgNetwork -Name Test -OrgVdcName "Test-OrgVDC" -OrgName "Test-Org" -EdgeName "Test-OrgEdge" -SubnetMask 255.255.255.0 -Gateway 192.168.66.1 -IPRangeStart 192.168.66.100 -IPRangeEnd 192.168.66.200 -Shared:$False

    .EXAMPLE
        $params = @{ 'Name' = 'Test';
                    'OrgVdcName'= 'Test-OrgVDC';
                    'OrgName'='Test-Org';
                    'EdgeName'='Test-OrgEdge';
                    'SubnetMask' = '255.255.255.0';
                    'Gateway' = '192.168.66.1';
                    'IPRangeStart' = '192.168.66.100';
                    'IPRangeEnd' = '192.168.66.200'
                    }
        New-MyOrgNetwork @params -Verbose

    .PARAMETER Name
        Name of the New Org Network as String

    .PARAMETER OrgVDCName
        OrgVDC where the new Org Network should be created as string

    .PARAMETER OrgName
        Org where the newOrg Networkshould be created as string

    .PARAMETER EdgeName
        Edge Gateway Name for the new Org Network as String

    .PARAMETER SubnetMask
         Subnet Mask of the New Org Network as IP Address

    .PARAMETER Gateway
         Gateway of the New Org Network as IP Address

    .PARAMETER IPRangeStart
        IP Range Start of the New Org Network as IP Address

    .PARAMETER IPRangeEnd
         IP Range End of the New Org Network as IP Address

    .PARAMETER Shared
         Switch for Shared OrgVDC Network

         Default: $True

    .PARAMETER Timeout
        Timeout for the Org Network to become Ready

        Default: 120s

    #>
        Param (
            [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="Name of the New Org Network as String")]
            [ValidateNotNullorEmpty()]
                [String] $Name,
            [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="OrgVDC where the new Org Network should be created as string")]
            [ValidateNotNullorEmpty()]
                [String] $OrgVdcName,
            [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="Org where the new Org Network should be created as string")]
            [ValidateNotNullorEmpty()]
                [String] $OrgName,
            [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="Edge Gateway Name for the new Org Network as String")]
            [ValidateNotNullorEmpty()]
                [String] $EdgeName,
            [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="Subnet Mask of the New Org Network as IP Address")]
            [ValidateNotNullorEmpty()]
                [IPAddress] $SubnetMask,
            [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="Gateway of the New Org Network as IP Address")]
            [ValidateNotNullorEmpty()]
                [IPAddress] $Gateway,
            [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="IP Range Start the New Org Network as IP Address")]
            [ValidateNotNullorEmpty()]
                [IPAddress] $IPRangeStart,
            [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="IP Range End the New Org Network as IP Address")]
            [ValidateNotNullorEmpty()]
                [IPAddress] $IPRangeEnd,
            [Parameter(Mandatory=$False, ValueFromPipeline=$False, HelpMessage="Switch for Shared OrgVDC Network")]
            [ValidateNotNullorEmpty()]
                [Bool] $Shared = $True,
            [Parameter(Mandatory=$False, ValueFromPipeline=$False,HelpMessage="Timeout for the Org Network to become Ready")]
            [ValidateNotNullorEmpty()]
                [int] $Timeout = 120
        )
        Process {

        ## Get Org vDC
        Write-Verbose "Get Org vDC"
        [Array] $orgVdc = Get-Org -Name $OrgName | Get-OrgVdc -Name $OrgVdcName

        if ( $orgVdc.Count -gt 1) {
            throw "Multiple OrgVdcs found!"
            }
            elseif ( $orgVdc.Count -lt 1) {
                throw "No OrgVdc found!"
                }
        $orgVdcView = $orgVdc| Get-CIView

        ## Get EdgeGateway
        Write-Verbose "Get EdgeGateway"
        [Array] $edgeGateway = Search-Cloud -QueryType EdgeGateway -Name $EdgeName | Get-CIView
        if ( $edgeGateway.Count -gt 1) {
            throw "Multiple EdgeGateways found!"
            }
            elseif ( $edgeGateway.Count -lt 1) {
                throw "No EdgeGateway found!"
                }

        ## Define Org Network
        Write-Verbose "Define Org Network"
        $OrgNetwork = new-object vmware.vimautomation.cloud.views.orgvdcnetwork
        $OrgNetwork.name = $Name
        $OrgNetwork.edgegateway = $edgeGateway.id
        $OrgNetwork.isshared = $Shared

        $OrgNetwork.configuration = new-object vmware.vimautomation.cloud.views.networkconfiguration
        $OrgNetwork.configuration.fencemode = "natRouted"
        $OrgNetwork.configuration.ipscopes = new-object vmware.vimautomation.cloud.views.ipscopes

        $Scope = new-object vmware.vimautomation.cloud.views.ipScope
        $Scope.gateway = $Gateway
        $Scope.netmask = $SubnetMask

        $Scope.ipranges = new-object vmware.vimautomation.cloud.views.ipranges
        $Scope.ipranges.iprange = new-object vmware.vimautomation.cloud.views.iprange
        $Scope.ipranges.iprange[0].startaddress = $IPRangeStart
        $Scope.ipranges.iprange[0].endaddress = $IPRangeEnd

        $OrgNetwork.configuration.ipscopes.ipscope += $Scope

        ## Create Org Network
        Write-Verbose "Create Org Network"
        $CreateOrgNetwork = $orgVdcView.CreateNetwork($OrgNetwork)

        ## Wait for Org Network to become Ready
        Write-Verbose "Wait for Org Network to become Ready"
        while(!(Get-OrgVdcNetwork -Id $CreateOrgNetwork.Id -ErrorAction SilentlyContinue)){
            $i++
            Start-Sleep 5
            if($i -gt $Timeout) { Write-Error "Creating Org Network."; break}
            Write-Progress -Activity "Creating Org Network" -Status "Wait for Network to become Ready..."
        }
        Write-Progress -Activity "Creating Org Network" -Completed
        Start-Sleep 1

        Get-OrgVdcNetwork -Id $CreateOrgNetwork.Id | Select-Object Name, OrgVdc, NetworkType, DefaultGateway, Netmask, StaticIPPool, @{ N='isShared'; E = {$_.ExtensionData.isShared} } | Format-Table -AutoSize

        }
    }

Functions like these, with a lot of parameters are usually a bit awkward to execute. This method is a little bit more comfortable:

$params = @{ 'Name' = '<String>';  
             'OrgVdcName' = '<String>';  
             'OrgName' = '<String>';  
             'EdgeName' = '<String>';  
             'SubnetMask' = '255.255.255.0';  
             'Gateway' = '192.168.66.1';  
             'IPRangeStart' = '192.168.66.100';  
             'IPRangeEnd' = '192.168.66.200'  
             }  
New-MyOrgNetwork @params -Verbose

Modify vCloud Director Org Netzwerk

In another community diskussion, the topic was how to modify these networks with VMware PowerCLI. I can clearly recommend the use of the UpdateServerData() method to modify existing networks:

C:\> Get-OrgVdcNetwork -Name Test | Select-Object Name, DefaultGateway, Description  
  
  
Name DefaultGateway Description  
---- -------------- -----------  
Test 192.168.66.1 

C:\> $OrgNetView = Get-OrgVdcNetwork -Name Test |  Get-CIView  
C:\> $OrgNetView.Description = "Test Network"  
C:\> $OrgNetView.UpdateServerData()  
C:\> Get-OrgVdcNetwork -Name Test | Select-Object Name, DefaultGateway, Description  
  
  
Name DefaultGateway Description  
---- -------------- -----------  
Test 192.168.66.1   Test Network

Published on PowerShell Gallery

Within this update of the vCloud Director PowerShell Module I have published it on PowerShell Gallery.

Create routed vCloud Director Org Network - Find-Module

VMware-vCD-Module

Share with:

Leave a Reply