View and download JSON templates using AzureRM PowerShell

Q. Using AzureRM PowerShell how can I view templates and download one locally?

A. Prior to PowerShell 1.0 there were cmdlets to browse and download JSON templates, for example:

Get-AzureResourceGroupGalleryTemplate -Publisher Microsoft
Get-AzureResourceGroupGalleryTemplate -Identity Microsoft.WindowsServer2012Datacenter.0.2.77-preview
Save-AzureResourceGroupGalleryTemplate -Identity Microsoft.WindowsServer2012Datacenter.0.2.77-preview -Path D:\Temp\Microsoft.WindowsServer2012Datacenter.0.2.77-preview.json

There is currently no equivalent but this is being worked on by Microsoft. They have an article available at https://github.com/Azure/azure-powershell/issues/1064 which also contains code to work around the missing cmdlets which consists of downloading all the templates into an array and that array can then be searched and content downloaded from it. Note the example code actually downloads ALL templates to a local c:\templates folder.

# Retrieve all available items
$allGalleryItems = Invoke-WebRequest -Uri "https://gallery.azure.com/Microsoft.Gallery/GalleryItems?api-version=2015-04-01&includePreview=true" | ConvertFrom-Json

# Get all items published by Microsoft
$allGalleryItems | Where-Object { $_.PublisherDisplayName -eq "Microsoft" }

$allGalleryItems | Where-Object { $_.itemDisplayName -match "Windows Server 2012 R2 Datacenter" } | ft itemDisplayName, identity -AutoSize
$allGalleryItems | Where-Object { $_.itemDisplayName -eq "Windows Server 2012 R2 Datacenter" } | ft itemDisplayName, identity -AutoSize

# Save default template for all items under directory "C:\Templates"
$allGalleryItems | Foreach-Object { 
$path = Join-Path -Path "C:\templates" -ChildPath $_.Identity
New-Item -type Directory -Path $path

$.Artifacts | Where-Object { $.type -eq "template" } | ForEach-Object {
$templatePath = Join-Path -Path $path -ChildPath ( $_.Name + ".json" )

(Invoke-WebRequest -Uri $_.Uri).Content | Out-File -FilePath $templatePath
}
}

Each week, John Savill answers all of your toughest tech questions about the worlds of Windows Server, Azure, and beyond. Read his past IT advice here, and email your questions to [email protected].

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish