Varun Mehta
Varun Mehta

Reputation: 144

Not able to update AppSettings for Azure App Service using PS cmdlet Set-AzWebApp

This seems like a fairly simple task to do and I have numerous other examples where people have implemented it in the similar fashion. But somehow this doesnt work for me. Here is my script.

$webapp = Get-AzWebApp -Name $webappname -ResourceGroupName $resourcegroupName
$appset = $webapp.SiteConfig.AppSettings
$appset


$newsettings = new-object Microsoft.Azure.Management.WebSites.Models.NameValuePair
$newsettings.Name = "keyName"
$newsettings.Value = "MyValue"
   
$appset.Add($newsettings)
   
$newappset = @{}
$appset | ForEach-Object {
    $newappset[$_.Name] = $_.Value
}

Set-AzWebApp -ResourceGroupName $resourcegroupName -Name $webappname  -AppSettings $newappset
$webapp = Get-AzWebApp -Name $webappname -ResourceGroupName $resourcegroupName

Write-Output "New AppSettings:"
$webapp.SiteConfig.AppSettings

Both before/after gives the same set of configs and nothing changes when I check it on the Azure portal. I don't get any errors as well. Can anyone see what's wrong with the script.

Upvotes: 1

Views: 2529

Answers (2)

Bhargavi Annadevara
Bhargavi Annadevara

Reputation: 5502

Adding to @Satya's response, another workaround is to pass along other SiteConfig properties like -HttpLoggingEnabled $true as mentioned in #15038:

Set-AzWebApp -ResourceGroupName $resourcegroupName -Name $webappname -AppSettings $newappset -HttpLoggingEnabled $true

However, the fix for this issue has now been rolled out and is available as part of the latest Az module versioned 6.0.0 having Az.Websites version 2.6.0. I can confirm that your script works alright with the latest Az module.

Please update the module on your machine with:

Update-Module -Name Az

and retry setting AppSettings.

Upvotes: 1

Satya V
Satya V

Reputation: 4164

Looks like a reported issue here.

Reference : https://github.com/Azure/azure-powershell/issues/15078

enter image description here

enter image description here

I am able to observe the behavior at my end despite using a older Az module 4.2.0. You could try downgrading further and give it a shot.

Alternatively as a workaround, I was able to meet my requirement using the management API.

function update-webappsetting($web,$appsetting)
{
$token = (Get-AzAccessToken).Token 
$url = "https://management.azure.com" + $web.Id + "/config/appsettings?api-version=2020-06-01"
$headers = @{"Content-type"="application/json";"Authorization" = "Bearer $token"}
$body = '{"properties" :' + ($appsetting | ConvertTo-Json)  +  '}'


Invoke-WebRequest -Uri $url -Headers $headers -Body $body -Method PUT
}

To invoke the function :

update-webappsetting -web $web -appsetting $newappset 

Does the exact thing that Set-AzWebApp -Name NAME -ResourceGroupName RG -AppSettings SETTING does

Upvotes: 0

Related Questions