0

我正在尝试向<remove />web.config 添加一个密钥以删除 WebDAV 模块。目前 web.config 的配置是...

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>  
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\Server.Host.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    </system.webServer>
  </location>
</configuration>

...并且我想使用 PowerShell 以编程方式将其更新为以下...

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <modules>
        <remove name="WebDAVModule" />
      </modules>    
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\Server.Host.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    </system.webServer>
  </location>
</configuration>

我曾尝试使用 PowerShell IIS 模块 Remove-WebConfigurationProperty,但无法让它添加块。

Remove-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST/Default Web Site/webapp'  -Filter "system.webServer/modules" -Name "WebDAVModule" -Location "."

任何帮助表示赞赏。

4

1 回答 1

1

谢谢@Theo,这是一个很好的建议。我使用以下 PowerShell 脚本来更新 web.config。

$PathWebConfig = "C:\web.config";
[xml]$xmlDoc = Get-Content($PathWebConfig);
$Node = $xmlDoc.configuration.location.'system.webServer'.modules;

if ($Node -eq $null) {
    Write-Host "Node doesn't exist.";
    $xmlNodeModules = $xmlDoc.CreateNode("element","modules","");
    $xmlKeyRemove = $xmlDoc.CreateNode("element","remove","");
    $xmlKeyRemove.SetAttribute("name","WebDAVModule");
    $xmlNodeModules.AppendChild($xmlKeyRemove) | Out-Null;
    $xmlDoc.configuration.location.'system.webServer'.AppendChild($xmlNodeModules) | Out-Null;
    $xmlDoc.save($PathWebConfig);
} else {
    Write-Host "Node exists.";

}
于 2020-06-16T23:14:51.607 回答