IIS reset handler mappings

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Nick Chard Nick Chard 2 years, 5 months ago.

  • Author
    Posts
  • #16693
    Profile photo of Nick Chard
    Nick Chard
    Participant

    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
    $handler.delete

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

  • #16700
    Profile photo of Istvan Szarka
    Istvan Szarka
    Participant

    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)

  • #16702
    Profile photo of Jason Helmick
    Jason Helmick
    Keymaster

    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.

    http://troyparsons.com/blog/2012/06/how-to-remove-isapi-filters-and-script-mappings-from-iis7-with-powershell/

  • #16715
    Profile photo of Nick Chard
    Nick Chard
    Participant

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

  • #16734
    Profile photo of Nick Chard
    Nick Chard
    Participant

    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: (:) [], 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.

You must be logged in to reply to this topic.