Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Last active July 27, 2017 22:57
Show Gist options
  • Save jcefoli/073ba10103d23b58eada7b69ea25f799 to your computer and use it in GitHub Desktop.
Save jcefoli/073ba10103d23b58eada7b69ea25f799 to your computer and use it in GitHub Desktop.
Copy All AppSettings and ConnectionStrings from One Azure Webapp to Another
<#
.SYNOPSIS
Powershell Script that copies AppSettings and ConnectionStrings between from one Azure Web App to another on the same subscription
.Description
Pass the old and new resource group and webapp names to this script and it will copy the settings over seamlessly
.NOTES
Version: 1.0
Author: jcefoli
Creation Date: 7/27/2017
This script assumes you have the AzureRM Powershell modules installed, are logged into your Azure Account (Login-AzureRmAccount) and on the correct Subscription
.PARAMETER FromResourceGroup
The resource group of the Web App to copy settings FROM
.PARAMETER FromWebApp
Azure Web App to copy settings FROM
.PARAMETER ToResourceGroup
The resource group of the Web App to copy settings TO
.PARAMETER ToWebApp
Azure Web App to copy settings FROM
.EXAMPLE
./copy_azure_configs.ps1 -FromResourceGroup "old-resource-group" -FromWebApp "old-webapp" -ToResourceGroup "new-resource-group" -FromWebApp "new-webapp"
This will recursively run all scripts except those with filenames matching "*RunOnce*" in E:\SQL\v1_1 against the database specified in the connectionString. -SQLFolderSuffix may be a NULL string
#>
param (
[string]
$FromResourceGroup = $(throw "-FromResourceGroup is required."),
[string]
$FromWebApp = $(throw "-FromWebApp is required."),
[string]
$ToResourceGroup = $(throw "-ToResourceGroup is required."),
[string]
$ToWebApp = $(throw "-ToWebApp is required.")
)
#Get some data. We'll use these later
$fromWebappObject = Get-AzureRMWebApp -ResourceGroupName $FromResourceGroup -Name $FromWebApp
$fromappSettingList = $fromWebappObject.SiteConfig.AppSettings
$fromConnStringsList = $fromWebappObject.SiteConfig.ConnectionStrings
#Create hash table of AppSetings
$appSettingsHashTable = @{}
ForEach ($kvp in $fromappSettingList) {
$appSettingsHashTable[$kvp.Name] = $kvp.Value
}
#Create hash table of ConnectionStrings
$connStringsHashTable = @{}
ForEach ($kvp in $fromConnStringsList) {
$connStringsHashTable[$kvp.Name] = @{ Type = $kvp.Type.ToString(); Value = $kvp.ConnectionString }
}
#Update AppSettings Config on new Web App
Set-AzureRMWebApp -ResourceGroupName $ToResourceGroup -Name $ToWebApp -AppSettings $appSettingsHashTable
#Update ConnectionStrings Config on new Web App
Set-AzureRmWebapp -ResourceGroupName $ToResourceGroup -Name $ToWebApp -ConnectionStrings $connStringsHashTable
Write-Host "[DONE] - Copied AppSettings and ConnectionStrings from $FromWebApp to $ToWebApp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment