View and download JSON templates using AzureRM PowerShell

Download JSON templates using AzureRM

John Savill

January 22, 2016

1 Min Read
ITPro Today logo

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 MicrosoftGet-AzureResourceGroupGalleryTemplate -Identity Microsoft.WindowsServer2012Datacenter.0.2.77-previewSave-AzureResourceGroupGalleryTemplate -Identity Microsoft.WindowsServer2012Datacenter.0.2.77-preview -Path D:TempMicrosoft.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 $_.IdentityNew-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].

About the Author(s)

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like