|
| 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 Ensure-Login () |
| 12 | +{ |
| 13 | + $context = Get-AzureRmContext |
| 14 | + If($context.Subscription -eq $null) |
| 15 | + { |
| 16 | + Write-Host "Loging in ..." |
| 17 | + If((Login-AzureRmAccount -ErrorAction SilentlyContinue -ErrorVariable Errors) -eq $null) |
| 18 | + { |
| 19 | + Write-Host ("Login failed: {0}" -f $Errors[0].Exception.Message) -ForegroundColor Red |
| 20 | + Break |
| 21 | + } |
| 22 | + } |
| 23 | + Write-Host "User logedin." -ForegroundColor Green |
| 24 | +} |
| 25 | + |
| 26 | +function Select-SubscriptionId { |
| 27 | + param ( |
| 28 | + $subscriptionId |
| 29 | + ) |
| 30 | + Write-Host "Selecting subscription '$subscriptionId'." |
| 31 | + $context = Get-AzureRmContext |
| 32 | + If($context.Subscription.Id -ne $subscriptionId) |
| 33 | + { |
| 34 | + Try |
| 35 | + { |
| 36 | + Select-AzureRmSubscription -SubscriptionId $subscriptionId -ErrorAction Stop | Out-null |
| 37 | + } |
| 38 | + Catch |
| 39 | + { |
| 40 | + Write-Host "Subscription selection failed: $_" -ForegroundColor Red |
| 41 | + Break |
| 42 | + } |
| 43 | + } |
| 44 | + Write-Host "Subscription selected." -ForegroundColor Green |
| 45 | +} |
| 46 | + |
| 47 | +function Load-VirtualNetwork { |
| 48 | + param ( |
| 49 | + $resourceGroupName, |
| 50 | + $virtualNetworkName |
| 51 | + ) |
| 52 | + Write-Host("Loading virtual network '{0}' in resource group '{1}'." -f $virtualNetworkName, $resourceGroupName) |
| 53 | + $virtualNetwork = Get-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName -Name $virtualNetworkName -ErrorAction SilentlyContinue |
| 54 | + If($virtualNetwork.Id -ne $null) |
| 55 | + { |
| 56 | + Write-Host "Virtual network loaded." -ForegroundColor Green |
| 57 | + return $virtualNetwork |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + Write-Host "Virtual network not found." -ForegroundColor Red |
| 62 | + Break |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function Load-ResourceGroup { |
| 67 | + param ( |
| 68 | + $resourceGroupName |
| 69 | + ) |
| 70 | + Write-Host("Loading resource group '{0}'." -f $resourceGroupName) |
| 71 | + $resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName |
| 72 | + If($resourceGroup.ResourceId -ne $null) |
| 73 | + { |
| 74 | + Write-Host "Resource group loaded." -ForegroundColor Green |
| 75 | + return $resourceGroup |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + Write-Host "Resource group not found." -ForegroundColor Red |
| 80 | + Break |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function ConvertCidrToUint32Array |
| 85 | +{ |
| 86 | + param($cidrRange) |
| 87 | + $cidrRangeParts = $cidrRange.Split(@(".","/")) |
| 88 | + $ipnum = ([Convert]::ToUInt32($cidrRangeParts[0]) -shl 24) -bor ` |
| 89 | + ([Convert]::ToUInt32($cidrRangeParts[1]) -shl 16) -bor ` |
| 90 | + ([Convert]::ToUInt32($cidrRangeParts[2]) -shl 8) -bor ` |
| 91 | + [Convert]::ToUInt32($cidrRangeParts[3]) |
| 92 | + |
| 93 | + $maskbits = [System.Convert]::ToInt32($cidrRangeParts[4]) |
| 94 | + $mask = 0xffffffff |
| 95 | + $mask = $mask -shl (32 -$maskbits) |
| 96 | + $ipstart = $ipnum -band $mask |
| 97 | + $ipend = $ipnum -bor ($mask -bxor 0xffffffff) |
| 98 | + return @($ipstart, $ipend) |
| 99 | +} |
| 100 | + |
| 101 | +function ConvertUInt32ToIPAddress |
| 102 | +{ |
| 103 | + param($uint32IP) |
| 104 | + $v1 = $uint32IP -band 0xff |
| 105 | + $v2 = ($uint32IP -shr 8) -band 0xff |
| 106 | + $v3 = ($uint32IP -shr 16) -band 0xff |
| 107 | + $v4 = ($uint32IP -shr 24) |
| 108 | + return "$v4.$v3.$v2.$v1" |
| 109 | +} |
| 110 | + |
| 111 | +function CalculateNextAddressPrefix |
| 112 | +{ |
| 113 | + param($virtualNetwork, $prefixLength) |
| 114 | + Write-Host "Calculating address prefix." |
| 115 | + $startIPAddress = 0 |
| 116 | + ForEach($addressPrefix in $virtualNetwork.AddressSpace.AddressPrefixes) |
| 117 | + { |
| 118 | + $endIPAddress = (ConvertCidrToUint32Array $addressPrefix)[1] |
| 119 | + If($endIPAddress -gt $startIPAddress) |
| 120 | + { |
| 121 | + $startIPAddress = $endIPAddress |
| 122 | + } |
| 123 | + } |
| 124 | + $startIPAddress += 1 |
| 125 | + return (ConvertUInt32ToIPAddress $startIPAddress) + "/" + $prefixLength |
| 126 | +} |
| 127 | + |
| 128 | +function CalculateVpnClientAddressPoolPrefix |
| 129 | +{ |
| 130 | + param($gatewaySubnetPrefix) |
| 131 | + Write-Host "Calculating VPN client address pool prefix." |
| 132 | + If($gatewaySubnetPrefix.StartsWith("10.")) |
| 133 | + { |
| 134 | + return "192.168.0.0/24" |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + return "172.16.0.0/24" |
| 139 | + } |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +Ensure-Login |
| 144 | +Select-SubscriptionId -subscriptionId $subscriptionId |
| 145 | + |
| 146 | +$virtualNetwork = Load-VirtualNetwork -resourceGroupName $resourceGroupName -virtualNetworkName $virtualNetworkName |
| 147 | + |
| 148 | +$resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName |
| 149 | + |
| 150 | +$certificate = New-SelfSignedCertificate -Type Custom -KeySpec Signature ` |
| 151 | + -Subject ("CN=$certificateNamePrefix"+"P2SRoot") -KeyExportPolicy Exportable ` |
| 152 | + -HashAlgorithm sha256 -KeyLength 2048 ` |
| 153 | + -CertStoreLocation "Cert:\CurrentUser\My" -KeyUsageProperty Sign -KeyUsage CertSign |
| 154 | + |
| 155 | +$certificateThumbprint = $certificate.Thumbprint |
| 156 | + |
| 157 | +New-SelfSignedCertificate -Type Custom -DnsName ($certificateNamePrefix+"P2SChild") -KeySpec Signature ` |
| 158 | + -Subject ("CN=$certificateNamePrefix"+"P2SChild") -KeyExportPolicy Exportable ` |
| 159 | + -HashAlgorithm sha256 -KeyLength 2048 ` |
| 160 | + -CertStoreLocation "Cert:\CurrentUser\My" ` |
| 161 | + -Signer $certificate -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2") | Out-null |
| 162 | + |
| 163 | +$publicRootCertData = [Convert]::ToBase64String((Get-Item cert:\currentuser\my\$certificateThumbprint).RawData) |
| 164 | + |
| 165 | +$gatewaySubnetPrefix = CalculateNextAddressPrefix $virtualNetwork 28 |
| 166 | + |
| 167 | +$vpnClientAddressPoolPrefix = CalculateVpnClientAddressPoolPrefix $gatewaySubnetPrefix |
| 168 | + |
| 169 | +Write-Host |
| 170 | + |
| 171 | +# Start the deployment |
| 172 | +Write-Host "Starting deployment..." |
| 173 | + |
| 174 | +$templateParameters = @{ |
| 175 | + virtualNetworkName = $virtualNetworkName |
| 176 | + gatewaySubnetPrefix = $gatewaySubnetPrefix |
| 177 | + vpnClientAddressPoolPrefix = $vpnClientAddressPoolPrefix |
| 178 | + publicRootCertData = $publicRootCertData |
| 179 | + } |
| 180 | + |
| 181 | +New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri ($scriptUrlBase+'/azuredeploy.json') -TemplateParameterObject $templateParameters |
0 commit comments