1
votes

So, I've got this ARM template for deploying a VM to Azure. In order to create a unique but deterministic storage account name, I use the uniqueString() function. It looks something like:

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

I want to be able to create that same string outside the deployment template, for example in a PowerShell script, or use it as an input in a VSTS task.

Is there any way for me to do this?

1
Assaf, do you want to be able to generate the same string outside of a ARM template? Or In your scenario, can you generate the string in your ARM template, return it as an output and then use it in a PowerShell script or VSTS task? - Vivien Chevallier
Either will do. My goal is to use it in a vsts task. Either generate it in the same way or output it. - Assaf Stone

1 Answers

1
votes

Assaf,

This is not possible, but assuming you want to use your variable in a subsequent VSTS task, here are the steps to achieve it.

In your main ARM template file, at the end, output your variable like following:

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

After your deployment task, set your variable in the VSTS task context by executing this PowerShell script:

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) }

For this script, you need to provide the Azure resource group name where the deployment will be made. The script get the last deployment in the resource group, and set each output as a variable in the VSTS task context.


Access your variable and use it as a parameter like with any other VSTS variable:

-myparameter $(vhdStorageName)