How to deploy to Azure using an ARM template with PowerShell

I wrote this article here “How to use/create ARM templates for deployments” where I show how I generated the ARM template for multiple Azure features existing in a Resource Group.  I then deploy all features using the ARM template using the Template deployment blade in the portal.  This article explains how to run the same ARM template from PowerShell.  It is actuall quit simple.

When you create the Automation script via the portal, the extraction also creates a PS1 file named deploy.ps1.  This contains the script that performs the deployment using the JSON files, which were also created.  Simply right-click on the deploy.ps1 file, as seen in Figure 1 and select “Run with PowerShell”. image

Figure 1, deploy ARM template using PowerShell

One note about taking the “Run with PowerShell” approach is that the window closes if there is an exception and upon completion, so you don’t get a chance to see the details of either.  You can also select the ‘Edit’ menu item and run it from the PowerShell IDE as seen in Figure 2.
image

Figure 2, deploy ARM template using PowerShell

If all goes well, then you will be prompted for the the necessary information and the execution will result in the proper deployment of the resources.  An output of my ARM template, which was extracted from this resource group here, is as shown in Figure 3. image

Figure 3, deploy ARM template using PowerShell

 
cmdlet deploy.ps1 at command pipeline position 1
Supply values for the following parameters:
subscriptionId: 
resourceGroupName: READINESS-HC017-RG
deploymentName: READINESS017
Logging in...

Account          : 
SubscriptionName : 
SubscriptionId   : 
TenantId         : 
Environment      : AzureCloud

Selecting subscription 

Name               : [, ]
Account            : 
Environment        : AzureCloud
Subscription       : 
Tenant             : 
TokenCache         : Microsoft.Azure.Commands.Common.Authentication.AuthenticationStoreTokenCache
VersionProfile     : 
ExtendedProperties : {}

Registering resource providers
Registering resource provider microsoft.compute
ProviderNamespace : Microsoft.Compute
RegistrationState : Registered
ResourceTypes     : {availabilitySets, virtualMachines, virtualMachines/extensions, virtualMachineScaleSets...}
Locations         : {East US, East US 2, West US, Central US...}
ZoneMappings      : 

Registering resource provider microsoft.network
ProviderNamespace : Microsoft.Network
RegistrationState : Registered
ResourceTypes     : {virtualNetworks, publicIPAddresses, networkInterfaces, loadBalancers...}
Locations         : {West US, East US, North Europe, West Europe...}
ZoneMappings      : 

Registering resource provider microsoft.storage
ProviderNamespace : Microsoft.Storage
RegistrationState : Registered
ResourceTypes     : {storageAccounts, operations, locations/asyncoperations, storageAccounts/listAccountSas...}
Locations         : {East US, East US 2, West US, West Europe...}
ZoneMappings      : 

Registering resource provider microsoft.web
ProviderNamespace : Microsoft.Web
RegistrationState : Registered
ResourceTypes     : {sites/extensions, sites/slots/extensions, sites/instances, sites/slots/instances...}
Locations         : {Central US, North Europe, West Europe, Southeast Asia...}
ZoneMappings      : 

Resource group READINESS-HC017-RG does not exist. To create a new resource group, please enter a location.
resourceGroupLocation: West Europe
Creating resource group READINESS-HC017-RG in location West Europe
ResourceGroupName : READINESS-HC017-RG
Location          : westeurope
ProvisioningState : Succeeded
Tags              : 
TagsTable         : 
ResourceId        : /subscriptions//resourceGroups/READINESS-HC017-RG

Starting deployment...

DeploymentName          : template
CorrelationId           : ac008d7b-c64a-46ba-8fa3-8112260635d5
ResourceGroupName       : READINESS-HC017-RG
ProvisioningState       : Succeeded
Timestamp               : 21.03.2018 09:16:56
Mode                    : Incremental
TemplateLink            : 
TemplateLinkString      : 
DeploymentDebugLogLevel : 
Parameters              : 
  {[sites_READINESS_HCM001_name, Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.DeploymentVariable], 
   [virtualMachines_HCM001_name, Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.DeploymentVariable], 
   [virtualMachines_WEB001_name, Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.DeploymentVariable], 
   [serverfarms_READINESS_HCM_ASP_name, Microsoft.Azure.Commands.ResourceManager
         .Cmdlets.SdkModels.DeploymentVariable]...}
ParametersString        : 
                          Name             Type                             Value     
                          ===============  =========================        ==========
                          sites_READINESS_HCM001_name  String                   READINESS-HCM001
                          virtualMachines_HCM001_name  String                   HCM001    
                          virtualMachines_WEB001_name  String                   WEB001    
                          serverfarms_READINESS_HCM_ASP_name  String            READINESS-HCM-ASP
                          networkInterfaces_hcm001446_name  String              hcm001446 
                          networkInterfaces_web001454_name  String              web001454 
                          publicIPAddresses_HCM001_ip_name  String              HCM001-ip 
                          publicIPAddresses_WEB001_ip_name  String              WEB001-ip 
                          config_web_name  String                           web       
                          networkSecurityGroups_HCM001_nsg_name  String         HCM001-nsg
                          networkSecurityGroups_WEB001_nsg_name  String         WEB001-nsg
                          virtualNetworks_READINESS_HCM001_RG_vnet_name  String  READINESS-HCM001-RG-vnet
                          storageAccounts_readinesshcm001rgdiag701_name  String  readinesshcm001rgdiag701
                          subnets_default_name  String                          default   
                          securityRules_default_allow_rdp_name  String          default-allow-rdp
                          securityRules_default_allow_rdp_name_1  String        default-allow-rdp
                          hostNameBindings_readiness_hcm001.azurewebsites.net_name  
                              String  +.azurewebsites.net
                          virtualMachines_HCM001_id  String                     
                                                     /subscriptions//resourceGroups/READ
                                                     INESS-HCM001-RG/providers/Microsoft.Compute/disks/
                                                     HCM001_OsDisk_1_1189fc07b347421b878b9632040d5414
                          virtualMachines_WEB001_id  String                     
                                                     /subscriptions/2/resourceGroups/READ
                                                     INESS-HCM001-RG/providers/Microsoft.Compute/disks/
                                                     WEB001_OsDisk_1_f29722f8755744eb8d17074eb05df182
                          
Outputs                 : 
OutputsString           : 

NOTES:

  • It states in the notes of the deploy.ps1 file that if the parameters.json file is found that it will be used to fill the required parameters values.  By default, in my case all the values had a null value.  If the parameters.json file is not found then values are collected from the template.json file which did have the values.  I simply removed the parameters.json file from the directory which I ran the deploy.ps1 fil.  Here is the exception:
 
New-AzureRmResourceGroupDeployment : 09:29:43 - Error: Code=InvalidDeploymentParameterValue; 
  Message=The value of deployment parameter 
   hostNameBindings_readiness_hcm001.azurewebsites.net_name is null. 
  Please specify the value or use the parameter reference. See 
    https://aka.ms/arm-deploy/#parameter-file for details.
     At C:\Users\benperk\Downloads\FullTemplate\deploy.ps1:104 char:5
     +     New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGr ...
     +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-AzureRmResourceGroupDeployment], Exception
    + FullyQualifiedErrorId : 
       Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

And here is the content from deploy.ps1 file which made the claritifaction:

 
 .SYNOPSIS
    Deploys a template to Azure

 .DESCRIPTION
    Deploys an Azure Resource Manager template

 .PARAMETER subscriptionId
    The subscription id where the template will be deployed.

 .PARAMETER resourceGroupName
    The resource group where the template will be deployed. Can be the name of an 
     existing or a new resource group.

 .PARAMETER resourceGroupLocation
    Optional, a resource group location. If specified, will try to create a new 
    resource group in this location. 
    If not specified, assumes resource group is existing.

 .PARAMETER deploymentName
    The deployment name.

 .PARAMETER templateFilePath
    Optional, path to the template file. Defaults to template.json.

 .PARAMETER parametersFilePath
    Optional, path to the parameters file. Defaults to parameters.json. If file is not 
    found, will prompt for parameter values based on template.
  • Initially, i needed to execute Set-ExecutionPolicy to be able to run this, even as an Administrator

That was not so hard and worked as expected.

To get a overview of the project I worked on, read the following articles as well.