简体   繁体   中英

How to add a <remove /> block using Remove-WebConfigurationProperty to a web.config?

I'm trying to add a <remove /> key to a web.config to remove the WebDAV module. Currently the configuration for the web.config is...

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

... and I would like to update it programmatically using PowerShell to the following...

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

I have tried to use the PowerShell IIS module Remove-WebConfigurationProperty, but I can't get it to add the block.

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

Any help is appreciated.

Thanks @Theo, that was a good suggestion. I used the following PowerShell script to update the 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.";

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM