Skip to content

Instantly share code, notes, and snippets.

@danparker276
Created November 11, 2018 18:04
Show Gist options
  • Save danparker276/6d080f687718c8a92d8d8a43eddaae79 to your computer and use it in GitHub Desktop.
Save danparker276/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
#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
@danparker276
Copy link
Author

Azure made it a bit easier now to copy settings in advanced edit mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment