top of page
Search
  • Writer's pictureShannon

Use PowerShell to Build an ARM Template as a Job

A random question sort of pushed itself into view recently: can you submit an ARM Template build as a job? Did you know you can? There are a number of reasons why you might consider going this route. The big one I can think of is your deployment may be a longer deployment and you might not care about getting a bunch of verbose messages every 10-ish seconds or so during the build process. Wouldn't it be better to build the environment as a job, and then check in every so often?


That's what I was intent on doing, just for the sheer fact this might help someone who's doing a tremendous amount of automation with things like the DSC extension and provisioning large environments that may take some time to build.


The focus of this effort will once again be PowerShell. Apologies to those that would like to see more Az-CLI work. Believe me, it's on my docket to document some of what I've been doing in that realm, so stay tuned.


I have a PowerShell script I've tweaked over time that deploys a basic ARM Template and parameters file with the verbose flag. The verbose flag is helpful in identifying any issues after you've built a template. The code looks like this and can be found here:

[CmdletBinding()]
    param(
    [Parameter(Mandatory=$true)]
    [string]$location,    
    [Parameter(Mandatory=$true)]    
    [string]$rgName,
    [Parameter(Mandatory=$true)]
    [string]$resDeplName,
    [Parameter(Mandatory=$true)]
    [string]$tmplPath,
    [Parameter(Mandatory=$true)]
    [string]$tmplFile,
    [Parameter(Mandatory=$true)]
    [string]$tmpl,
    [Parameter(Mandatory=$true)]
    [string]$paramPath,
    [Parameter(Mandatory=$true)]
    [string]$paramFile,
    [Parameter(Mandatory=$true)]
    [string]$params
    )

New-AzResourceGroup `
    -Name $rgName `
    -Location $location `
    -Verbose -Force
    
New-AzResourceGroupDeployment `
    -Name $resDeplName `
    -ResourceGroupName $rgName `
    -TemplateFile $tmpl `
    -TemplateParameterFile $params `
    -Verbose -Force

When I'm working deep in multiple directories, I formulate my template file and template parameters file like so related to my variable declaration:

$location = 'eastus'
$rgName = 'storaccts001'
$resDeplName = 'diagnostic-storage-account'
$tmplpath = 'C:\Users\shkuehn\source\foundation\storage\'
$tmplfile = 'azuredeploy.json'
$tmpl = $tmplPath + '\' + $tmplFile
$paramPath = 'C:\Users\shkuehn\source\foundation\storage\'
$paramFile = 'azuredeploy.parameters.json'
$params = $paramPath + '\' + $paramFile

This way I only have to rerun the small amount that changes before running a different template.


So how do you build the PowerShell script so it runs the build as a job? Easy! If you examine the Azure PowerShell cmdlet New-AzResourceGroupDeployment, that tells you part of the tale. The rest comes with experimentation and I just went through that process for you!


The easiest way is to build your deployment script as follows (using the input variables I declared earlier):

$job = New-AzResourceGroupDeployment`
-Name $resDeplName`
-ResourceGroupName $rgName`
-TemplateFile $tmpl`
-TemplateParameterFile $params-AsJob

You'll then check in on the job periodically until it's finished by running the following command:

$job.StatusMessage

The entire deployment script looks like this within my world (and I linked to the GitHub repo where my code is stored earlier in the blog):















And when you run the status message check, here's what should display within your console window (I'm still in my Log Analytics local repo and was too lazy to change the directory path for the screen shot - my apologies):

Hopefully that helps you out if you're running through a series of deployments and can't wait for all the verbose flags to pop up within your console window.

534 views0 comments

Recent Posts

See All
bottom of page