0
votes

I am attempting to edit my WebApp's Web.config using the Kudu WebApp API and an Azure Automation Runbook.

Logging into the WebApp via the Kudu interface and executing the code in the PowerShell debugger removes the XML node as expected:

$username = "`$myusername"
$password = "123456"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password))) 

$ProgressPreference="SilentlyContinue"

$apiUrl = "https://mysite.scm.azurewebsites.net/api/command"

$pscommand = $webConfigPath = "D:\home\site\wwwroot\Web.config"
[xml] $webConfigXML = Get-Content $webConfigPath
#remove the following handler in the <httpHanderls> section in the web.config
$targetName = "Sitecore.FeedRequestHandler"
$nodePath = "configuration/system.webServer/handlers/add[@name='{0}']" -f $targetName
$node = $webConfigXML.SelectSingleNode($nodePath)
if($node -ne $null)
{
    $webConfigXML.configuration.'system.webServer'.handlers.RemoveChild($node)
}
$webConfigXML.Save($webConfigPath)

$commandBody = @{
    command = "powershell -command `"$pscommand`""
}

Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST -ContentType "application/json" -Body (ConvertTo-Json $commandBody)

However, if I try to run this via PowerShell ISE or an Azure Automation Runbook, it produces the following errors:

Program 'Web.config' failed to run: Access is deniedAt line:1 char:1..

Get-Content : Cannot find path 'D:\home\site\wwwroot\Web.config' because it does not exist.

Any ideas on how I can edit the XML of the Web.config via an Azure Automation Runbook?

1
What exactly do you mean by 'PowerShell debugger'?David Ebbo
The debug console of Kudu. It has a PowerShell screenKode

1 Answers

1
votes

Looking at your code, I think it is confusing two model:

  1. Some of it is written to call Kudu from the outside via http, when you call the /api/command API.
  2. Some of it is written to run directly from within Kudu, when you try to access D:\home\site\wwwroot\Web.config on the local file system.

2 has no chance of working if you run anywhere other than in Kudu.

What you need to do instead is use the Kudu vfs API to read the file, modify it locally, and then save it back using the vfs API.