11$parameters = $args [0 ]
2+ $scriptUrlBase = $args [1 ]
23
34$subscriptionId = $parameters [' subscriptionId' ]
45$resourceGroupName = $parameters [' resourceGroupName' ]
56$virtualMachineName = $parameters [' virtualMachineName' ]
67$virtualNetworkName = $parameters [' virtualNetworkName' ]
78$managementSubnetName = $parameters [' subnetName' ]
8- $administratorLogin = $parameters [' administratorLogin' ]
9- $administratorLoginPassword = $parameters [' administratorLoginPassword' ]
10-
11- $scriptUrlBase = $args [1 ]
9+ $administratorLogin = $parameters [' administratorLogin' ]
10+ $administratorLoginPassword = $parameters [' administratorLoginPassword' ]
1211
13- if ($virtualMachineName -eq ' ' -or $virtualMachineName -eq $null ) {
12+ if ($virtualMachineName -eq ' ' -or ( $null -eq $virtualMachineName ) ) {
1413 $virtualMachineName = ' Jumpbox'
1514 Write-Host " VM Name: 'Jumpbox'." - ForegroundColor Green
1615}
1716
18- if ($managementSubnetName -eq ' ' -or $managementSubnetName -eq $null ) {
17+ if ($managementSubnetName -eq ' ' -or ( $null -eq $managementSubnetName ) ) {
1918 $managementSubnetName = ' Management'
2019 Write-Host " Using subnet 'Management' to deploy jumpbox VM." - ForegroundColor Green
2120}
2221
23- function VerifyPSVersion
24- {
25- Write-Host " Verifying PowerShell version, must be 5.0 or higher."
26- if ($PSVersionTable.PSVersion.Major -ge 5 )
27- {
28- Write-Host " PowerShell version verified." - ForegroundColor Green
22+ function VerifyPSVersion {
23+ Write-Host " Verifying PowerShell version."
24+ if ($PSVersionTable.PSEdition -eq " Desktop" ) {
25+ if (($PSVersionTable.PSVersion.Major -ge 6 ) -or
26+ (($PSVersionTable.PSVersion.Major -eq 5 ) -and ($PSVersionTable.PSVersion.Minor -ge 1 ))) {
27+ Write-Host " PowerShell version verified." - ForegroundColor Green
28+ }
29+ else {
30+ Write-Host " You need to install PowerShell version 5.1 or heigher." - ForegroundColor Red
31+ Break ;
32+ }
33+ }
34+ else {
35+ if ($PSVersionTable.PSVersion.Major -ge 6 ) {
36+ Write-Host " PowerShell version verified." - ForegroundColor Green
37+ }
38+ else {
39+ Write-Host " You need to install PowerShell version 6.0 or heigher." - ForegroundColor Red
40+ Break ;
41+ }
2942 }
30- else
31- {
32- Write-Host " You need to install PowerShell version 5.0 or heigher." - ForegroundColor Red
33- Break ;
43+ }
44+
45+ function EnsureAzModule {
46+ Write-Host " Checking if Az module is imported."
47+ $module = Get-Module Az
48+ If ($null -eq $module ) {
49+ try {
50+ Import-Module Az - ErrorAction Stop
51+ Write-Host " Module Az imported." - ForegroundColor Green
52+ }
53+ catch {
54+ Install-Module Az - AllowClobber
55+ Write-Host " Module Az installed." - ForegroundColor Green
56+ }
57+ } else {
58+ Write-Host " Module Az imported." - ForegroundColor Green
3459 }
3560}
3661
37- function EnsureLogin ()
38- {
39- $context = Get-AzureRmContext
40- If ($null -eq $context.Subscription )
41- {
62+ function EnsureLogin () {
63+ $context = Get-AzContext
64+ If ($null -eq $context.Subscription ) {
4265 Write-Host " Sign-in..."
43- If ($null -eq (Login- AzureRmAccount - ErrorAction SilentlyContinue - ErrorVariable Errors))
44- {
66+ If ($null -eq (Connect-AzAccount - ErrorAction SilentlyContinue - ErrorVariable Errors)) {
4567 Write-Host (" Sign-in failed: {0}" -f $Errors [0 ].Exception.Message) - ForegroundColor Red
4668 Break
4769 }
@@ -54,16 +76,14 @@ function SelectSubscriptionId {
5476 $subscriptionId
5577 )
5678 Write-Host " Selecting subscription '$subscriptionId '..."
57- $context = Get-AzureRmContext
58- If ($context.Subscription.Id -ne $subscriptionId )
59- {
60- Try
61- {
62- Write-Host " Switching subscription $context .Subscription.Id to '$subscriptionId '." - ForegroundColor Green
63- Select-AzureRmSubscription - SubscriptionId $subscriptionId - ErrorAction Stop | Out-null
79+ $context = Get-AzContext
80+ If ($context.Subscription.Id -ne $subscriptionId ) {
81+ Try {
82+ $currentSubscriptionId = $context.Subscription.Id
83+ Write-Host " Switching subscription $currentSubscriptionId to '$subscriptionId '." - ForegroundColor Green
84+ Select-AzSubscription - SubscriptionId $subscriptionId - ErrorAction Stop | Out-null
6485 }
65- Catch
66- {
86+ Catch {
6787 Write-Host " Subscription selection failed: $_ " - ForegroundColor Red
6888 Break
6989 }
@@ -76,59 +96,52 @@ function LoadVirtualNetwork {
7696 $resourceGroupName ,
7797 $virtualNetworkName
7898 )
79- Write-Host (" Loading virtual network '{0}' in resource group '{1}'." -f $virtualNetworkName , $resourceGroupName )
80- $virtualNetwork = Get-AzureRmVirtualNetwork - ResourceGroupName $resourceGroupName - Name $virtualNetworkName - ErrorAction SilentlyContinue
81- $id = $virtualNetwork.Id
82- If ($null -ne $id )
83- {
84- Write-Host " Virtual network with id $id is loaded." - ForegroundColor Green
85- If ($virtualNetwork.VirtualNetworkPeerings.Count -gt 0 ) {
86- Write-Host " Virtual network is loaded, but it should not have peerings." - ForegroundColor Red
87- }
88- return $virtualNetwork
89- }
90- else
91- {
92- Write-Host " Virtual network $virtualNetworkName cannot be found." - ForegroundColor Red
93- Break
99+ Write-Host (" Loading virtual network '{0}' in resource group '{1}'." -f $virtualNetworkName , $resourceGroupName )
100+ $virtualNetwork = Get-AzVirtualNetwork - ResourceGroupName $resourceGroupName - Name $virtualNetworkName - ErrorAction SilentlyContinue
101+ $id = $virtualNetwork.Id
102+ If ($null -ne $id ) {
103+ Write-Host " Virtual network with id $id is loaded." - ForegroundColor Green
104+ If ($virtualNetwork.VirtualNetworkPeerings.Count -gt 0 ) {
105+ Write-Host " Virtual network is loaded, but it should not have peerings." - ForegroundColor Red
94106 }
107+ return $virtualNetwork
108+ }
109+ else {
110+ Write-Host " Virtual network $virtualNetworkName cannot be found." - ForegroundColor Red
111+ Break
112+ }
95113}
96114
97- function SetVirtualNetwork
98- {
115+ function SetVirtualNetwork {
99116 param ($virtualNetwork )
100117
101118 Write-Host " Applying changes to the virtual network."
102- Try
103- {
104- Set-AzureRmVirtualNetwork - VirtualNetwork $virtualNetwork - ErrorAction Stop | Out-Null
119+ Try {
120+ Set-AzVirtualNetwork - VirtualNetwork $virtualNetwork - ErrorAction Stop | Out-Null
105121 }
106- Catch
107- {
122+ Catch {
108123 Write-Host " Failed to configure Virtual Network: $_ " - ForegroundColor Red
109124 }
110125}
111126
112- function ConvertCidrToUint32Array
113- {
127+ function ConvertCidrToUint32Array {
114128 param ($cidrRange )
115129 $cidrRangeParts = $cidrRange.Split (" /" )
116130 $ipParts = $cidrRangeParts [0 ].Split(" ." )
117131 $ipnum = ([Convert ]::ToUInt32($ipParts [0 ]) -shl 24 ) -bor `
118- ([Convert ]::ToUInt32($ipParts [1 ]) -shl 16 ) -bor `
119- ([Convert ]::ToUInt32($ipParts [2 ]) -shl 8 ) -bor `
120- [Convert ]::ToUInt32($ipParts [3 ])
132+ ([Convert ]::ToUInt32($ipParts [1 ]) -shl 16 ) -bor `
133+ ([Convert ]::ToUInt32($ipParts [2 ]) -shl 8 ) -bor `
134+ [Convert ]::ToUInt32($ipParts [3 ])
121135
122136 $maskbits = [System.Convert ]::ToInt32($cidrRangeParts [1 ])
123137 $mask = 0xffffffff
124- $mask = $mask -shl (32 - $maskbits )
138+ $mask = $mask -shl (32 - $maskbits )
125139 $ipstart = $ipnum -band $mask
126140 $ipend = $ipnum -bor ($mask -bxor 0xffffffff )
127141 return @ ($ipstart , $ipend )
128142}
129143
130- function ConvertUInt32ToIPAddress
131- {
144+ function ConvertUInt32ToIPAddress {
132145 param ($uint32IP )
133146 $v1 = $uint32IP -band 0xff
134147 $v2 = ($uint32IP -shr 8 ) -band 0xff
@@ -137,16 +150,13 @@ function ConvertUInt32ToIPAddress
137150 return " $v4 .$v3 .$v2 .$v1 "
138151}
139152
140- function CalculateNextAddressPrefix
141- {
153+ function CalculateNextAddressPrefix {
142154 param ($virtualNetwork , $prefixLength )
143155 Write-Host " Calculating address prefix with length $prefixLength ..."
144156 $startIPAddress = 0
145- ForEach ($addressPrefix in $virtualNetwork.AddressSpace.AddressPrefixes )
146- {
157+ ForEach ($addressPrefix in $virtualNetwork.AddressSpace.AddressPrefixes ) {
147158 $endIPAddress = (ConvertCidrToUint32Array $addressPrefix )[1 ]
148- If ($endIPAddress -gt $startIPAddress )
149- {
159+ If ($endIPAddress -gt $startIPAddress ) {
150160 $startIPAddress = $endIPAddress
151161 }
152162 }
@@ -156,57 +166,57 @@ function CalculateNextAddressPrefix
156166 return $addressPrefixResult
157167}
158168
159- function CalculateVpnClientAddressPoolPrefix
160- {
169+ function CalculateVpnClientAddressPoolPrefix {
161170 param ($gatewaySubnetPrefix )
162171 Write-Host " Calculating VPN client address pool prefix."
163- If ($gatewaySubnetPrefix.StartsWith (" 10." ))
164- {
172+ If ($gatewaySubnetPrefix.StartsWith (" 10." )) {
165173 return " 192.168.0.0/24"
166174 }
167- else
168- {
175+ else {
169176 return " 172.16.0.0/24"
170177 }
171178
172179}
173180
174181VerifyPSVersion
182+ EnsureAzModule
175183EnsureLogin
176184SelectSubscriptionId - subscriptionId $subscriptionId
177185
178186$virtualNetwork = LoadVirtualNetwork - resourceGroupName $resourceGroupName - virtualNetworkName $virtualNetworkName
179187
180188$subnets = $virtualNetwork.Subnets.Name
181189
182- If ($false -eq $subnets.Contains ($managementSubnetName ))
183- {
190+ If ($false -eq $subnets.Contains ($managementSubnetName )) {
184191 Write-Host " $managementSubnetName is not one of the subnets in $subnets " - ForegroundColor Yellow
185- Write-Host " Creating subnet $managementSubnetName ($managementSubnetPrefix ) in the VNet..." - ForegroundColor Green
186192 $managementSubnetPrefix = CalculateNextAddressPrefix $virtualNetwork 28
193+ Write-Host " Creating subnet $managementSubnetName ($managementSubnetPrefix ) in the virtual network ..." - ForegroundColor Green
187194
188195 $virtualNetwork.AddressSpace.AddressPrefixes.Add ($managementSubnetPrefix )
189- Add-AzureRmVirtualNetworkSubnetConfig - Name $managementSubnetName - VirtualNetwork $virtualNetwork - AddressPrefix $managementSubnetPrefix | Out-Null
196+ Add-AzVirtualNetworkSubnetConfig - Name $managementSubnetName - VirtualNetwork $virtualNetwork - AddressPrefix $managementSubnetPrefix | Out-Null
190197
191198 SetVirtualNetwork $virtualNetwork
192- Write-Host " Added subnet $managementSubnetName into VNet." - ForegroundColor Green
193- } else {
194- Write-Host " The subnet $managementSubnetName exists in the VNet." - ForegroundColor Green
199+ Write-Host " Added subnet $managementSubnetName into virtual network." - ForegroundColor Green
200+ }
201+ else {
202+ Write-Host " The subnet $managementSubnetName exists in the virtual network." - ForegroundColor Green
195203}
196204
197205Write-Host
198206
199207# Start the deployment
200208Write-Host " Starting deployment..."
209+ Write-Host " Deployment will take about 20m." - ForegroundColor Yellow
201210
202211$templateParameters = @ {
203- virtualNetworkName = $virtualNetworkName
204- managementSubnetName = $managementSubnetName
205- virtualMachineName = $virtualMachineName
206- administratorLogin = $administratorLogin
207- administratorLoginPassword = $administratorLoginPassword
212+ location = $virtualNetwork.Location
213+ virtualNetworkName = $virtualNetworkName
214+ managementSubnetName = $managementSubnetName
215+ virtualMachineName = $virtualMachineName
216+ administratorLogin = $administratorLogin
217+ administratorLoginPassword = $administratorLoginPassword
208218}
209219
210- New-AzureRmResourceGroupDeployment - ResourceGroupName $resourceGroupName - TemplateUri ($scriptUrlBase + ' /azuredeploy.json?t=' + [DateTime ]::Now.Ticks) - TemplateParameterObject $templateParameters
220+ New-AzResourceGroupDeployment - ResourceGroupName $resourceGroupName - TemplateUri ($scriptUrlBase + ' /azuredeploy.json?t=' + [DateTime ]::Now.Ticks) - TemplateParameterObject $templateParameters
211221
212222Write-Host " Deployment completed."
0 commit comments