Powershell 使用Azure Power Shell将应用程序设置添加到现有Azure Web应用程序
我想编写一个使用azure power shell运行的脚本,以自动添加Web应用程序配置 Azure>MyWebApp>应用程序设置>应用程序设置 它看起来像key=“value” 我写这个剧本Powershell 使用Azure Power Shell将应用程序设置添加到现有Azure Web应用程序,powershell,azure,azure-powershell,Powershell,Azure,Azure Powershell,我想编写一个使用azure power shell运行的脚本,以自动添加Web应用程序配置 Azure>MyWebApp>应用程序设置>应用程序设置 它看起来像key=“value” 我写这个剧本 ########################### # MyApp Config Automation # ########################### #Begin $subscriptionName="MySubscriptionName" $webSiteName="MyWeb
###########################
# 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 power shell实现这些步骤或API吗
编辑:
此脚本可以自动创建新的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
检索应用程序设置
首先设置这两个变量
$myResourceGroup = 'RESOURCE_GROUP_NAME'
$mySite = 'SITE_NAME'
然后切换到新的资源管理器模式并登录到您的帐户
Switch-AzureMode AzureResourceManager
Get-AzureAccount
然后检索应用程序设置。(请注意,后面的勾号(`)表示新行。)
添加/更新应用程序设置
要更新设置,请首先将其放入变量中
$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
完整的代码清单
笔记
AzureServiceManagement和AzureResourceManager不适用于同一会话。目前,后者似乎不允许通过
Set AzureResource
更新应用程序设置。以上是一个解决方法。另一种方法是使用Azure CLI而不是PowerShell。以下是基于12/2015 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
这些答案显示了它们的年龄,因为原来的Azure PowerShell和AzureRM都不推荐使用。要使用Az PowerShell模块执行此操作,它将如下所示: 例子 解释
连接AzaAccount
-连接到Azure帐户,如果需要选择订阅,可能需要执行后续步骤$site=Get-AzWebApp
…-检索要修改的站点$oldSettings
…-获取所有现有设置并将其放入哈希表中
$site.SiteConfig.AppSettings |%
-通过ForEach对象的简写别名来管道(传递)每个设置
{$h=@{}
-通过-Begin
位置参数创建哈希表
{$h[$\u.Name]=$\u Value}
-通过-Process
位置参数为$site.SiteConfig.AppSettings
中的每个值向哈希表添加命名值
{$h}
-通过-End
位置参数将新填充的哈希表返回到左边的变量
$newSettings=@{
..-创建要添加的设置的哈希表
Set-AzWebApp
…-组合两个哈希表,并用组合的集合替换现有的AppSettings。请注意,这假设新旧设置之间没有重复项。如果这种情况适用于您,则需要以对您有意义的方式进行重复数据消除,即覆盖/不覆盖你的编辑回答了你的问题吗?@ShaunLuttin我想没有什么比你更详细的答案了,我会检查它并返回给你这是12/2015 Azure PowerShell命令发布后的更好答案。我必须运行此代码段,同时使用Set-AzureRMWebApp和Set-AzureRMWebAppSlot,否则,如果我只运行Set-AzureRMWebApp,暂存插槽中的非插槽特定设置未更新。除非我遗漏了某些内容…还值得注意的是,如果您在azure管道中作为一个步骤运行此设置,则需要
启用AzureRMWebAppsLottProperty
,然后才能使用Set-AzureRMWebAppsLottProperty回答正确,但($oldSettings+$newSettings)
如果散列包含任何相同的键,则将不起作用。
$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
$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
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)