0
votes

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"?

3
Do you have any updates about this thread?Tom Sun - MSFT
I tested and accepted the answer below.Jay Godse

3 Answers

2
votes

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

enter image description here

1
votes

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