AzureRM Web App - 如何使用Powershell控制插槽设置

时间:2016-12-15 21:18:11

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

我想使用PowerShell设置Azure网络应用程序的连接字符串和应用程序设置。我希望这些设置能够与插槽保持一致,而不是在交换时使用应用程序。

应用设置的代码如下所示,并且有效:

$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应用程序连接字符串的插槽设置复选框(或将其配置为"粘贴"?

3 个答案:

答案 0 :(得分:2)

请尝试使用以下代码将连接字符串设置为插槽的粘滞设置。它适用于我。有关使用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

enter image description here

答案 1 :(得分:1)

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

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

Set-AzureRmWebAppSlotConfigName -ResourceGroupName MyRg -Name MyWebApp -RemoveAllConnectionStringNames

答案 2 :(得分:0)

$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/