【问题标题】:AzureRM Web App - How to control the slot setting with PowershellAzureRM Web App - 如何使用 Powershell 控制插槽设置
【发布时间】:2017-09-26 21:28:02
【问题描述】:

我想使用 powershell 设置我的 Azure Web 应用程序的连接字符串和应用程序设置。而且我希望这些设置保留在插槽中,而不是在交换时保留在应用程序中。

应用设置的代码如下所示,并且可以正常工作:

$PropertiesObject = @{"SMTPUser"="myuser"; "SMTPPassword"="secretpwd";}
$webAppName = "mywebapp"
$slotName = "demo"
$resourceGroupName = "myResourceGroup"

New-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/slots/config -ResourceName $webAppName/$slotName/appsettings -ApiVersion 2015-08-01 -Force


$stickSlotConfigObject = @{"connectionStringNames"=@(); "appSettingNames" = @("SMTPUserName","SMTPPassword");}

$result = Set-AzureRmResource -PropertyObject $stickSlotConfigObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $webAppName/slotConfigNames -ApiVersion 2015-08-01 -Force

这行得通。当我在 Azure 门户中转到 Web 应用程序的插槽刀片时,会根据需要选中“插槽设置”复选框。

我正在为如何设置 连接字符串 以同时选中“插槽设置”框而苦恼。我尝试了以下,

$PropertiesObject =  @{ 
  AzureWebJobsStorage = @{  
    Type = "Custom"; 
    Value = "somestring"
  };
  Common = @{   
    Type = "SQLAzure"; 
    Value = "somedatabasestring" 
  };
};

$webAppName = "mywebapp"
$slotName = "demo"
$resourceGroupName = "myResourceGroup"

New-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/slots/config -ResourceName $webAppName/$slotName/appsettings -ApiVersion 2015-08-01 -Force

$stickSlotConfigObject = @{"appSettingNames"=@();"connectionStringNames"=@("AzureWebJobsStorage","Common"); }

$result = Set-AzureRmResource -PropertyObject $stickSlotConfigObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $webAppName/appsettings -ApiVersion 2015-08-01 -Force

这不起作用。我收到以下错误:

New-AzureRmResource : {"Code":"BadRequest","Message":"The parameter properties has an invalid value.","Target":null,"Details":[{"Message":"The parameter properties has an invalid value."},{"Code":"BadRequest"},{"ErrorEntity":{"Code":"BadRequest","Message":"The parameter properties has an invalid value.","ExtendedCode":"51008","MessageTemplate":"The parameter {0} has an invalid value.","Parameters":["properties"],"InnerErrors":null}}],"Innererror":null}

我尝试了另一个调整(我忘记了),它说 $PropertiesObject 对象的格式不正确。

如何在 Powershell 中对其进行编码,以便我可以检查 Web 应用连接字符串的插槽设置复选框(或将其配置为“粘性”?

【问题讨论】:

  • 你有关于这个线程的任何更新吗?
  • 我测试并接受了下面的答案。

标签: powershell connection-string sticky azure-resource-manager azure-web-app-service


【解决方案1】:

请尝试使用以下代码将连接字符串设置为插槽的粘性设置。它对我来说正常工作。有关使用 PowerShell ARM 方式自动化 Azure WebApps 的更多信息,请参阅document.

$connectionString = @{}
$webAppName = "Web AppName"
$resourceGroup ="Resource Group Name"
$slotName ="slot Name" 
$connectionString.Add("AzureWebJobsStorage", @{ value = "The Actual connecting string here" ; Type = 3 }) #Custom
$connectionString.Add("Common", @{ value = "The Actual connecting string here" ; Type = 2 }) #Azure SQL

  Login-AzureRmAccount
  # creat slot connection string
  New-AzureRmResource -PropertyObject $connectionString `
  -ResourceGroupName $resourceGroup `
  -ResourceType "Microsoft.Web/sites/slots/config" `
  -ResourceName "$webAppName/$slotName/connectionstrings" `
  -ApiVersion 2015-08-01 -Force

  # set connection string  as sticky setting 
  $stickSlotConfigObject =  @{"connectionStringNames" = @("AzureWebJobsStorage","Common")} #connection string Name
  Set-AzureRmResource -PropertyObject $stickSlotConfigObject `
  -ResourceGroupName $resourceGroup `
  -ResourceType Microsoft.Web/sites/config `
  -ResourceName  $webAppName/slotConfigNames `
  -ApiVersion 2015-08-01 -Force

【讨论】:

  • 谢谢。我是如此接近。 "$webAppName/$slotName/connectionstrings" 和 $webAppName/slotConfigNames 是我所缺少的。我无法从文档中得知如何命名子哈希。此外,resources.azure.com 门户网站也有很大帮助!该门户应该链接到 portal.azure.comm 中的每个页面,以便人们可以轻松地弄清楚如何使用 REST 或 Powershell 编写脚本。
【解决方案2】:

现在有两个新的 cmdlet 可以控制插槽设置:Get-AzureRmWebAppSlotConfigNameSet-AzureRmWebAppSlotConfigName

例如,我想确保我的连接字符串不是插槽配置,所以我执行了:

Set-AzureRmWebAppSlotConfigName -ResourceGroupName MyRg -Name MyWebApp -RemoveAllConnectionStringNames

【讨论】:

    【解决方案3】:
    $resourceName = $webappname + “/slotconfigname”
    $stickySlot = Get-AzureRmResource -ResourceName $resourceName -ResourceGroupName -ResourceType “Microsoft.Web/Sites/config” -ApiVersion “2015-08-01”
    

    然后您可以通过以下方式检查现有的:

    $stickySlot.Properties.AppSettingNames
    

    在这里你需要不同的方法。如果这些从一开始就为空,则需要使用设置创建一个新数组:

    $settings = @(“AppSetting1, “AppSetting2”)
    $stickySlot.Properties.AppSettingNames = $settings
    

    如果已经有其他值,并且您想保留它们:

    $stickySlot.Properties.AppSettingNames += “AppSetting1”
    $stickySlot.Properties.AppSettingNames += “AppSetting2”
    

    然后完成:

    Set-AzureRmResource -ResourceName $resourceName -ResourceGroupName -ResourceType “Microsoft.Web/Sites/config” -Properties $stickySlot.Properties -ApiVersion “2015-08-01"
    

    取自:https://msftplayground.com/2016/02/adding-azure-app-service-application-settings-powershell/

    【讨论】:

    • 第一个命令不起作用,即使我输入了 $resourceGroupName。
    猜你喜欢
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 2016-04-18
    • 2023-03-17
    • 2020-01-08
    • 1970-01-01
    相关资源
    最近更新 更多