简体   繁体   中英

Trying to make if/else powershell for ARM template

I have a PowerShell script for an ARM template to deploy some resources into Azure, more specifically ASE v2.

My ARM template has a condition in it stating:

"sv-ase-version": "v2",

    "sv-asp-template-filenameHash": {
      "v1": "[concat(variables('sv-baseURI'),concat('/azuredeploy-asp.v1.json',parameters('_artifactsLocationSasToken')))]",
      "v2": "[concat(variables('sv-baseURI'),concat('/azuredeploy-asp.json',parameters('_artifactsLocationSasToken')))]"
    },

What i have right now in PowerShell:

Param(

    [string] $TemplateFile = 'azuredeploy-dev.json',
    [string] $TemplateParametersFile = 'azuredeploy-dev.parameters.json',

)

$TemplateFile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $TemplateFile))
$TemplateParametersFile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $TemplateParametersFile))

What i wanna add in PowerShell is:

Param(

    [string] $TemplateFile = 'azuredeploy-dev.json',
    [string] $TemplateParametersFile = 'azuredeploy-dev.parameters.json',
    [string] $TemplateFilev2 = 'azuredeploy.json',
    [string] $TemplateParametersFilev2 = 'azuredeploy.parameters.json',

)

#Checking if this is the correct way to do it

if ("sv-ase-version" -eq "v1") {

    $TemplateFile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $TemplateFile))
    $TemplateParametersFile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $TemplateParametersFile))

}
else {

    $TemplateFilev2 = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $TemplateFilev2))
    $TemplateParametersFilev2 = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $TemplateParametersFilev2))

}

My intention: make the switch in the JSON file, without needing to change things in PowerShell.

Would this work? How would you approach this differently?

Thanks.

The easiest way to do that is use a parameter in the template, call it something like deploymentPrefix :

"deploymentPrefix": {
    "type": "string",
    "defaultValue": "dev",
    "allowedValues": [
        "dev",
        "prod"
    ],
    "metadata": {
        "description": "Resources created will be prefixed with this."
    }
},

and based on the value of that parameter decide what to deploy in the template:

"variables": {
    "template-dev": "someurl",
    "template-prod": "someotherurl",
    "template-url": "[concat('template-', parameters('deploymentPrefix))]"
    ...
}

and in your powershell you would just use New-AzureRmResourceGroupDeployment and pass that (dev or prod) to the parameter and the template would figure out what to use for the template-url variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM