Skip to content

Instantly share code, notes, and snippets.

@tom-henderson
Created July 31, 2016 04:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tom-henderson/7468f5bc1b5d90305dd7f120200a6088 to your computer and use it in GitHub Desktop.
Save tom-henderson/7468f5bc1b5d90305dd7f120200a6088 to your computer and use it in GitHub Desktop.
# Create the Azure end of an Azure Site to Site VPN
$subscription = 'Visual Studio Enterprise'
$location = 'Australia East'
$resourceGroup = 'RG-Network'
$networkName = 'VN-Azure'
$networkPrefix = '10.20.0.0/16'
$localGatewayName = 'GW-Local'
$localGatewayIP = '123.123.123.123' # Your external IP
$localGatewayPrefix = '10.0.0.0/16' # Your internal network
$remoteGatewayName = 'GW-Azure'
$remoteGatewayIPAddressName = 'GW-Azure-IP'
$sharedKey = 'change_this_to_something_secret'
# 1. Connect to Azure
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName $subscription
# 2. Create a resource group
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
# 3. Create subnets for the remote network
# The gateway must be named 'GatewaySubnet'
$gatewaySubnet = New-AzureRmVirtualNetworkSubnetConfig -Name 'GatewaySubnet' -AddressPrefix '10.20.255.0/28'
$azureSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name 'AzureSubnet' -AddressPrefix '10.20.0.0/24'
# 4. Create a virtual network and add subnets
$remoteNetwork = New-AzureRmVirtualNetwork -Name $networkName -ResourceGroupName $resourceGroup -Location $location -AddressPrefix $networkPrefix -Subnet $gatewaySubnet, $azureSubnet
# 5. Create a local network gateway
$localGateway = New-AzureRmLocalNetworkGateway -Name $localGatewayName -ResourceGroupName $resourceGroup -Location $location -GatewayIpAddress $localGatewayIP -AddressPrefix $localGatewayPrefix
# 6. Request a public IP address for the VPN gateway
$remoteGatewayIPAddress = New-AzureRmPublicIpAddress -Name $remoteGatewayIPAddressName -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic
# 7. Create the gateway IP addressing configuration
$remoteGatewaySubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name 'GatewaySubnet' -VirtualNetwork $remoteNetwork
$remoteGatewayIpConfig = New-AzureRmVirtualNetworkGatewayIpConfig -Name $remoteGatewayIPAddressName -SubnetId $remoteGatewaySubnet.Id -PublicIpAddressId $remoteGatewayIPAddress.Id
# 8. Create the virtual network gateway
# Note this may take 20 mins or more to return
$remoteGateway = New-AzureRmVirtualNetworkGateway -Name $remoteGatewayName -ResourceGroupName $resourceGroup -Location $location -IpConfigurations $remoteGatewayIpConfig -GatewayType Vpn -VpnType RouteBased -GatewaySku Standard
# 9. Create the VPN connection
New-AzureRmVirtualNetworkGatewayConnection -Name "$remoteGatewayName-$localGatewayName" -ResourceGroupName $resourceGroup -Location $location -VirtualNetworkGateway1 $remoteGateway -LocalNetworkGateway2 $localGateway -ConnectionType IPsec -RoutingWeight 10 -SharedKey $sharedKey
# 10. Get Azure gateway IP address
$remoteGatewayIP = Get-AzureRmPublicIpAddress -Name $remoteGatewayIPAddressName -ResourceGroupName $resourceGroup
Write-Host $remoteGatewayIP.IpAddress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment