0

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.

Alex
  • 75
  • 7
  • `if ("sv-ase-version" -eq "v1")` this doesnt make sense. this is always false. what are you trying to achieve? – 4c74356b41 Jul 26 '17 at 10:59
  • Trying to tell powershell to use 2 variables if the value of sv-ase-version is v1. if it's v2, needs to use other 2 variables. – Alex Jul 26 '17 at 12:10
  • where is that value coming from? right now its hardcoded – 4c74356b41 Jul 26 '17 at 12:13
  • the value is hardcoded into the JSON called azuredeploy.json that deployes ARM templates into azure. Please see the first part of the code i posted. – Alex Jul 26 '17 at 12:18
  • this doesn't make sense. you want powershell do decide what to deploy based on what is in the template? – 4c74356b41 Jul 26 '17 at 12:22
  • Correct. I might have written the powershell code incorrectly. This is where i need help. – Alex Jul 26 '17 at 12:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150189/discussion-between-alex-i-and-4c74356b41). – Alex Jul 26 '17 at 12:25
  • hey, does this work for you? – 4c74356b41 Jul 30 '17 at 19:54

1 Answers1

1

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.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141