Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an App Settings to existing Azure Web Application using Azure Power Shell

I want to write a script that run using azure power shell to automate adding the Web Application configuration

Azure > MyWebApp > Application Settings > App settings

It's look like key = "value"

I write this script

########################### # MyApp Config Automation # ###########################  #Begin  $subscriptionName="MySubscriptionName" $webSiteName="MyWebAppName" $storageAccountName="StorageAccountName" ######################################## $userName = "[email protected]" $securePassword = ConvertTo-SecureString -String "mypass" -AsPlainText -Force ##################################### $cred = New-Object System.Management.Automation.PSCredential($userName, $securePassword) ##################################### Add-AzureAccount -Credential $cred  Select-AzureSubscription -SubscriptionName $subscriptionName -Default ##################################### Get-AzureWebsite -Name $webSiteName  #End 

but i know that the above script is only get my web application, now i need to access MyWebApp > Application Settings > App settings and give the script file/array of my new App settings and the script check if there are any new App Settings key it will add it to App Settings, if there are any existing keys it will override it's value. What is the steps or APIS or can i do that with azure power shell?

Edit: This script can Automate creating new web application and adding App Settings to it:

############################################## # Creating website and Adding Configs Script # ##############################################  $webSiteName="mywebsite" $storageAccountName="storageaccount" $subscriptionName="mysubsc" $userName = "myaccount" $securePassword = ConvertTo-SecureString -String "mypass" -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential($userName, $securePassword) Add-AzureAccount -Credential $cred  Select-AzureSubscription -SubscriptionName $subscriptionName -Default  New-AzureWebsite -Name $webSiteName New-AzureStorageAccount –StorageAccountName $storageAccountName -Location "South Central US" $ClientId="dfgdf6" $Password="ffefe" $StorageAccountKey = Get-AzureStorageKey -StorageAccountName $storageAccountName $AppSettings = @{"StorageAccountPrimary" = $StorageAccountKey.Primary;"StorageAccountSecondary" = $StorageAccountKey.Secondary;"ida:ClientId"=$ClientId;"ida:Password"=$Password}  Set-AzureWebsite -Name $webSiteName -AppSettings $AppSettings 
like image 682
Marzouk Avatar asked Sep 12 '15 21:09

Marzouk


People also ask

How do I edit web config in Azure App Service?

If you want to change web. config files you can use the Azure portal, there is a tool called "App Service Editor" in preview or Kudu that lets you edit any of the files you've deployed.

How do I connect to Azure app configuration?

In the upper-left corner of the home page, select Create a resource. In the Search services and marketplace box, enter App Configuration and select Enter . Select App Configuration from the search results, and then select Create. Select the Azure subscription that you want to use to test App Configuration.

Where do you configure and customize your app Azure?

In the Azure portal, search for and select App Services, and then select your app. In the app's left menu, select Configuration > General settings. Here, you can configure some common settings for the app.

How to configure Azure App service using PowerShell?

The configuration of the Azure App Service can be automated by using PowerShell, for this first example we will start configuring Application settings. To maintain the application settings values we start off by making a configuration file in which the key value pairs can be saved.

Where can I find the application settings in the Azure portal?

To find the application settings, see Get started in the Azure portal. The Application settings tab maintains settings that are used by your function app. You must select Show values to see the values in the portal. To add a setting in the portal, select New application setting and add the new key-value pair.

How do I add a single key/value application in Azure web app?

Adds (or updates) a single key/value application setting in an Azure Web App deployment slot. This cmdlet will apply the specified setting to an Azure Web App deployment slot. If the setting is not found, it will be added. If the setting is already found, it will be updated to the new value.

What is Azure web application configuration?

Azure Web Applications have access to an additional key/value settings store which is stored in the Azure Web Site Configuration Database (in Azure infrastructure; not your code/config files). The settings are exposed as environment variables to the web server. This means that you can access the settings through non .NET web sites too.


2 Answers

Here's an update to it based on the 12/2015 Azure PowerShell commands. The example is for slot-specific settings, if you want global, use Get/Set-AzureRmWebApp and remove the -slot parameter.

$myResourceGroup = 'PartsUnlimitedMRP' $mySite = 'centpartsunlimited'  $webApp = Get-AzureRMWebAppSlot -ResourceGroupName $myResourceGroup -Name $mySite -Slot production $appSettingList = $webApp.SiteConfig.AppSettings  $hash = @{} ForEach ($kvp in $appSettingList) {     $hash[$kvp.Name] = $kvp.Value }  $hash['NewKey'] = "NewValue" $hash['ExistingKey'] = "NewValue"  Set-AzureRMWebAppSlot -ResourceGroupName $myResourceGroup -Name $mySite -AppSettings $hash -Slot production 
like image 84
Dan Piessens Avatar answered Sep 19 '22 08:09

Dan Piessens


Retrieve App Settings

First set these two variables.

$myResourceGroup = 'RESOURCE_GROUP_NAME' $mySite = 'SITE_NAME' 

Then switch to the new Resource Manager mode and sign-in to your account.

Switch-AzureMode AzureResourceManager Get-AzureAccount 

Then retrieve the app settings. (Note that a back tick (`) means a new line.)

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

Add/Update App Settings

To update settings, first put them into a variable.

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

To use Set-AzureWebsite convert the variable to a hash table.

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

Now add/update values in the hash table.

$hash.NewKey = "NewValue" $hash.ExistingKey = "NewValue" 

Then switch back to Service Management mode and commit the settings.

Switch-AzureMode AzureServiceManagement Set-AzureWebsite -Name $mySite -AppSettings $hash 

Complete Code Listing

$myResourceGroup = 'RESOURCE_GROUP_NAME' $mySite = 'SITE_NAME'  Switch-AzureMode AzureResourceManager Get-AzureAccount  (Invoke-AzureResourceAction -ResourceGroupName $myResourceGroup `  -ResourceType Microsoft.Web/sites/Config -Name $mySite/appsettings `  -Action list -ApiVersion 2015-08-01 -Force).Properties  $props = (Invoke-AzureResourceAction -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) }  $hash.NewKey = "NewValue" $hash.ExistingKey = "NewValue"  Switch-AzureMode AzureServiceManagement Set-AzureWebsite -Name $mySite -AppSettings $hash 

Notes

The AzureServiceManagement and AzureResourceManager are not meant for use in the same session. For now the latter does not seem to permit updating the app settings via Set-AzureResource. The above is a workaround. Another way is to use the Azure CLI instead of PowerShell.

like image 28
Shaun Luttin Avatar answered Sep 20 '22 08:09

Shaun Luttin