JJJCoder
JJJCoder

Reputation: 16916

How to Update Parameters for Azure Resource Template Deployment Using Powershell

I have created a template for creating a new SQL resource in Azure.

From this, I get the two standard files:

I now want to create a deployment using Powershell.

I could just have different parameters.json files with passwords written directly in them (this works), but I want to keep passwords out of Source Control. However, I cannot find a way to update the parameters without updating the file.

Here is my code (using Test-AzResourceGroupDeployment, with the aim of moving to New-AzResourceGroupDeployment once it's working):

$templatePath = "./sqlserver/template/template.json"
$paramsPath = "./sqlserver/template/parameters.json"
$administratorLogin = "adminUserName"
$administratorLoginPassword = "ARealPasswordHere!!!"
$databaseName = "MyDatabaseName"
$serverName = "serverforthedatabase"


$paramsFileText = [System.IO.File]::ReadAllText($paramsPath)
$params = ConvertFrom-Json $paramsFileText -AsHashtable
$params.parameters.administratorLogin = $administratorLogin
$params.parameters.administratorLoginPassword = ConvertTo-SecureString $administratorLoginPassword -AsPlainText -Force 
$params.parameters.databaseName = $databaseName
$params.parameters.serverName = $serverName

# Run New-AzResourceGroupDeployment when working
Test-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName `
-TemplateFile $templatePath `
-TemplateParameterObject $params

If I run the above, I get a prompt for every parameter in the params file.

If I change -TemplateParameterObject $params to -TemplateParameterObject $params.parameters, then I get the 'Template parameter JToken type is not valid error.

If I try making a TemplateObject so both inputs are objects, this too fails.

Test-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName `
-TemplateObject $templateObj `
-TemplateParameterObject $params.parameters

How should this be done please?

Upvotes: 1

Views: 890

Answers (1)

JJJCoder
JJJCoder

Reputation: 16916

Have managed to work out the answer.

You have to enter the parameters you wish to override as part of the script. Have tested with New-AzResourceGroupDeployment and it's all good.

Here is my working code:

$templatePath = "./sqlserver/template/template.json"
$paramsPath = "./sqlserver/template/parameters.json"
$administratorLogin = "adminUserName"
$administratorLoginPassword = ConvertTo-SecureString "ARealPasswordHere!!!" -AsPlainText -Force
$databaseName = "MyDatabaseName"
$serverName = "serverforthedatabase"


Test-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName `
-TemplateFile $templatePath `
-TemplateParameterFile $paramsPath `
-administratorLogin $administratorLogin `
-administratorLoginPassword $administratorLoginPassword `
-databaseName $databaseName `
-serverName $serverName

and just for added clarity for searchers, here is part of the `parameters.json` file, so you can see some of the parameters I'm overriding here.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "administratorLogin": {
            "value": ""
        },
        "administratorLoginPassword": {
            "value": ""
        },
        "collation": {
            "value": "SQL_Latin1_General_CP1_CI_AS"
        },
        "databaseName": {
            "value": ""
        },
        "tier": {
            "value": "Basic"
        },
...

Upvotes: 1

Related Questions