Que tal isso:
[Reflection.Assembly]::Load(
"Microsoft.Web.Administration, Version=7.0.0.0,
Culture=Neutral, PublicKeyToken=31bf3856ad364e35") > $null
$serverManager = New-Object Microsoft.Web.Administration.ServerManager
# Default Web Site
$siteId = 1
$site = $serverManager.Sites | where { $_.Id -eq $siteID }
#
# Sets the filter in the site's web.config
#
$config = $site.GetWebConfiguration()
$requestFilteringSection = '
$config.GetSection("system.webServer/security/requestFiltering")
#
# Or delete last two lines and use next two instead to set in applicationHost.config
#
# $config = serverManager.GetApplicationHostConfiguration();
# $requestFilteringSection = '
$config.GetSection("system.webServer/security/requestFiltering", $site.Name);
$fileExtensionsElement = '
$requestFilteringSection.GetChildElement("fileExtensions")
$fileExtensionsCollection = $fileExtensionsElement.GetCollection()
$addElement = $fileExtensionsCollection.CreateElement("add")
$addElement["fileExtension"] = ".txt"
$addElement["allowed"] = $false
$fileExtensionsCollection.Add($addElement)
$serverManager.CommitChanges()
Você também pode usar appcmd.exe
:
Configure no site web.config
:
appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"fileExtensions.[fileExtension='.txt',allowed='False']"
Ou configure em applicationHost.config
:
appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"fileExtensions.[fileExtension='.bong',allowed='False']" /commit:apphost