BrownField/Networking/AVS-to-VNet-ExistingVNet/PowerShell/Deploy-ExRConnection-GenerateAuthKey.ps1 (32 lines of code) (raw):
# Parameters for deployment
$privateCloudName = "<Existing Private Cloud name>"
$PrivateCloudResourceGroup = "<Resource Group name of the existing Private Cloud>"
$GatewayName = "<Name for the existing VNet Gateway>"
$GatewayResourceGroup = "<Resource Group name of the existing VNet Gateway>"
$location = ""
$ConnectionName = "$privateCloudNameer-ExR-Connection"
# Get Private Cloud and create ExR authorisation key, must be a circuit owner
$privatecloud = Get-AzVMwarePrivateCloud -Name $privateCloudName -ResourceGroupName $PrivateCloudResourceGroup
$peerid = $privatecloud.CircuitExpressRouteId
Write-Host -ForegroundColor Yellow "Generating Authorization Key"
$exrauthkey = New-AzVMwareAuthorization -Name "$privateCloudName-authkey" -PrivateCloudName $privatecloud.name -ResourceGroupName $PrivateCloudResourceGroup
$exrgwtouse = Get-AzVirtualNetworkGateway -Name $GatewayName -ResourceGroupName $GatewayResourceGroup
# Create AVS Private Cloud connection to ExpressRoute
Write-Host -ForegroundColor Yellow "Connecting AVS Private Cloud $privateCloudName to Azure Virtual Network Gateway "$exrgwtouse.name""
New-AzVirtualNetworkGatewayConnection -Name $ConnectionName -ResourceGroupName $GatewayResourceGroup -Location $location -VirtualNetworkGateway1 $exrgwtouse -PeerId $peerid -ConnectionType ExpressRoute -AuthorizationKey $exrauthkey.Key
# Check ExpressRoute connection status
while ($currentprovisioningstate -ne "Succeeded")
{
$timeStamp = Get-Date -Format "hh:mm"
"$timestamp - Current Status: $currentprovisioningstate "
Start-Sleep -Seconds 20
$provisioningstate = Get-AzVirtualNetworkGatewayConnection -Name $ConnectionName -ResourceGroupName $GatewayResourceGroup
$currentprovisioningstate = $provisioningstate.ProvisioningState
}
if ($currentprovisioningstate -eq "Succeeded")
{
Write-host -ForegroundColor Green "Connection was created successfully"
}
else {
Write-host -ForegroundColor Red "Connection was not created succesfully"
}