Azure Kudu API - 无法通过 Azure 自动化编辑 Web.config

Azure Kudu API - Can't Edit Web.config via Azure Automation

我正在尝试使用 Kudu WebApp API 和 Azure Automation Runbook 编辑我的 WebApp Web.config。

通过 Kudu 界面登录到 WebApp 并在 PowerShell 调试器中执行代码会按预期删除 XML 节点:

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

但是,如果我尝试通过 PowerShell ISE 或 Azure Automation Runbook 运行 这样做,它会产生以下错误:

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.

关于如何通过 Azure Automation Runbook 编辑 Web.config 的 XML 有什么想法吗?

查看您的代码,我认为它混淆了两个模型:

  1. 有些写的是通过http从外部调用Kudu,当你调用/api/command API.
  2. 当您尝试访问本地文件系统上的 D:\home\site\wwwroot\Web.config 时,其中一些直接从 Kudu 内部写入 [​​=27=]。

2 如果你 运行 除了在 Kudu 以外的任何地方,就没有机会工作。

您需要做的是使用 Kudu vfs API 读取文件,在本地修改它,然后使用 vfs API.

将其保存回来