使用点到站点连接 (RM Powershell) 将 Azure WebApp 添加到现有 VPN

Add a Azure WebApp to an Existing VPN using a Point-to-Site connection (RM Powershell)

我一直在寻找这个问题的答案,但运气不佳。我能找到的所有文章要么设置点到站点,要么是经典 Azure 的说明,而不是 Azure 2.0(资源组)

目前,我们每次新建时都会拨入一个全新的资源组。这包括 Web 应用程序和 SQL 数据库。当我们有一个新的构建时,我们启动新的并删除旧的资源组。简单的。为了最大限度地减少启动时间,我们有一个未删除的静态资源组,其中包含与我们的 on Prem 资源的 VPN 连接。

我遇到的问题是,当我使用 AzureRM Powershell cmd 将新网站添加到点到站点时,它说成功了。 Azure 门户说它很好,但它确实让我可以交流。如果我从 8 个 WebApps 中的一个中删除并添加它,它们都会开始工作。

我没主意了。任何帮助将不胜感激。

Azure VPN

下面是我根据在那里找到的功能整理的功能。

function AddExistingVnet{
param(
    [string] $subscriptionId,
    [string] $resourceGroupName,
    [string] $webAppName

)

$Vnet = Get-AzureRmVirtualNetwork | Where-Object {$_.ResourceGroupName -like "*Static*"}

IF($Vnet.Name.count -gt 1) {write-host 'Two or networks have been returned. Unable to continue ' return}

    $gatewaySubnet = $vnet.Subnets | Where-Object { $_.Name -eq "GatewaySubnet" }
    $vnetName = $vnet.Name
    $uriParts = $gatewaySubnet.IpConfigurations[0].Id.Split('/')
    $gatewayResourceGroup = $uriParts[4]
    $gatewayName = $uriParts[8]
    $gateway = Get-AzureRmVirtualNetworkGateway -ResourceGroupName $vnet.ResourceGroupName -Name $gatewayName

    Write-Host "Creating App association to VNET"
    $propertiesObject = @{
     "vnetResourceId" = "/subscriptions/$($subscriptionId)/resourceGroups/$($vnet.ResourceGroupName)/providers/Microsoft.Network/virtualNetworks/$($vnetName)"
    }

    $virtualNetwork = New-AzureRmResource -Location $location -Properties $PropertiesObject -ResourceName "$($webAppName)/$($vnet.Name)" -ResourceType "Microsoft.Web/sites/virtualNetworkConnections" -ApiVersion 2015-08-01 -ResourceGroupName $resourceGroupName -Force

# Now finish joining by getting the VPN package and giving it to the App
Write-Host "Retrieving VPN Package and supplying to App"
$packageUri = Get-AzureRmVpnClientPackage -ResourceGroupName $vnet.ResourceGroupName -VirtualNetworkGatewayName $gateway.Name -ProcessorArchitecture Amd64

# Put the VPN client configuration package onto the App
$PropertiesObject = @{
"vnetName" = $vnet.Name; "vpnPackageUri" = $packageUri
}

New-AzureRmResource -Location $location -Properties $PropertiesObject -ResourceName "$($webAppName)/$($vnet.Name)/primary" -ResourceType "Microsoft.Web/sites/virtualNetworkConnections/gateways" -ApiVersion 2015-08-01 -ResourceGroupName $resourceGroupName -WarningAction silentlyContinue -Force 

}  

因此,在与 Microsoft(有一个非常好的人 Charles)来回交流 2 周后,我们设法找到了问题。

请求时

$packageUri = Get-AzureRmVpnClientPackage -ResourceGroupName $vnet.ResourceGroupName -VirtualNetworkGatewayName $gateway.Name -ProcessorArchitecture Amd64

它给我的输出是:

"https://mdsbrketwprodsn1prod.blob.core.windows.net/cmakexe/xxx~xxx/amd64/xxxx~xxxx&sp=r&fileExtension=.exe"

出于某种原因(微软可以解释)为什么它一直在变量的开头和结尾添加 "

我觉得很奇怪,它让脚本与 " 一起工作,并允许 WebApps 加入 VPN。

为什么这里是基本上从 $packageUri 的开头和结尾删除 " 的修复:

$packageUri = $packageUri.ToString(); 
$packageUri = $packageUri.Substring(1, $packageUri.Length-2);

所以希望能帮助那些正在努力解决同样问题的其他人。

如果有人感兴趣,这里是完整的函数:

function AddExistingVnet{
    param(
        [string] $subscriptionId,
        [string] $resourceGroupName,
        [string] $webAppName

    )


$Vnet = Get-AzureRmVirtualNetwork | Where-Object {$_.ResourceGroupName -like "*Static*"}


IF($Vnet.Name.count -gt 1) {write-host 'Two or networks have been returned. Unable to continue ' return}

        $gatewaySubnet = $vnet.Subnets | Where-Object { $_.Name -eq "GatewaySubnet" }
        $vnetName = $vnet.Name
        $uriParts = $gatewaySubnet.IpConfigurations[0].Id.Split('/')
        $gatewayResourceGroup = $uriParts[4]
        $gatewayName = $uriParts[8]
        $gateway = Get-AzureRmVirtualNetworkGateway -ResourceGroupName $vnet.ResourceGroupName -Name $gatewayName

        $webApp = Get-AzureRmResource -ResourceName $webAppName -ResourceType "Microsoft.Web/sites" -ApiVersion 2015-08-01 -ResourceGroupName $resourceGroupName
        $location = $webApp.Location

        Write-Host "Creating App association to VNET"
        $propertiesObject = @{
         "vnetResourceId" = "/subscriptions/$($subscriptionId)/resourceGroups/$($vnet.ResourceGroupName)/providers/Microsoft.Network/virtualNetworks/$($vnetName)"
        }

        $virtualNetwork = New-AzureRmResource -Location $location -Properties $PropertiesObject -ResourceName "$($webAppName)/$($vnet.Name)" -ResourceType "Microsoft.Web/sites/virtualNetworkConnections" -ApiVersion 2015-08-01 -ResourceGroupName $resourceGroupName -Force

    # Now finish joining by getting the VPN package and giving it to the App
    Write-Host "Retrieving VPN Package and supplying to App"
    $packageUri = Get-AzureRmVpnClientPackage -ResourceGroupName $vnet.ResourceGroupName -VirtualNetworkGatewayName $gateway.Name -ProcessorArchitecture Amd64

    $packageUri = $packageUri.ToString(); 
    $packageUri = $packageUri.Substring(1, $packageUri.Length-2);

    # Put the VPN client configuration package onto the App
    $PropertiesObject = @{
    "vnetName" = $vnet.Name; "vpnPackageUri" = $packageUri.ToString()
    }
    $date = Get-Date -format "HH:mm tt"

    New-AzureRmResource -Location $location -Properties $PropertiesObject -ResourceName "$($webAppName)/$($vnet.Name)/primary" -ResourceType "Microsoft.Web/sites/virtualNetworkConnections/gateways" -ApiVersion 2015-08-01 -ResourceGroupName $resourceGroupName -WarningAction silentlyContinue -Force  

}

尽情享受