Need help w/ passing PS parameter/s to JSON

I don’ t have much exp. w/ powershell and hope someone can help me to understand and learn how to pass PS parameters to JSON file.

I have PS script that I want to automate creation of RG and vNET in Azure subscription (this will be used with Jenkins job).

Here is what I have:

 

Add-AzureRmAccount -EnvironmentName AzureUSGovernment

select-azurermsubscription -subscriptionname ‘Azure Government Free Trial’

#Create or check for existing resource group

$resourceGroupName = Read-Host ‘Enter name for your RG’

$resourceGroupLocation = “useast”

$resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if(!$resourceGroup)
{
Write-Host “Resource group ‘$resourceGroupName’ does not exist. To create a new resource group, please enter a location.”;
if(!$resourceGroupLocation) {
$resourceGroupLocation = Read-Host “resourceGroupLocation”;
}
Write-Host “Creating resource group ‘$resourceGroupName’ in location ‘$resourceGroupLocation’”;
New-AzureRmResourceGroup -Name $resourceGroupName -Location $resourceGroupLocation
}
else{
Write-Host “Using existing resource group ‘$resourceGroupName’”;
}

$vnetName = Read-host ‘enter your vnet name’

$subnet1Name = Read-host ‘enter your subnet1 name’

$subnet2Name = Read-host ‘enter your subnet2 name’

$Deployment = @{
Name = ‘TestDeployment’
ResourceGroupname = ‘$resourceGroupName’
Mode = ‘Complete’
TemplateFile = “O:\vnetcreation.json”
TemplateParameterObject = @{
vnetName = ‘$vnetName’
subnet1Name= ‘$subnet1Name’
subnet2Name = ‘$subnet2Name’
}

}

New-AzureRmResourceGroupDeployment @Deployment

 

It creates Resource Group but I get following error:

New-AzureRmResourceGroupDeployment : ‘resourceGroupName’ does not match expected pattern ‘[1]+$’.
At line:43 char:1

  • New-AzureRmResourceGroupDeployment @Deployment
  • CategoryInfo : CloseError: (:slight_smile: [New-AzureRmResourceGroupDeployment], ValidationException
  • FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

and it never creates vNET and Subnets.


  1. -\w._() ↩︎

Remove the single qoutes in your splat:

$Deployment = @{
    Name              = 'TestDeployment'
    ResourceGroupname = $resourceGroupName
    Mode              = 'Complete'
    TemplateFile      = "O:\vnetcreation.json"
    TemplateParameterObject = @{
        vnetName    = $vnetName
        subnet1Name = $subnet1Name
        subnet2Name = $subnet2Name
    }
}

When you do this with single qoutes:

    ResourceGroupname = '$resourceGroupName'

your are making the variable literal, so it’s not resolving the variable, you’re passing $resourceGroupName as the name. You do need to wrap the variable in any type of qoutes, but using double qoutes would tell Powershell to resolve the variable:

PS C:\> $param1 = 'Awesome'

PS C:\> "$param1"
Awesome

PS C:\> '$param1'
$param1

Exactly, thanks Rob!

Elvir, since you are writing the scripts using AzureRM PowerShell module, it is better to write the scripts using Az module. Microsoft has released a new Azure module called Az which will be a replacement for the AzureRM module and works not only with Windows but also with Linux and MacOS. In windows, it works with PowerShell 5.x and 6.x as well, and Microsoft will not release any updates for AzureRM module except bug fixes, all future releases come only with Az module itself. For more details please visit the link below…

Thank you.

Thank you Rob, I will test it today.

Thanks Kiran, I’m aware of PS Core but I was just trying to get this working and then I will work on transition my scripts to PS Core (I know its double work :slight_smile: ).

I’m keep getting same error (I thought that the issue is with my ARM template but ARM templates works if I deploy it from Azure Portal).

This the error I’m getting:

New-AzureRmResourceGroupDeployment : 7:01:38 PM - Error: Code=InvalidTemplate; Message=Deployment template validation failed: ‘The provided value for the template parameter ‘storageAccountName’ at line ‘7’ and column ‘31’
is not valid.’.
At line:60 char:1

  • New-AzureRmResourceGroupDeployment @Deployment
  • CategoryInfo : NotSpecified: (:slight_smile: [New-AzureRmResourceGroupDeployment], Exception
  • FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

New-AzureRmResourceGroupDeployment : The deployment validation failed
At line:60 char:1

  • New-AzureRmResourceGroupDeployment @Deployment
  • CategoryInfo : CloseError: (:slight_smile: [New-AzureRmResourceGroupDeployment], InvalidOperationException
  • FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

I get this same error for every “key/value” pair that is first:

$Deployment = @{
Name = ‘Deployment’
ResourceGroupName = $resourceGroupName
Mode = ‘Complete’
TemplateFile = “C:\Users\elvir\Documents\createstorageaccount.json”
TemplateParameterObject = @{
storageAccountName = $storageAccountName
accountType = $accountType
kind = $kind
accessTier = $accessTier
location = $location
}

}

New-AzureRmResourceGroupDeployment @Deployment

 

so if my location is the first one, I will get the same error.

This is my ARM Template (I don’t see any issue w/ this ARM Template)

 

{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string",
"defaultValue": ""
},
"accountType": {
"type": "string",
"defaultValue": ""
},
"kind": {
"type": "string",
"defaultValue": ""
},
"accessTier": {
"type": "string",
"defaultValue": ""
},
"location": {
"type": "string",
"defaultValue": ""
},
"supportsHttpsTrafficOnly": {
"type": "bool",
"defaultValue": "true"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"sku": {
"name": "[parameters('accountType')]"
},
"kind": "[parameters('kind')]",
"name": "[parameters('storageAccountName')]",
"apiVersion": "2017-10-01",
"location": "[parameters('location')]",
"properties": {
"accessTier": "[parameters('accessTier')]",
"supportsHttpsTrafficOnly": "[parameters('supportsHttpsTrafficOnly')]"
},
"dependsOn": []
}
],
"outputs": {}
}
 

 

I was able to get this working (but not sure if that is correct way)

What I did, I removed hash-table and I used following:

$storageAccountName = Read-host ‘enter your storageAccountName name’

$accountType = Read-host ‘enter your accountType name’

$kind = Read-host ‘enter your account kind Type name’

$accessTier = Read-host ‘enter your account accessTier name’

$location = Read-host ‘enter your storageaccount location’

$TemplateFilePath = “C:\Users\elvir\Documents\createstorageaccount.json”

Test-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $TemplateFilePath -storageAccountName $storageAccountName -accountType $accountType -kind $kind -accessTier $accessTier -location $location

This seems to be working but it would be nice if I didn’t have to input all these key/value w/ PS command.