Is it possible to easily copy applications settings from one web app to another on azure

南楼画角 提交于 2019-12-03 12:12:44

问题


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.


回答1:


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



回答2:


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."



回答3:


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



来源:https://stackoverflow.com/questions/35118432/is-it-possible-to-easily-copy-applications-settings-from-one-web-app-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!