functions/New-MyExternalNetwork.ps1

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Function New-MyExternalNetwork {
<#
.DESCRIPTION
    Creates a new External Network with Default QSC P3NG Parameters
 
.NOTES
    File Name : New-MyExternalNetwork.ps1
    Author : Markus Kraus
    Version : 1.0
    State : Ready
 
.LINK
    https://mycloudrevolution.com/
 
.EXAMPLE
    $params = @{ 'name' = 'MyTest';
             'vCenterName'='MyVcenter';
             'PortGroupName'='MyTest';
             'SubnetMask' = '255.255.255.0';
             'Gateway' = '192.168.110.1';
             'IPRangeStart' = '192.168.110.100';
             'IPRangeEnd' = '192.168.110.200'
             }
    New-MyExternalNetwork @params
 
.PARAMETER Name
    Name of the New Org Network as String
 
.PARAMETER vCenterName
    Name of the vCenter (vCD Config) as String
 
.PARAMETER PortGroupName
    Name of the DV PortGroup as String
 
.PARAMETER SubnetMask
     Subnet Mask of the New External Network as IP Address
 
.PARAMETER Gateway
     Gateway of the New External Network as IP Address
 
.PARAMETER IPRangeStart
    IP Range Start of the New External Network as IP Address
 
.PARAMETER IPRangeEnd
     IP Range End of the New External Network as IP Address
 
.PARAMETER Timeout
    Timeout for the External Network to become Ready
 
    Default: 120s
 
#>

    Param (
        [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="Name of the New External Network as String")]
        [ValidateNotNullorEmpty()]
            [String] $Name,
        [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="Name of the vCenter (vCD Config) as String")]
        [ValidateNotNullorEmpty()]
            [String] $vCenterName,
        [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="Name of the DV PortGroup as String")]
        [ValidateNotNullorEmpty()]
            [String] $PortGroupName,
        [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="Subnet Mask of the New External Network as IP Address")]
        [ValidateNotNullorEmpty()]
            [IPAddress] $SubnetMask,
        [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="Gateway of the New External Network as IP Address")]
        [ValidateNotNullorEmpty()]
            [IPAddress] $Gateway,
        [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="IP Range Start the New External Network as IP Address")]
        [ValidateNotNullorEmpty()]
            [IPAddress] $IPRangeStart,
        [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage="IP Range End the New External Network as IP Address")]
        [ValidateNotNullorEmpty()]
            [IPAddress] $IPRangeEnd,
        [Parameter(Mandatory=$False, ValueFromPipeline=$False,HelpMessage="Timeout for the External Network to become Ready")]
        [ValidateNotNullorEmpty()]
            [int] $Timeout = 120
    )

    Process {

    ## Get vCenter
    Write-Verbose "Get vCenter Reference"
    [Array]$vCenterRef = Search-Cloud VirtualCenter | Get-CIView | Where-Object {$_.name -eq $vCenterName}

    if ( $vCenterRef.Count -gt 1) {
        throw "Multiple vCenters found!"
        }
        elseif ( $vCenterRef.Count -lt 1) {
            throw "No vCenter found!"
            }

    Write-Verbose "Get PortGroup Reference"
    [Array]$PortGroup = Get-View -ViewType DistributedVirtualPortGroup | Where-Object {$_.name -like $PortGroupName}

    if ( $PortGroup.Count -gt 1) {
        throw "Multiple PortGroups found!"
        }
        elseif ( $PortGroup.Count -lt 1) {
            throw "No PortGroup found!"
            }

    ## Define External Network
    Write-Verbose "Define External Network"
    $ExternalNetwork = new-object vmware.vimautomation.cloud.views.VMWExternalNetwork
    $ExternalNetwork.Name = $Name
    $ExternalNetwork.VimPortGroupRef = new-object VMware.VimAutomation.Cloud.Views.VimObjectRef
    $ExternalNetwork.VimPortGroupRef.MoRef = $PortGroup.key
    $ExternalNetwork.VimPortGroupRef.VimObjectType = "DV_PORTGROUP"

    $ExternalNetwork.VimPortGroupRef.VimServerRef = new-object VMware.VimAutomation.Cloud.Views.Reference
    $ExternalNetwork.VimPortGroupRef.VimServerRef.href = $vCenterRef.href
    $ExternalNetwork.VimPortGroupRef.VimServerRef.type = "application/vnd.vmware.admin.vmwvirtualcenter+xml"

    $ExternalNetwork.Configuration = new-object VMware.VimAutomation.Cloud.Views.NetworkConfiguration
    $ExternalNetwork.configuration.fencemode = "isolated"

    $ExternalNetwork.Configuration.IpScopes = new-object VMware.VimAutomation.Cloud.Views.IpScopes
    $ExternalNetwork.Configuration.IpScopes.IpScope = new-object VMware.VimAutomation.Cloud.Views.IpScope
    $ExternalNetwork.Configuration.IpScopes.ipscope[0].Gateway = $Gateway
    $ExternalNetwork.Configuration.IpScopes.ipscope[0].Netmask = $SubnetMask
    $ExternalNetwork.Configuration.IpScopes.ipscope[0].IsInherited = "False"

    $ExternalNetwork.Configuration.IpScopes.ipscope[0].ipranges = new-object vmware.vimautomation.cloud.views.ipranges
    $ExternalNetwork.Configuration.Ipscopes.ipscope[0].ipranges.iprange = new-object vmware.vimautomation.cloud.views.iprange
    $ExternalNetwork.Configuration.IpScopes.ipscope[0].IpRanges.IpRange[0].startaddress = $IPRangeStart
    $ExternalNetwork.Configuration.IpScopes.ipscope[0].IpRanges.IpRange[0].endaddress = $IPRangeEnd

    ## Create External Network
    Write-Verbose "Create External Network"
    $vCloud = $DefaultCIServers[0].ExtensionData
    $Admin = $vCloud.GetAdmin()
    $Ext = $Admin.GetExtension()
    $CreateExternalNetwork = $Ext.CreateExternalNet($ExternalNetwork)

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

    Get-ExternalNetwork -Id $CreateExternalNetwork.Id | Select-Object Name, VlanId, Gateway, UsedIpCount, TotalIpCount  | Format-Table -AutoSize

    }
}