1- # placeholder
1+ $parameters = $args [0 ]
2+
3+ $subscriptionId = $parameters [' subscriptionId' ]
4+ $resourceGroupName = $parameters [' resourceGroupName' ]
5+ $virtualNetworkName = $parameters [' virtualNetworkName' ]
6+ $administratorLogin = $parameters [' administratorLogin' ]
7+ $administratorLoginPassword = $parameters [' administratorLoginPassword' ]
8+
9+ $scriptUrlBase = $args [1 ]
10+
11+ function VerifyPSVersion
12+ {
13+ Write-Host " Verifying PowerShell version, must be 5.0 or higher."
14+ if ($PSVersionTable.PSVersion.Major -ge 5 )
15+ {
16+ Write-Host " PowerShell version verified." - ForegroundColor Green
17+ }
18+ else
19+ {
20+ Write-Host " You need to install PowerShell version 5.0 or heigher." - ForegroundColor Red
21+ Break ;
22+ }
23+ }
24+
25+ function EnsureLogin ()
26+ {
27+ $context = Get-AzureRmContext
28+ If ($null -eq $context.Subscription )
29+ {
30+ Write-Host " Loging in ..."
31+ If ($null -eq (Login- AzureRmAccount - ErrorAction SilentlyContinue - ErrorVariable Errors))
32+ {
33+ Write-Host (" Login failed: {0}" -f $Errors [0 ].Exception.Message) - ForegroundColor Red
34+ Break
35+ }
36+ }
37+ Write-Host " User logedin." - ForegroundColor Green
38+ }
39+
40+ function SelectSubscriptionId {
41+ param (
42+ $subscriptionId
43+ )
44+ Write-Host " Selecting subscription '$subscriptionId '."
45+ $context = Get-AzureRmContext
46+ If ($context.Subscription.Id -ne $subscriptionId )
47+ {
48+ Try
49+ {
50+ Select-AzureRmSubscription - SubscriptionId $subscriptionId - ErrorAction Stop | Out-null
51+ }
52+ Catch
53+ {
54+ Write-Host " Subscription selection failed: $_ " - ForegroundColor Red
55+ Break
56+ }
57+ }
58+ Write-Host " Subscription selected." - ForegroundColor Green
59+ }
60+
61+ function LoadVirtualNetwork {
62+ param (
63+ $resourceGroupName ,
64+ $virtualNetworkName
65+ )
66+ Write-Host (" Loading virtual network '{0}' in resource group '{1}'." -f $virtualNetworkName , $resourceGroupName )
67+ $virtualNetwork = Get-AzureRmVirtualNetwork - ResourceGroupName $resourceGroupName - Name $virtualNetworkName - ErrorAction SilentlyContinue
68+ If ($null -ne $virtualNetwork.Id )
69+ {
70+ Write-Host " Virtual network loaded." - ForegroundColor Green
71+ return $virtualNetwork
72+ }
73+ else
74+ {
75+ Write-Host " Virtual network not found." - ForegroundColor Red
76+ Break
77+ }
78+ }
79+
80+ function SetVirtualNetwork
81+ {
82+ param ($virtualNetwork )
83+
84+ Write-Host " Applying changes to the virtual network."
85+ Try
86+ {
87+ Set-AzureRmVirtualNetwork - VirtualNetwork $virtualNetwork - ErrorAction Stop | Out-Null
88+ }
89+ Catch
90+ {
91+ Write-Host " Failed: $_ " - ForegroundColor Red
92+ }
93+ }
94+
95+ function ConvertCidrToUint32Array
96+ {
97+ param ($cidrRange )
98+ $cidrRangeParts = $cidrRange.Split (@ (" ." , " /" ))
99+ $ipnum = ([Convert ]::ToUInt32($cidrRangeParts [0 ]) -shl 24 ) -bor `
100+ ([Convert ]::ToUInt32($cidrRangeParts [1 ]) -shl 16 ) -bor `
101+ ([Convert ]::ToUInt32($cidrRangeParts [2 ]) -shl 8 ) -bor `
102+ [Convert ]::ToUInt32($cidrRangeParts [3 ])
103+
104+ $maskbits = [System.Convert ]::ToInt32($cidrRangeParts [4 ])
105+ $mask = 0xffffffff
106+ $mask = $mask -shl (32 - $maskbits )
107+ $ipstart = $ipnum -band $mask
108+ $ipend = $ipnum -bor ($mask -bxor 0xffffffff )
109+ return @ ($ipstart , $ipend )
110+ }
111+
112+ function ConvertUInt32ToIPAddress
113+ {
114+ param ($uint32IP )
115+ $v1 = $uint32IP -band 0xff
116+ $v2 = ($uint32IP -shr 8 ) -band 0xff
117+ $v3 = ($uint32IP -shr 16 ) -band 0xff
118+ $v4 = ($uint32IP -shr 24 )
119+ return " $v4 .$v3 .$v2 .$v1 "
120+ }
121+
122+ function CalculateNextAddressPrefix
123+ {
124+ param ($virtualNetwork , $prefixLength )
125+ Write-Host " Calculating address prefix."
126+ $startIPAddress = 0
127+ ForEach ($addressPrefix in $virtualNetwork.AddressSpace.AddressPrefixes )
128+ {
129+ $endIPAddress = (ConvertCidrToUint32Array $addressPrefix )[1 ]
130+ If ($endIPAddress -gt $startIPAddress )
131+ {
132+ $startIPAddress = $endIPAddress
133+ }
134+ }
135+ $startIPAddress += 1
136+ return (ConvertUInt32ToIPAddress $startIPAddress ) + " /" + $prefixLength
137+ }
138+
139+ function CalculateVpnClientAddressPoolPrefix
140+ {
141+ param ($gatewaySubnetPrefix )
142+ Write-Host " Calculating VPN client address pool prefix."
143+ If ($gatewaySubnetPrefix.StartsWith (" 10." ))
144+ {
145+ return " 192.168.0.0/24"
146+ }
147+ else
148+ {
149+ return " 172.16.0.0/24"
150+ }
151+
152+ }
153+
154+ VerifyPSVersion
155+ EnsureLogin
156+ SelectSubscriptionId - subscriptionId $subscriptionId
157+
158+ $virtualNetwork = LoadVirtualNetwork - resourceGroupName $resourceGroupName - virtualNetworkName $virtualNetworkName
159+
160+ $managementSubnetPrefix = CalculateNextAddressPrefix $virtualNetwork 28
161+
162+ $virtualNetwork.AddressSpace.AddressPrefixes.Add ($managementSubnetPrefix )
163+ Add-AzureRmVirtualNetworkSubnetConfig - Name Management - VirtualNetwork $virtualNetwork - AddressPrefix $managementSubnetPrefix | Out-Null
164+
165+ SetVirtualNetwork $virtualNetwork
166+
167+ Write-Host
168+
169+ # Start the deployment
170+ Write-Host " Starting deployment..."
171+
172+ $templateParameters = @ {
173+ virtualNetworkName = $virtualNetworkName
174+ managementSubnetPrefix = $managementSubnetPrefix
175+ administratorLogin = $administratorLogin
176+ administratorLoginPassword = $administratorLoginPassword
177+ }
178+
179+ New-AzureRmResourceGroupDeployment - ResourceGroupName $resourceGroupName - TemplateUri ($scriptUrlBase + ' /azuredeploy.json?t=' + [DateTime ]::Now.Ticks) - TemplateParameterObject $templateParameters
0 commit comments