使用Azure Power Shell为现有的Azure Web应用程序添加应用程序设置

43
我想编写一个脚本,使用Azure PowerShell来自动添加Web应用程序配置。
Azure > MyWebApp > 应用程序设置 > 应用设置
它看起来像是 键 = "值"
我编写了这个脚本
###########################
# 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
但我知道上面的脚本只能获取我的 Web 应用程序,现在我需要访问 MyWebApp > 应用设置 > 应用设置,并提供我的新应用设置的脚本文件/数组,然后脚本会检查是否有任何新的应用设置键,如果有它将把它添加到应用设置中,如果有任何现有的键它将覆盖其值。有什么步骤或 API 可以做到这一点,或者我可以使用 Azure PowerShell 吗?
编辑: 此脚本可以自动创建新的 Web 应用程序并向其添加应用设置。
##############################################
# 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

你的编辑是否回答了你的问题? - Shaun Luttin
1
@ShaunLuttin 我认为不是。 - Marzouk
4个回答

92

以下是基于2015年12月的Azure PowerShell命令的更新。此示例用于特定于插槽的设置,如果您需要全局设置,请使用Get/Set-AzureRmWebApp并删除-slot参数。

$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

2
这是更好的答案,随着2015年12月发布的Azure PowerShell命令。 - Andy Mehalick
我必须运行这段代码片段,同时使用Set-AzureRMWebApp和Set-AzureRMWebAppSlot。否则,如果我只运行Set-AzureRMWebApp,则无法更新暂存槽中的非槽特定设置。除非我漏掉了什么... - MartynJones87
值得注意的是,如果您将此作为 Azure 管道中的一步运行,则需要在使用 Set-AzureRMWebAppSlot 之前运行 Enable-AzureRmAlias - Serban Murariu

14

检索应用程序设置

首先设置这两个变量。

$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

使用Set-AzureWebsite时,需要将变量转换为哈希表。

 $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

完整代码清单

$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

笔记

AzureServiceManagement和AzureResourceManager不应在同一会话中使用。目前后者似乎不允许通过Set-AzureResource更新应用程序设置。上述是一种解决方法。另一种方法是使用Azure CLI而不是PowerShell。


1
非常感谢您详细的回答,我会检查并回复您。 - Marzouk

12

这些答案已经过时了,因为原始的Azure PowerShell和AzureRM已被弃用。如果要使用Az PowerShell模块来执行此操作,则应该像这样:

示例

Connect-AzAccount
$site = Get-AzWebApp -Name foo-com-dev-as
$oldSettings = ($site.SiteConfig.AppSettings | % { $h = @{} } { $h[$_.Name] = $_.Value } { $h })

$newSettings = @{ StorageAccountPrimary = $StorageAccountKey.Primary
                  StorageAccountSecondary = $StorageAccountKey.Secondary
                  "ida:ClientId" = $ClientId
                  "ida:Password" = $Password }

Set-AzWebApp -ResourceGroupName foo-com-dev-rg -Name foo-com-dev-as -AppSettings ($oldSettings + $newSettings)

解释

  1. Connection-AzAccount - 连接到 Azure 帐户,如果需要选择订阅,则可能需要执行后续步骤
  2. $site = Get-AzWebApp... - 检索要修改的网站
  3. $oldSettings... - 获取所有现有设置并将它们放入 HashTable 中
    1. $site.SiteConfig.AppSettings | % - 通过 ForEach-Object 的简写别名传递(传递)每个设置
    2. { $h = @{} } - 通过 -Begin 位置参数创建一个 HashTable
    3. { $h[$_.Name] = $_Value } - 通过 -Process 位置参数为 $site.SiteConfig.AppSettings 中的每个值添加一个命名值到 HashTable
    4. { $h } - 通过 -End 位置参数返回新填充的 HashTable 到左侧变量
  4. $newSettings = @{... - 创建要添加设置的 HashTable
  5. Set-AzWebApp... - 将两个 HashTable 结合起来,并用组合集替换现有的 AppSettings。请注意,这假定您的旧设置和新设置之间没有重复项。如果您的情况是如此,您将需要以对您有意义的方式进行去重,即覆盖/不覆盖。

3
答案很不错,但是如果哈希表中包含相同的键,则($oldSettings + $newSettings)无法正常工作。 - Dan Friedman

1

2023年更新

这是一个类似于@robb-vandaveer的实用函数,但会覆盖新设置与旧设置重叠的键。如果交换foreach语句的顺序,可以获得相反的行为。

请注意,它假定您已经连接到Azure。

function Update-WebAppSettings {
    Param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ResourceGroupName,
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$AppName,
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [hashtable]$AdditionalAppSettings
    )

    #common parameters to target the app in the resource group
    $WebAppSelection = @{
        ResourceGroupName = $ResourceGroupName
        Name              = $AppName
    }

    # hashtable where we will build up the complete merged set of app settings
    $NewAppSettings = @{}

    # get the old settings
    $CurrentAppSettingList = $(Get-AzWebApp @WebAppSelection).SiteConfig.AppSettings

    # they are in list format, so iterate through and add 
    # them to the hashtable we are building
    foreach ($Setting in $CurrentAppSettingList) {
        $NewAppSettings[$Setting.Name] = $Setting.Value
    }

    # iterate through the new settings hashtable and add them to the one we are building
    # NB: if new settings have the same keys as old settings it will overwrite them (this 
    # is different behavior than if you merge the two hashtables with the '+' operator)
    foreach ($Key in $AdditionalAppSettings.Keys) {
        $NewAppSettings[$Key] = $AdditionalAppSettings[$Key]
    }

    $WebAppSettings = @{
        AppSettings       = $NewAppSettings
    }

    Set-AzWebApp @WebAppSelection @WebAppSettings
}

在调用地点只需进行以下操作:

$AdditionalAppSettings = @{
    someSetting = "someValue"
    someOtherSetting = "someOtherValue"
}
Update-WebAppSettings -ResourceGroupName my-resource-group -AppName myWebServicesApp -AdditionalAppSettings @AdditionalAppSettings

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接