Created
November 11, 2018 18:04
-
-
Save dp-la/6d080f687718c8a92d8d8a43eddaae79 to your computer and use it in GitHub Desktop.
Azure Powershell copy but not replace app settings and connection strings for WebApps and Azure Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#For copying Azure WebApp or Azure Functions application settings and connection strings | |
#This will copy from Web App, but not replace, because things like Insights, storage and existing settings | |
#you probably want to keep at times. Most samples I found were only complete replacements which I didn't want | |
#Make sure you're logged in | |
try{ | |
$acct = Get-AzureRmSubscription | |
} | |
catch{ | |
Login-AzureRmAccount | |
} | |
## parts are taken from | |
## http://jasonhaley.com/post/Azure-Help-WebApps-Copying-and-Exporting-Connection-Strings-and-App-Settings | |
$resourceGroupSource = 'azureResourceGroup1' | |
$webAppsource = 'webAppV1' | |
$resourceGroupTarget = 'azureResourceGroup1' | |
$webAppTarget = 'webAppV2' | |
# Load Existing Web App settings for source and target (I removed slots) | |
$webAppSource = Get-AzureRmWebApp -ResourceGroupName $resourceGroupSource ` | |
-Name $webAppsource | |
# -Slot $slotSource -- Get-AzureRmWebAppSlot | |
$webTargetSource = Get-AzureRmWebApp -ResourceGroupName $resourceGroupSource ` | |
-Name $webAppTarget | |
# Get reference to the source app settings | |
$appSettingsSource = $webAppSource.SiteConfig.AppSettings | |
# Get reference to the target Existing app settings | |
#$appSettingsExisting = $webTargetSource.SiteConfig.AppSettings | |
#I'm just putting in a hash so I can use ContainsKey below | |
$appSettingsExisting = @{} | |
ForEach ($ase in $webTargetSource.SiteConfig.AppSettings) { | |
$appSettingsExisting[$ase.Name] = $ase.Value | |
} | |
# Create Hash variable for App Settings | |
$appSettingsTarget = @{} | |
Write-Output 'start' | |
# Copy over all Existing App Settings to the Hash | |
ForEach ($appSettingSource in $appSettingsSource) { | |
If (! $appSettingsExisting.ContainsKey($appSettingSource.Name)){ | |
$appSettingsTarget[$appSettingSource.Name] = $appSettingSource.Value | |
} | |
else{ | |
$appSettingsTarget[$appSettingSource.Name] = $appSettingsExisting[$appSettingSource.Name] | |
} | |
} | |
# Save App Settings Strings to Target | |
Set-AzureRmWebApp -ResourceGroupName $resourceGroupTarget -Name $webAppTarget ` | |
-AppSettings $appSettingsTarget | |
#Now do Connection strings | |
$connectionStringsSource = $webAppSource.SiteConfig.ConnectionStrings | |
# Get reference to the target Existing app settings | |
$connectionStringsExisting = @{} | |
ForEach ($ase in $webTargetSource.SiteConfig.ConnectionStrings) { | |
$connectionStringsExisting[$ase.Name] = ` | |
@{ Type = $ase.Type.ToString(); ` | |
Value = $ase.ConnectionString } | |
} | |
# Create Hash variable for Connection Strings | |
$connectionStringsTarget = @{} | |
# Copy over all Existing Connection Strings to the Hash | |
ForEach($connStringSource in $connectionStringsSource) { | |
If (! $connectionStringsExisting.ContainsKey($connStringSource.Name)){ | |
$connectionStringsTarget[$connStringSource.Name] = ` | |
@{ Type = $connStringSource.Type.ToString(); ` | |
Value = $connStringSource.ConnectionString } | |
#$appSettingsTarget[$appSettingSource.Name] = $appSettingSource.Value | |
} | |
else{ | |
$connectionStringsTarget[$connStringSource.Name] = ` | |
@{ Type = $connectionStringsExisting[$connStringSource.Name].Type; ` | |
Value = $connectionStringsExisting[$connStringSource.Name].Value } | |
#$appSettingsTarget[$appSettingSource.Name] = $appSettingsExisting[$appSettingSource.Name] | |
} | |
} | |
#Write-Output $connectionStringsExisting | |
#Write-Output $connectionStringsTarget | |
# Save Connection Strings to Target | |
Set-AzureRmWebApp -ResourceGroupName $resourceGroupTarget -Name $webAppTarget ` | |
-ConnectionStrings $connectionStringsTarget |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Azure made it a bit easier now to copy settings in advanced edit mode