AzureRM Web App - How to control the slot setting with Powershell

谁说胖子不能爱 提交于 2019-12-12 03:54:15

问题


I would like to set the connection strings and app settings of my Azure web app using powershell. And I would like those settings to stick with the slot, and not with the app when it is swapped.

The code for app settings looks like this and it works:

$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

This works. When I go to the slot blade of the web app in the Azure portal, the "Slot Setting" check box is checked as I want it to be.

I'm struggling with how to set the connection strings to also have the "slot setting" box checked. I tried the following,

$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

This did not work. I got the following error:

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}

I tried another tweak (which I forgot) and it said that the $PropertiesObject object was not in the right format.

How do I code it in Powershell so that I can check the slot setting check box of a web app connection string (or configure it as "sticky"?


回答1:


Please have a try with the following code to set connection string as sticky setting for slot. It works correctly for me. More info about automating Azure WebApps with PowerShell ARM way please refer to the 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




回答2:


There are now two new cmdlets to control the slot settings: Get-AzureRmWebAppSlotConfigName and Set-AzureRmWebAppSlotConfigName

For instance, I wanted to make sure that my connection strings weren't a slot config so I executed:

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”

You can then check the existing ones by:

$stickySlot.Properties.AppSettingNames

Here you need to different approaches. If these are empty from the get go, you need to create a new array with settings:

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

If there already are other values, and you want to keep them:

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

Then after that is done:

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

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



来源:https://stackoverflow.com/questions/41173482/azurerm-web-app-how-to-control-the-slot-setting-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!