IIS reset handler mappings

Hi all

I have a problem and I’m hoping there is a solution using PowerShell. I have a group of IIS 7 web servers, and somehow the handler mappings are not set to inherit their settings from the parent. This means that the applicationhost.config file is huge, because every application in every website lists every handler individually.
I could go through each one and select ‘Revert to parent’ in IIS, but this would take ages. Similarly I could delete the handlers node for each application from applicationhost.config, but again this would take a long time.

After some reading around I found I can view the handler data for each site and application with these commands
Get-WebConfigurationProperty -filter system.webserver/handlers/add -PSPath ‘IIS:\Sites\Site_Name\Application_Name’-name allowpathinfo
Get-WebConfigurationProperty -filter system.webserver/handlers/add -PSPath ‘IIS:\Sites\Site_Name\Application_Name’-name name
Get-WebConfigurationProperty -filter system.webserver/handlers/add -PSPath ‘IIS:\Sites\Site_Name\Application_Name’-name modules

I can change these values using set-WebConfigurationProperty, but that doesn’t really help me. I need a command that will delete the handler node, or revert it to parent.

I started working on a script, but this is all I’ve got so far…
$IISObject = gci iis:\sites -recurse | where { $.NodeType -eq “directory” -or $.NodeType -eq “application” }

foreach ( $object in $IISObject )

#At this point I think I need something like this:

$handler = Get-WebConfigurationProperty -filter system.webserver/handlers/add -PSPath ‘$Object’ -name allowpathinfo

#but I know it won’t work because I’m guessing. Can anyone offer any tips?

The object that Get-WebConfigurationProperty gets does have a delete method, but you have to put parantheses at the end, like $handler.delete()
That’s all I can tell, I don’t know much abou IIS (yet - I’m gonna buy Jason Helmick’s book on IIS)

Hey Nick! I haven’t had a chance to try this yet – but Clear-WebConfiguration might help - plus I think Istvan has a good idea. I did a quick look and found this article…its not exactly what your looking for…but it might help. When I get a chance I’ll try it.


Hi Istvan & Jason, thanks for these suggestions. I will try both the delete method and Clear-WebConfiguration and report back…

I did some work on this today. First I tried the delete method using Get-WebConfiguration, but this gave me an error.

(Get-WebConfiguration -filter /system.webserver/handlers -PSPath IIS:\Sites\MySite\MyApp).delete()

Exception calling “Delete” with “0” argument(s): “The configuration object is read only, because it has been committed
by a call to ServerManager.CommitChanges(). If write access is required, use ServerManager to get a new reference.”
At line:1 char:149

  • (Get-WebConfiguration -filter /system.webserver/handlers -PSPath IIS:\Sites\MySite\MyApp).delete <<<< ()
    • CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
    • FullyQualifiedErrorId : DotNetMethodException

Next I tried the delete method with Get-WebConfigurationProperty, but again I got an error

(Get-WebConfigurationProperty -filter /system.webserver/handlers/add -name Name).delete()

Method invocation failed because [System.Object] doesn't contain a method named 'delete'.
At line:1 char:88

  • (Get-WebConfigurationProperty -filter /system.webserver/handlers/add -name Name).delete <<<< ()
    • CategoryInfo : InvalidOperation: (delete:String) , RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

This seemed like a strange error, so I checked which methods were available with get-member

Get-WebConfigurationProperty -filter /system.webserver/handlers/add -name Name | gm

TypeName: Microsoft.IIs.PowerShell.Framework.ConfigurationAttribute

Name MemberType Definition

AddToPSObject Method System.Void AddToPSObject(psobject obj)
CompareTo Method int CompareTo(System.Object obj)
Delete Method System.Void Delete()
Equals Method bool Equals(System.Object a)
GetHashCode Method int GetHashCode()
GetMetadata Method System.Object GetMetadata(string metadataType)
GetType Method type GetType()
SetMetadata Method System.Void SetMetadata(string metadataType, System.Object value)
ToPSObject Method psobject ToPSObject()
ToString Method string ToString()
Update Method bool Update(psobject data)
ItemXPath NoteProperty System.String ItemXPath=/system.webServer/handlers/add[@name='ScriptHandler…
IsExtended Property System.Boolean IsExtended {get;}
IsInheritedFromDefaultValue Property System.Boolean IsInheritedFromDefaultValue {get;}
IsProtected Property System.Boolean IsProtected {get;}
Name Property System.String Name {get;}
Schema Property Microsoft.IIs.PowerShell.Framework.ConfigurationAttributeSchema Schema {get;}
TypeName Property System.String TypeName {get;}
Value Property System.Object Value {get;set;}

The delete method is there, so I'm not sure why I get the error

So I thought I would try Clear-WebConfiguration

I used set-location to navigate the iis:\ provider, and set the location as one of the Applications, then ran get-WebConfiguration followed by Clear WebConfiguration

get-WebConfiguration -filter /system.webserver/handlers

SectionPath PSPath Location

/system.webServer/handlers MACHINE/WEBROOT/APPHOST/MySite…

clear-WebConfiguration -filter /system.webserver/handlers

Clear-WebConfiguration didn't give any errors and appeared to be successful, but I couldn't see any evidence that it had changed anything.

I'll run a few more tests tomorrow to see if I can get any further.