-
Notifications
You must be signed in to change notification settings - Fork 7
/
04_03_Deploy-Networking.ps1
63 lines (52 loc) · 2.27 KB
/
04_03_Deploy-Networking.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
#Define Deployment Variables
$appNamePrefix = 'tws'
$locationDetails = Get-Content -Path './locationDetails.json' | ConvertFrom-Json
#Deploy Virtual Network Gateways
foreach ($i in $locationDetails) {
$location = $i.Location
$vNetName = "$appNamePrefix-vnet-$location"
$resourceGroupName = "$appNamePrefix-iaas-$location"
$vNet = Get-AzVirtualNetwork -Name $vNetName -ResourceGroupName $resourceGroupName
$pipName = "$appNamePrefix-pip-$location"
$pip = New-AzPublicIpAddress `
-Name $pipName `
-ResourceGroupName $resourceGroupName `
-Location $location `
-AllocationMethod Dynamic `
-Force
$gwSubnet = Get-AzVirtualNetworkSubnetConfig -Name 'GatewaySubnet' -VirtualNetwork $vNet
$gwConfigName = "$appNamePrefix-gw-$location-config"
$gwIpConfig = New-AzVirtualNetworkGatewayIpConfig `
-Name $gwConfigName `
-Subnet $gwSubnet `
-PublicIpAddress $pip
$gwName = "$appNamePrefix-gw-$location"
New-AzVirtualNetworkGateway `
-Name $gwName `
-ResourceGroupName $resourceGroupName `
-Location $location `
-IpConfigurations $gwIpConfig `
-GatewayType Vpn `
-VpnType RouteBased `
-GatewaySku VpnGw1
}
#Establish VNet-to-VNet Connection
foreach ($i in $locationDetails) {
$location = $i.Location
$resourceGroupName = "$appNamePrefix-iaas-$location"
$localGateway = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroupName
$targetGatewayResource = Get-AzResource | Where-Object {$_.Type -eq 'Microsoft.Network/virtualNetworkGateways' -and $_.ResourceGroupName -ne $resourceGroupName}
$targetGatewayName = $targetGatewayResource.Name
$targetGatewayRG = $targetGatewayResource.ResourceGroupName
$targetGateway = Get-AzVirtualNetworkGateway -Name $targetGatewayName -ResourceGroupName $targetGatewayRG
$sharedKey = 'AzureVPN123'
$connectionName = $localGateway.Name + '-to-' + $targetGateway.Name
New-AzVirtualNetworkGatewayConnection `
-Name $connectionName `
-ResourceGroupName $resourceGroupName `
-Location $location `
-VirtualNetworkGateway1 $localGateway `
-VirtualNetworkGateway2 $targetGateway `
-ConnectionType Vnet2Vnet `
-SharedKey $sharedKey
}