Quick bit – How to remove isapi filters and script mappings from IIS7 with Powershell

Written by Troy on June 27, 2012 Categories: Uncategorized Tags: , , , , ,

The documentation for the WebAdministration module, to administer IIS7 with Powershell, has simple examples of how to add many things. Removing items in the pipeline is a little less clear and requires a bit of trial-and-error. Here’s a quick example that hopefully saves you a bit of digging:

$isapiName = "fooBarIsapi.dll"

# Remove the ISAPI filter from restriction list
Get-WebConfiguration -PSPath IIS:\Sites -Filter "/system.webServer/security/isapiCgiRestriction/add" `
	| where { $_.Path -ilike "*$isapiName" } `
	| % { Clear-WebConfiguration -PSPath $_.PSPath -Filter $_.ItemXPath -Location $_.Location }

# Remove all instances of the ISAPI filter
Get-WebConfiguration -PSPath IIS:\Sites -Recurse -Filter "/system.webServer/isapiFilters/filter" `
	| where { $_.Path -ilike "*$isapiName" } `
	| % { Clear-WebConfiguration -PSPath $_.PSPath -Filter $_.ItemXPath -Location $_.Location }

# Remove all script handlers leveraging the ISAPI filter
Get-WebConfiguration -PSPath IIS:\Sites -Recurse -Filter "/system.webServer/handlers/add" `
	| where { $_.scriptProcessor -ilike "*$isapiName" } `
	| % { Clear-WebConfiguration -PSPath $_.PSPath -Filter $_.ItemXPath -Location $_.Location }

You may even like to encapsulate these into functions to make your pipeline a little more manageable.

function Get-WebIsapiFilters
{
	Get-WebConfiguration -PSPath IIS:\Sites -Recurse -Filter "/system.webServer/isapiFilters/filter"
}

function Get-WebScriptHandlers
{
	Get-WebConfiguration -PSPath IIS:\Sites -Recurse -Filter "/system.webServer/handlers/add"
}

function Clear-WebItem
{
	Clear-WebConfiguration -PSPath $_.PSPath -Filter $_.ItemXPath -Location $_.Location
}

# Do I have any 'foo' script handlers?
Get-WebScriptHandlers | where { $_.scriptProcessor -ilike "*foobar.dll" }

# Delete some ISAPI filters...
Get-WebIsapiFilters | where { $_.Path -ilike "*fooIisapi.dll" } | % { Clear-WebItem }
No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>