11

I was wondering if there is an easy way to completely copy all the key values from one web app's application settings to another, as seen in the below picture I have a lot of these key values and having to do this manually every time is very cumbersome.

enter image description here

1

4 Answers 4

18

You can use Azure PowerShell. Here is a PowerShell Script for you.

try{
    $acct = Get-AzureRmSubscription
}
catch{
    Login-AzureRmAccount
}

$myResourceGroup = '<your resource group>'
$mySite = '<your web app>'
$myResourceGroup2 = '<another resource group>'
$mySite2 = '<another web app>'

$props = (Invoke-AzureRmResourceAction -ResourceGroupName $myResourceGroup `
        -ResourceType Microsoft.Web/sites/Config -Name $mySite/appsettings `
        -Action list -ApiVersion 2015-08-01 -Force).Properties

$hash = @{}
$props | Get-Member -MemberType NoteProperty | % { $hash[$_.Name] = $props.($_.Name) }

Set-AzureRMWebApp -ResourceGroupName $myResourceGroup2 `
        -Name $mySite2 -AppSettings $hash

This script copy app settings from $mySite to $mySite2. If your web app involves with slot, for $props, you should use the following command instead.

$props = (Invoke-AzureRmResourceAction -ResourceGroupName $myResourceGroup `
        -ResourceType Microsoft.Web/sites/slots/Config -Name $mySite/$slot/appsettings `
        -Action list -ApiVersion 2015-08-01 -Force).Properties 

And, use Set-AzureRMWebAppSlot instead of Set-AzureRMWebApp

Set-AzureRMWebAppSlot -ResourceGroupName $myResourceGroup2 `
        -Name $mySite2 -Slot $slot -AppSettings $hash
6
  • This is a great answer if you want to copy from the current site to another one. If the goal is to deploy many sites all with the same settings, the suggested approach is to use an ARM template.
    – David Ebbo
    Commented Feb 1, 2016 at 4:15
  • @Jack Zeng I tried to run your code as a runbook but got an error, stackoverflow.com/questions/35486796/… Commented Feb 18, 2016 at 16:19
  • This is awesome! Commented Nov 24, 2017 at 13:03
  • That randomizes the order of the app settings though.
    – John
    Commented Mar 2, 2018 at 11:01
  • Had an issue, but I had to update AzureRM module in powershell and then it worked
    – Dan Parker
    Commented Sep 30, 2018 at 23:44
1

Using PowerShell with the Az module instead of AzureRM (based on Jack Zeng's answer).

# Copies application settings from one Azure Web App to another. 
# Warning: This will overwrite any app settings on the target Web App.

# Ensure Az module is installed before proceeding
# i.e. run Install-Module -Name Az -Force

# Connect to an Azure subscription
Connect Az-Account -Subscription "<your subscription id>"

# Parameters:
$sourceResourceGroupName = "<your source resource group>"
$sourceWebAppName = "<your source web app>"
$targetResourceGroupName = "<your target resource group>"
$targetWebAppName = "<your target web app>"

# Get app settings from source web app
$props = (Invoke-AzResourceAction `
    -ResourceGroupName $sourceResourceGroupName `
    -ResourceType Microsoft.Web/sites/Config `
    -Name $sourceWebAppName/appsettings `
    -Action list -ApiVersion 2021-02-01 -Force).Properties

# Convert to a HashSet (needed by Set-AzWebApp)
$hash = @{}
$props | Get-Member -MemberType NoteProperty | % { $hash[$_.Name] = $props.($_.Name) }

# Set target Web App Application Settings
Set-AzWebApp `
    -ResourceGroupName $targetResourceGroupName `
    -Name $targetWebAppName `
    -AppSettings $hash

0

There appears to be no way to give SetAzureRmWebAppSlot the order of the settings, meaning it's a useless pile of garbage. Luckily, there's another kind of cloud shell.

srcResourceGroup=$1
srcName=$2
dstResourceGroup=$3
dstName=$4

settingsToBeRemoved=$(az webapp config appsettings list --resource-group $dstResourceGroup --name $dstName | jq '.[] | .name' -r)

if [[ ! -z $settingsToBeRemoved ]]; then
    az webapp config appsettings delete --resource-group $dstResourceGroup --name $dstName --setting-names $settingsToBeRemoved > /dev/null
fi

settingsToBeCopied=$(az webapp config appsettings list --resource-group $srcResourceGroup --name $srcName | jq '.[] | .name+"="+.value' -r)

if [[ ! -z $settingsToBeCopied ]]; then
    az webapp config appsettings set --resource-group $dstResourceGroup --name $dstName --settings $settingsToBeCopied > /dev/null
fi

echo "Copied settings from $srcName to $dstName."
1
  • Your answer is incomplete. You do not specify that you are using the Azure CLI. Also the script is using parameters, but you do not check or give any hint if the parameters are missing. Also your script requires JQ to be installed. And finally you might inform users in your text that the old settings of the destination are removed.
    – Tom
    Commented Mar 27, 2018 at 9:36
0

I adjust some code I found so I could copy all settings and connection strings, but not override existing ones. I was having the problem of creating the app with application insights and other variables specific for that app/function and I was seeing those get wiped out. It can also help with future updates of apps

https://gist.github.com/danparker276/6d080f687718c8a92d8d8a43eddaae79

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.