'******************************************************************************* '* '* File: ModifyHTTPCompression.vbs '* Author: Ethan Wilansky '* Created: August 2004 '* Version: 1.0 '* '* Description: This script modifies common HTTP compression '* settings in IIS 6.0 using WMI. It backs-up the IIS 6.0 '* metabase, makes changes to the metabase.xml file in '* %systemroot%\system32\inetsrv, then stops and restarts '* IIS 6.0. Use this script at your own risk. Test it carefully '* before deploying the changes to a production environment. '* '* Dependencies: '* 1. You must run this tool on a Windows Server 2003 '* computer with WMI and IIS 6.0 installed. '* '* 2. You must have administrative privileges on the '* Windows Server 2003 computer running IIS 6.0 '* '* IMPORTANT: There is no error handling in this script. For production '* use, consider adding error handling routines. Some debugging '* routines and commented calls remain in the script. If you are '* having trouble running the script, consider uncommenting some '* some of these simple debugging statements. '******************************************************************************** 'Constants Const DUPLICATE_EXTENSION = &h80070034 Const MD_BACKUP_SAVE_FIRST = &h2 Const MD_BACKUP_FORCE_BACKUP = &h4 Const MD_BACKUP_NEXT_VERSION = &HFFFFFFFF 'Change the value of strComputer to the computer where 'you want the changes made. You can use a NetBIOS name (as shown), 'an IP address, or a FQDN. strComputer = "atl-dc-02" 'Connect to the WMI MicrosoftIISv2 namespace Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\MicrosoftIISv2") '***************************************************** 'Backup the metabase before making changes to it Set objIIsComputer = objWMIService.Get("IIsComputer='LM'") 'Call the BackupWithPassword method. Specifying an empty string 'for the BackupLocation parameter stores the backup in the %systemroot%\System32\Inetsrv\MetaBack folder. 'MD_BACKUP_NEXT_VERSION backs-up to the next available version number in the default save 'location (metaback folder). 'MD_BACKUP_FORCE_BACKUP forces a backup even if the save data first operation fails. For more information 'about this, see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/iis/ref_mof_iiscomputer_backupwithpassword.asp. 'No backup/restore password was specified. strBackupPrefix="IISBackup" intBackupFlags = _ (MD_BACKUP_SAVE_FIRST + MD_BACKUP_FORCE_BACKUP) Call objIIsComputer.BackupWithPassword(strBackupPrefix, MD_BACKUP_NEXT_VERSION, intBackupFlags) '***************************************************** '***************************************************** 'Connect to the IIsCompressionSchemesSetting class Parameters instance 'to configure some of the HTTP compression settings Set objParameter = objWMIService.Get("IIsCompressionSchemesSetting='W3SVC/Filters/Compression/Parameters'") 'Look at existing settings (for debugging only) 'Call DisplayParameters() 'Enable the Compress application files checkbox objParameter.HcDoDynamicCompression = true 'Enable the Compress static files checkbox objParameter.HcDoStaticCompression = true 'Set the static file compression temporary directory. The default 'is %windir%\IIS Temporary Compressed Files" objParameter.HcCompressionDirectory="%windir%\IIS Compressed Files" 'Enable disk space limits. Default is unlimited (false). objParameter.HcDoDiskSpaceLimiting = true 'Set the size of the compressed file directory. This value is in bytes. 'For example, 99614720 bytes * 1 mb/1048576 bytes = 95 mb objParameter.HcMaxDiskSpaceUsage=262144000 'Save the changes to the metabase. objParameter.Put_() 'Check to see what parameters were set (for debugging only) 'CheckParameters() '***************************************************** '***************************************************** 'HTTP compression settings not in the IISMgr interface. 'Connect to the IIsCompressionSchemeSetting class gzip instance Set objGzipParameter = objWMIService.Get("IIsCompressionSchemeSetting='W3SVC/Filters/Compression/gzip'") 'Enable ondemand compression for static files. objGzipParameter.HcDoOnDemandCompression=True 'Read about and then carefully test compression levels 'before modifying the compression level objGzipParameter.HcDynamicCompressionLevel = 5 'Add extensions for static file types you want to compress. 'This script example adds .pdf files to the list of static file types 'to compress. 'Get the existing array of extensions arrCurrentExtensions = objGzipParameter.HcFileExtensions 'Check to see if the FileExtensionExists (for debugging only) 'Wscript.Echo "File Extension Exists is " & FileExtensionExists(arrCurrentExtensions,"pdf") 'Call the AppendExtension function to add an extension to the existing list 'of HcFileExtensions extensions 'FOR DEBUGGING ONLY!!! ADD THE EXTENSIONS BACK IN... 'objGzipParameter.HcFileExtensions = Array("txt", "htm", "html") 'objGzipParameter.HcScriptFileExtensions = Array("asp", "dll", "exe") 'objGzipParameter.Put_() NewStaticExtensionList = AppendExtension(arrCurrentExtensions,"pdf") If IsArray(NewStaticExtensionList) = True Then objGzipParameter.HcFileExtensions = NewStaticExtensionList 'Save changes to the metabase. objGzipParameter.Put_() End If 'Call the AppendExtension function to add an extension to the existing list 'of HcScriptFileExtensions extensions 'Get the existing array of dynamic file types you want to compress arrCurrentDynExtensions = objGzipParameter.HcScriptFileExtensions 'Call the AppendExtension function to add an extension to the existing list 'of HcScriptFileExtensions extensions NewDynamicExtensionList = AppendExtension(arrCurrentDynExtensions,"aspx") If IsArray(NewDynamicExtensionList) = True Then objGzipParameter.HcScriptFileExtensions = NewDynamicExtensionList 'Save changes to the metabase. objGzipParameter.Put_() End If '***************************************************** 'Add the gzip ISAPI dll for compressing HTTP content 'First check to see if it's already there If CheckForExtension("c:\windows\system32\inetsrv\gzip.dll", "HTTP Compression") = false Then 'To add a web service extension using WMI 'Connect to the IIsWebService class W3SVC instance Set objWebService = objWMIService.Get("IIsWebService='W3SVC'") Call objWebService.AddExtensionFile _ ("c:\windows\system32\inetsrv\gzip.dll",true,"HTTP Compression",true,"HTTP Compression") End If 'Look at existing list of web service extensions (for debugging only) 'Wscript.Echo ReturnExtensions() '***************************************************** '***************************************************** 'Explicitly save the data to the metabase so the changes are written to metabase.xml immediately objIIsComputer.SaveData() '***************************************************** '***************************************************** 'Reset IIS for all of the changes to take affect. 'Stop IIS Set objIIsWebService = objWMIService.Get("IIsWebService='W3SVC'") objIIsWebService.StopService 'Restart IIS... objIIsWebService.StartService '***************************************************** '***************************************************** 'Return information to the console to inform the operator of what just happened WScript.echo "A backup of the metabase was run, the HTTP compression changes" & VbCrlf & _ "were made, the changes were saved back to metabase.xml" & VbCrLf & _ "and the W3SVC was stopped and started on " & strComputer & "." '***************************************************** 'Functions and routines '***************************************************** 'To enumerate web service extensions using WMI Function ReturnExtensions 'Connect to the IIsWebServiceSetting class Set objWebServiceSetting = objWMIService.Get("IIsWebServiceSetting='W3SVC'") ' Obtain the value of the Web service extension restriction list property. arrWebSvcExtRestrictionList = objWebServiceSetting.WebSvcExtRestrictionList ' List each extension record For Each extension in arrWebSvcExtRestrictionList If extension.Access = 1 Then strAccess = "Allowed" Else strAccess = "Prohibited" End If strExtension = strExtension & vbCrlf & _ strAccess & vbcrlf & _ extension.FilePath & vbcrlf & _ extension.ServerExtension & vbcrlf & _ extension.Deletable & vbcrlf & _ extension.Description & vbcrlf & _ "--------------" Next ReturnExtensions = strExtension End Function Function CheckForExtension(FilePath,ServerExtension) 'Connect to the IIsWebServiceSetting class Set objWebServiceSetting = objWMIService.Get("IIsWebServiceSetting='W3SVC'") ' Obtain the value of the Web service extension restriction list property. arrWebSvcExtRestrictionList = objWebServiceSetting.WebSvcExtRestrictionList ' List each extension record CheckForExtension = false For Each extension in arrWebSvcExtRestrictionList If extension.FilePath = FilePath or _ extension.ServerExtension = ServerExtension Then CheckForExtension = true Exit Function End If Next End Function 'To avoid writing the same new extension, first check 'to see if the extension is already there. Function FileExtensionExists(ExistingArray, ExtensionName) FileExtensionExists = false For each ext in ExistingArray If ExtensionName = ext Then FileExtensionExists = true Next End Function 'Append one extension to an array of extensions. This function is limited to 'adding one extension. However, you can call it multiple times. Alternatively, improve the 'function to pass in one or more items to the array. Function AppendExtension(ExistingArray, ExtensionToAdd) 'Wscript.Echo "The file extension " & ExtensionToAdd & " exists: " & FileExtensionExists(ExistingArray, ExtensionToAdd) If FileExtensionExists(ExistingArray, ExtensionToAdd) = False Then ' Redimension the array to add space for 1 more entry arrSize = UBound(ExistingArray) ReDim Preserve ExistingArray(arrSize + 1) ExistingArray(arrSize + 1) = ExtensionToAdd AppendExtension = ExistingArray End If End Function 'Check the existing parameters Sub CheckParameters() For each parameter in objParameter.Properties_ Wscript.echo parameter.name & "=" & parameter.value Next End Sub