Skip to content

Commit c808460

Browse files
committed
[sql db] Az module update
1 parent 9acd707 commit c808460

1 file changed

Lines changed: 217 additions & 0 deletions

File tree

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
$parameters = $args[0]
2+
3+
$subscriptionId = $parameters['subscriptionId']
4+
$resourceGroupName = $parameters['resourceGroupName']
5+
$virtualNetworkName = $parameters['virtualNetworkName']
6+
$certificateNamePrefix = $parameters['certificateNamePrefix']
7+
$force = $parameters['force']
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 Ensure-Login ()
26+
{
27+
$context = Get-AzureRmContext
28+
If($context.Subscription -eq $null)
29+
{
30+
Write-Host "Loging in ..."
31+
If((Connect-AzAccount -ErrorAction SilentlyContinue -ErrorVariable Errors) -eq $null)
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 Select-SubscriptionId {
41+
param (
42+
$subscriptionId
43+
)
44+
Write-Host "Selecting subscription '$subscriptionId'."
45+
$context = Get-AzContext
46+
If($context.Subscription.Id -ne $subscriptionId)
47+
{
48+
Try
49+
{
50+
Select-AzSubscription -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 Load-VirtualNetwork {
62+
param (
63+
$resourceGroupName,
64+
$virtualNetworkName
65+
)
66+
Write-Host("Loading virtual network '{0}' in resource group '{1}'." -f $virtualNetworkName, $resourceGroupName)
67+
$virtualNetwork = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $virtualNetworkName -ErrorAction SilentlyContinue
68+
If($virtualNetwork.Id -ne $null)
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 Load-ResourceGroup {
81+
param (
82+
$resourceGroupName
83+
)
84+
Write-Host("Loading resource group '{0}'." -f $resourceGroupName)
85+
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
86+
If($resourceGroup.ResourceId -ne $null)
87+
{
88+
Write-Host "Resource group loaded." -ForegroundColor Green
89+
return $resourceGroup
90+
}
91+
else
92+
{
93+
Write-Host "Resource group not found." -ForegroundColor Red
94+
Break
95+
}
96+
}
97+
98+
function Set-VirtualNetwork
99+
{
100+
param($virtualNetwork)
101+
102+
Write-Host "Applying changes to the virtual network."
103+
Try
104+
{
105+
Set-AzVirtualNetwork -VirtualNetwork $virtualNetwork -ErrorAction Stop | Out-Null
106+
}
107+
Catch
108+
{
109+
Write-Host "Failed: $_" -ForegroundColor Red
110+
}
111+
112+
}
113+
114+
function ConvertCidrToUint32Array
115+
{
116+
param($cidrRange)
117+
$cidrRangeParts = $cidrRange.Split(@(".","/"))
118+
$ipnum = ([Convert]::ToUInt32($cidrRangeParts[0]) -shl 24) -bor `
119+
([Convert]::ToUInt32($cidrRangeParts[1]) -shl 16) -bor `
120+
([Convert]::ToUInt32($cidrRangeParts[2]) -shl 8) -bor `
121+
[Convert]::ToUInt32($cidrRangeParts[3])
122+
123+
$maskbits = [System.Convert]::ToInt32($cidrRangeParts[4])
124+
$mask = 0xffffffff
125+
$mask = $mask -shl (32 -$maskbits)
126+
$ipstart = $ipnum -band $mask
127+
$ipend = $ipnum -bor ($mask -bxor 0xffffffff)
128+
return @($ipstart, $ipend)
129+
}
130+
131+
function ConvertUInt32ToIPAddress
132+
{
133+
param($uint32IP)
134+
$v1 = $uint32IP -band 0xff
135+
$v2 = ($uint32IP -shr 8) -band 0xff
136+
$v3 = ($uint32IP -shr 16) -band 0xff
137+
$v4 = ($uint32IP -shr 24)
138+
return "$v4.$v3.$v2.$v1"
139+
}
140+
141+
function CalculateNextAddressPrefix
142+
{
143+
param($virtualNetwork, $prefixLength)
144+
Write-Host "Calculating address prefix."
145+
$startIPAddress = 0
146+
ForEach($addressPrefix in $virtualNetwork.AddressSpace.AddressPrefixes)
147+
{
148+
$endIPAddress = (ConvertCidrToUint32Array $addressPrefix)[1]
149+
If($endIPAddress -gt $startIPAddress)
150+
{
151+
$startIPAddress = $endIPAddress
152+
}
153+
}
154+
$startIPAddress += 1
155+
return (ConvertUInt32ToIPAddress $startIPAddress) + "/" + $prefixLength
156+
}
157+
158+
function CalculateVpnClientAddressPoolPrefix
159+
{
160+
param($gatewaySubnetPrefix)
161+
Write-Host "Calculating VPN client address pool prefix."
162+
If($gatewaySubnetPrefix.StartsWith("10."))
163+
{
164+
return "192.168.0.0/24"
165+
}
166+
else
167+
{
168+
return "172.16.0.0/24"
169+
}
170+
171+
}
172+
173+
VerifyPSVersion
174+
Ensure-Login
175+
Select-SubscriptionId -subscriptionId $subscriptionId
176+
177+
$virtualNetwork = Load-VirtualNetwork -resourceGroupName $resourceGroupName -virtualNetworkName $virtualNetworkName
178+
179+
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
180+
181+
$certificate = New-SelfSignedCertificate -Type Custom -KeySpec Signature `
182+
-Subject ("CN=$certificateNamePrefix"+"P2SRoot") -KeyExportPolicy Exportable `
183+
-HashAlgorithm sha256 -KeyLength 2048 `
184+
-CertStoreLocation "Cert:\CurrentUser\My" -KeyUsageProperty Sign -KeyUsage CertSign
185+
186+
$certificateThumbprint = $certificate.Thumbprint
187+
188+
New-SelfSignedCertificate -Type Custom -DnsName ($certificateNamePrefix+"P2SChild") -KeySpec Signature `
189+
-Subject ("CN=$certificateNamePrefix"+"P2SChild") -KeyExportPolicy Exportable `
190+
-HashAlgorithm sha256 -KeyLength 2048 `
191+
-CertStoreLocation "Cert:\CurrentUser\My" `
192+
-Signer $certificate -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2") | Out-null
193+
194+
$publicRootCertData = [Convert]::ToBase64String((Get-Item cert:\currentuser\my\$certificateThumbprint).RawData)
195+
196+
$gatewaySubnetPrefix = CalculateNextAddressPrefix $virtualNetwork 28
197+
198+
$vpnClientAddressPoolPrefix = CalculateVpnClientAddressPoolPrefix $gatewaySubnetPrefix
199+
200+
$virtualNetwork.AddressSpace.AddressPrefixes.Add($gatewaySubnetPrefix)
201+
Add-AzVirtualNetworkSubnetConfig -Name GatewaySubnet -VirtualNetwork $virtualNetwork -AddressPrefix $gatewaySubnetPrefix | Out-Null
202+
203+
Set-VirtualNetwork $virtualNetwork
204+
205+
Write-Host
206+
207+
# Start the deployment
208+
Write-Host "Starting deployment..."
209+
210+
$templateParameters = @{
211+
virtualNetworkName = $virtualNetworkName
212+
gatewaySubnetPrefix = $gatewaySubnetPrefix
213+
vpnClientAddressPoolPrefix = $vpnClientAddressPoolPrefix
214+
publicRootCertData = $publicRootCertData
215+
}
216+
217+
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri ($scriptUrlBase+'/azuredeploy.json?t='+ [DateTime]::Now.Ticks) -TemplateParameterObject $templateParameters

0 commit comments

Comments
 (0)