Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
749 views
in Technique[技术] by (71.8m points)

powershell - 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 = "myaccount@outlook.com"
$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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...