将变量从ARM模板传递到PowerShell

时间:2019-04-23 08:49:44

标签: json azure powershell arm-template

有什么方法可以将变量从ARM模板传递到Powershell?

我的Power Shell看起来像:

Param(
 [string] $TemplateFile = 'azuredeploy.json',
 [string] $TemplateParametersFile = 'azuredeploy.parameters.json',
 [string] $ResourceGroupName = 'MyFirstTest-Example'
)
    New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
                                       -ResourceGroupName $ResourceGroupName `
                                       -TemplateFile $TemplateFile `
                                       -TemplateParameterFile $TemplateParametersFile `
                                       @OptionalParameters `
                                       -Force -Verbose `
                                       -ErrorVariable ErrorMessages

Suspend-AzureRmAnalysisServicesServer -Name "I Wouldlike to use a variable from ARM Template in this position" -ResourceGroupName $ResourceGroupName ` 

我的ARM模板的结构如下:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    //My Code here
  },
  "variables": {
    "AzureMachine": "['TestMachine']"
  },
  "resources": [
    //My Code here
  ],
  "outputs": {}
}

1 个答案:

答案 0 :(得分:3)

您可以从手臂模板的输出中获取变量。这是example供您参考。

输出

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

Powershell脚本

Param(
 [string] $TemplateFile = 'azuredeploy.json',
 [string] $TemplateParametersFile = 'azuredeploy.parameters.json',
 [string] $ResourceGroupName = 'MyFirstTest-Example'
)
   $deployment=New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
                                       -ResourceGroupName $ResourceGroupName `
                                       -TemplateFile $TemplateFile `
                                       -TemplateParameterFile $TemplateParametersFile `
                                       @OptionalParameters `
                                       -Force -Verbose `
                                       -ErrorVariable ErrorMessages

Suspend-AzureRmAnalysisServicesServer -Name $deployment.Outputs.item("vMname").value -ResourceGroupName $ResourceGroupName `