Partager via


Enable the IIS 6 response compression

One of my projects required to enable the IIS 6 response compression. Here how I did it:

set initialFolder=%cd%
cd c:\Inetpub\AdminScripts
net stop iisadmin /Y
cscript adsutil.vbs set w3svc/filters/compression/parameters/HcDoDynamicCompression true
cscript adsutil.vbs set w3svc/filters/compression/parameters/HcDoStaticCompression true
cscript adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcFileExtensions "htm" "html" "txt" "application" "manifest" "deploy" "exe" "dll"
cscript adsutil.vbs set W3Svc/Filters/Compression/DEFLATE/HcFileExtensions "htm" "html" "txt" "application" "manifest" "deploy" "exe" "dll"
cscript adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "dll" "exe" "aspx" "asmx"
cscript adsutil.vbs set W3Svc/Filters/Compression/DEFLATE/HcScriptFileExtensions "asp" "dll" "exe" "aspx" "asmx"
rem todaydate=_YYYY-MM-DD_hhmmss
set todaydate=_%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
rem backup metabase
copy %windir%\system32\inetsrv\MetaBase.xml %windir%\system32\inetsrv\MetaBase%todaydate%.xml.old
rem add a HTTP extension
cscript %initialFolder%\IISExtension.vbs
net start w3svc
rem back to the initial folder
%initialFolder:~0,2%
cd %initialFolder%

Create a batch file with this content and execute it.

And here is the vbs file:

strComputer = "."
'
' add a HTTP extension
'
Set objWMIService = GetObject ("winmgmts:{authenticationLevel=pktPrivacy}\\" & strComputer & "\root\microsoftiisv2")
Set colItems = objWMIService.ExecQuery ("Select * From IIsWebService")
WScript.Echo "Search if the extension already exists"
Dim aExt
aExt = Array()
Set IIsWebServiceObj = GetObject("IIS://localhost/W3SVC")
aExt = IIsWebServiceObj.ListExtensionFiles
Dim exists
exists = 0
For ValueIndex = 0 To UBound(aExt)
WScript.Echo aExt(ValueIndex)
if ( aExt(ValueIndex) = "C:\WINDOWS\System32\inetsrv\gzip.dll") then
exists = 1
WScript.Echo "The extension already exists"
end if
Next
if (exists = 0) then
WScript.Echo "Add the extension"
For Each objItem in colItems
objItem.AddExtensionFile "C:\WINDOWS\System32\inetsrv\gzip.dll", True, "HTTPEXT", True, "HTTP Compression"
Next
end if
'
' modify the metabase.xml file
'
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\MicrosoftIISv2")
Set objIIsComputer = objWMIService.Get("IIsComputer='LM'")

'connect to the IIsCompressionSchemeSetting class gzip instance
Set objGzipParameter = objWMIService.Get("IIsCompressionSchemeSetting='W3SVC/Filters/Compression/gzip'")

'modify the compression level
objGzipParameter.HcDynamicCompressionLevel = 9

objGzipParameter.Put_()

'save the data to the metabase file
objIIsComputer.SaveData()