当您向下钻取时,您可以通过资源管理器 (https://resources.azure.com/) 通过安静的“创建”调用手动执行此操作:
订阅 -> 资源组 -> 站点 -> -> 混合连接
我已经复制了资源管理器应用程序发出的调用并在 Powershell 中复制了它。
你需要:
- 查找您的订阅租户 ID(谷歌搜索)
- 向 AzureAD 注册应用程序
- 从资源管理器中收集各种常量,例如 Biztalk Uri 和称为实体连接字符串的东西
脚本使用这些详细信息获取身份验证令牌以从 powershell 调用其余 api。然后它调用经典的 rest api 将现有的混合连接添加到网站。请注意,这仅适用于以前具有混合连接的网站,因此如果没有一些手动工作和资源管理器记录的详细信息,您将无法启动全新的环境。
下面是这个脚本的副本,我从 Octopus deploy 调用它,所以所有 #{...} 变量都是从那里提供的。
此脚本将继续向面向外部的网站调用 api 端点,该网站通过混合连接调用内部系统。它将尝试 5 次等待 200 响应。
我在脚本中使用的 Hybrid.ConnectionRestUrl 变量是通过观察资源管理器的调用获得的。它是这样构造的:https://management.azure.com/subscriptions/#{SubscriptionId}/resourceGroups/#{resource-group-name}/providers/Microsoft.Web/sites/#{web-site-name}/hybridconnection/#{web-site-name}?api-version=2015-08-01
无法保证此脚本将工作多长时间,因为它几乎不是受支持的方法。
$authUri = "https://login.microsoftonline.com/#{tenant-domain}/oauth2/token"
$authMethod = "POST"
$authFormFields = @{resource='https://management.core.windows.net/';client_id='#{AzureAD.ApplicationId}';grant_type='client_credentials';client_secret='#{AzureAD.ApplicationSecret}'}
$authResponse = Invoke-WebRequest -Uri $authUri -Method $authMethod -Body $authFormFields -ContentType "application/x-www-form-urlencoded" | ConvertFrom-Json
$authorization = "Bearer " + $authResponse.access_token
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$headers.Add("Content-Type", "application/json")
$URI = "#{Hybrid.ConnectionRestUrl}"
$method = "PUT"
$body = '{ "name" : "#{ExternalAzureService.WebApp}", "location" : "#{App.Location}", "type" : "Microsoft.Web/sites", "properties" : { "entityName" : "#{Hybrid.EntityName}", "entityConnectionString" : "#{Hybrid.EntityConnectionString}", "resourceType" : "", "resourceConnectionString" : "", "hostname" : "#{InternalService.Hostname.Raw}", "port" : 80, "biztalkUri" : "#{Hybrid.BiztalkUri}" } }'
Write-Output $URI
Write-Output $body
Try
{
$result = Invoke-RestMethod -Uri $URI -Method $method -Headers $headers -Body $body
}
Catch
{
Write-Output "Error Occurred "
$i = 1
$pingUrl = "http://#{ExternalAzureService.WebApp.HostName}/api/callinternalsystem"
Write-Output "Ping $i times this url: $pingUrl"
Do
{
Write-Output "Starting Ping call $i"
$response = Invoke-WebRequest $pingUrl
If ($response.StatusCode -eq 200) {
Write-Output "200 returned"
Break
}
$i++
} While ($i -le 5)
}
Write-Output " *********************************** SUCCESS ********************************** "
Write-Output $result