问题
I am adding a WebDAV authoringRule using
Add-WebConfiguration /system.webserver/webdav/authoringRules -PSPath IIS: -Location "$site_name/$app_name/VD" -Value @{users="*";path="*";access="Read,Write"}
In some environments, this is conflicting with the same authoring rule in the parent, and thus throwing an error. I want to add a clear element to the start of the authoringRules so it looks something like this
<authoringRules>
<clear />
<add users="*" path="*" access="Read, Write" />
</authoringRules>
But Clear-WebConfiguration
only clears the existing rules. How do I use powershell to add a <clear />
element to the config file?
回答1:
I believe you are referring to applicationHost.config.
I also found this an issue with the WebAdministration commandlets, I solved it using the the ServerManager class. My particular issue was with the windowsAuthentication providers collection, but I see no reason why this wouldn't work for you.
You may have to correct the path in the call to GetSection but this should get you very close to what you need.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration") | Out-Null
$serverManager = new-object Microsoft.Web.Administration.ServerManager
$config = $serverManager.GetApplicationHostConfiguration()
$authoringRulesSection = $config.GetSection("/system.webserver/webdav/authoringRules", "$($site_name)/$($app_name)/VD");
# Grab a reference to the collection of authoringRules
$authoringRulesCollection = $authoringRulesSection.GetCollection("authoringRules");
# Clear the current collection this also adds the <clear /> tag
$authoringRulesCollection.Clear();
# Add to the collection
$addElement = $authoringRulesCollection.CreateElement("add")
$addElement["users"] = "*";
$addElement["path"] = "*";
$addElement["access"] = "Read, Write";
$authoringRulesCollection.Add($addElement);
# Save the updates
$serverManager.CommitChanges();
来源:https://stackoverflow.com/questions/16619944/add-a-clear-element-to-webdav-authoringrules-using-powershell