1

所以,我有这个用于将 VM 部署到 Azure 的 ARM 模板。为了创建一个唯一但确定的存储帐户名称,我使用了uniqueString()函数。它看起来像:

"variables": {
    ...
    "vhdStorageName": "[concat('vhdstorage', uniqueString(resourceGroup().id))]",
    ...
}

我希望能够在部署模板之外创建相同的字符串,例如在 PowerShell 脚本中,或将其用作VSTS 任务中的输入。

我有什么办法可以做到这一点吗?

4

1 回答 1

1

阿萨夫,

这是不可能的,但假设您想在后续的 VSTS 任务中使用您的变量,以下是实现它的步骤。

最后,在您的主 ARM 模板文件中,输出您的变量,如下所示:

"outputs": {
  "vhdStorageName": {
    "type": "string",
    "value": "[variables('vhdStorageName')]"
  }
}

完成部署任务后,通过执行以下 PowerShell 脚本在VSTS 任务上下文中设置变量:

param ([string] $resourceGroupName)

#get the most recent deployment for the resource group
$lastRgDeployment = (Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName | Sort Timestamp -Descending | Select -First 1)

if(!$lastRgDeployment)
{
    throw "Resource Group Deployment could not be found for '$resourceGroupName'."
}

$deploymentOutputParameters = $lastRgDeployment.Outputs

if(!$deploymentOutputParameters)
{
    throw "No output parameters could be found for the last deployment of '$resourceGroupName'."
}

$deploymentOutputParameters.Keys | % { Write-Host ("##vso[task.setvariable variable="+$_+";]"+$deploymentOutputParameters[$_].Value) }

对于此脚本,您需要提供将在其中进行部署的 Azure 资源组名称。该脚本获取资源组中的最后一个部署,并将每个输出设置为 VSTS 任务上下文中的变量。


访问您的变量并将其用作任何其他 VSTS 变量的参数:

-myparameter $(vhdStorageName)
于 2016-12-11T00:00:21.887 回答