Skip to content

Exporting all Azure resources to ARM (Azure Resource Manager) Template with a simple PowerShell script

This high-level PowerShell script will export all resources of an Azure account to an ARM (Azure Resource Manager) template and deploy it on another Azure account (please note that this script will export and import only those resources that can be represented in an ARM template).

1. Make sure you have the Azure PowerShell module installed.
2. Authenticate to the source Azure account with `Connect-AzAccount` and the destination Azure account with `Connect-AzAccount -TenantId <TenantId> -SubscriptionId <SubscriptionId>`.

Replace the placeholder values in the script with the appropriate information, such as `SourceResourceGroupName`, `DestinationResourceGroupName`, `DestinationLocation`, `SourceSubscriptionId`, and `DestinationSubscriptionId`.

# Variables
$SourceResourceGroupName = "<SourceResourceGroupName>"
$DestinationResourceGroupName = "<DestinationResourceGroupName>"
$DestinationLocation = "<DestinationLocation>"
$SourceSubscriptionId = "<SourceSubscriptionId>"
$DestinationSubscriptionId = "<DestinationSubscriptionId>"

# Export resources from the source resource group to an ARM template
Select-AzSubscription -SubscriptionId $SourceSubscriptionId
$templateExport = Export-AzResourceGroup -ResourceGroupName $SourceResourceGroupName
$template = $templateExport.Template

# Create a new resource group in the destination Azure account
Select-AzSubscription -SubscriptionId $DestinationSubscriptionId
New-AzResourceGroup -Name $DestinationResourceGroupName -Location $DestinationLocation -Force

# Deploy the ARM template to the destination resource group
New-AzResourceGroupDeployment -ResourceGroupName $DestinationResourceGroupName -TemplateObject $template -Force

Please note that this script assumes you have the necessary permissions in both the source and destination Azure accounts. Additionally, you might need to adjust the script to accommodate any specific requirements or limitations associated with your resources, such as renaming resources or updating configurations.

Published inTutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Appliance - Powered by TurnKey Linux