Azure devops 使用AzureRM服务连接时如何获取Azure订阅ID?

Azure devops 使用AzureRM服务连接时如何获取Azure订阅ID?,azure-devops,Azure Devops,我在task.json中有一个带有标准AzureRM服务端点的自定义ADO任务: "name": ConnectedServiceName", "type": "connectedService:AzureRM" 这允许用户选择Azure订阅。但是,我需要将订阅ID传递给后端脚本,而我看不出这是如何公开的$(ConnectedServiceName)是已连接服务的id,而不是订阅…您可以使用powershell脚本从Azur

我在task.json中有一个带有标准AzureRM服务端点的自定义ADO任务:

"name": ConnectedServiceName",
"type": "connectedService:AzureRM"

这允许用户选择Azure订阅。但是,我需要将订阅ID传递给后端脚本,而我看不出这是如何公开的
$(ConnectedServiceName)
是已连接服务的id,而不是订阅…

您可以使用powershell脚本从AzureRM服务连接获取Azure订阅id

以下是一个例子:

文件:

Ps_modules (VstsTaskSdk)

Test.ps1

Icon.png

Task.json
Test.ps1

在任务(powershell脚本)中,您可以通过

$ConnectedServiceName = Get-VstsInput -Name ConnectedServiceName -Require
获取订阅ID

try {
    $Endpoint = Get-VstsEndpoint -Name $ConnectedServiceName -Require
    if (!$Endpoint) {
        throw "Endpoint not found..."
    }
    # Get the authentication details
    $subscriptionId=$Endpoint.Data.SubscriptionId
    $tenantId = $Endpoint.Auth.Parameters.TenantId
 ......

} catch {
    Write-Host "Authentication failed: $($_.Exception.Message)..." 
}
task.json中的部件代码(用于选择订阅的输入框):

这是,你可以参考一下

 "inputs": [{
        "name": "ConnectedServiceName",
        "type": "connectedService:AzureRM",
        "label": "AzureRM Subscription",
        "defaultValue": "",
        "required": true,
        "helpMarkDown": "Select the AzureRM Subscription that contains the AzureRM App Service"
    },