Chapter 8: IIS Site Operations Code
Backing Up the IIS Metabase Using a VBScript Active Server Page
Dim IisComputer Dim Flags Dim TargetComputer TargetComputer = "Target_Server_Name" Flags = (MD_BACKUP_SAVE_FIRST Or MD_BACKUP_FORCE_BACKUP) Set IIsComputer = GetObject("IIS://" & TargetComputer) IIsComputer.Backup "MyBackup10", MD_BACKUP_NEXT_VERSION, Flags
Enumerating Existing Backups Using a VBScript Active Server Page
On Error Resume Next Dim IIsComputer Dim TargetComputer Dim Version Dim Index Dim TermCond Dim Location Dim UTCDate TargetComputer = "Target_Server_Name" Set IIsComputer = GetObject("IIS://" &TargetComputer) Do While TermCond <> 1 IIsComputer.EnumBackups "", Index, Version, Location, UTCDate If Err.Number <> 0 Then Exit Do End If Response.Write Location & " Version: "& Version & " " & UTCDate & "<BR>" Index = Index + 1 Loop
Restoring an Existing Metabase Backup Using a VBScript Active Server Page
Dim IIsComputer Dim TargetComputer Dim BackupLocation TargetComputer = "Target_Server_Name" BackupLocation = "Backup_Location_On_Target_Server" Set IIsComputer = GetObject("IIS://" & TargetComputer) IIsComputer.Restore BackupLocation, MD_BACKUP_HIGHEST_VERSION, 0
Deleting an Existing Metabase Backup Using a VBScript Active Server Page
Dim IIsComputer Dim TargetComputer Dim BackupLocation TargetComputer = "Target_Server_Name" BackupLocation = "Backup_Location" Set IIsComputer = GetObject("IIS://" & TargetComputer) IIsComputer.DeleteBackup BackupLocation, MD_BACKUP_HIGHEST_VERSION
Querying MaxBandwidth Using a VBScript Active Server Page
Dim IIsComputer Dim TargetComputer Dim RetVal TargetComputer = "Target_Server_Name" Set IIsComputer = GetObject("IIS://" & TargetComputer) Response.Write IIsComputer.MaxBandwidth
Setting a New Value for MaxBandwidth Using a VBScript Active Server Page
Dim IIsComputer Dim TargetComputer Dim NewValue TargetComputer = "Target_Server_Name" NewValue = New_Throttle_Value_In_Bytes Set IIsComputer = GetObject("IIS://" & TargetComputer) IIsComputer.MaxBandwidth = NewValue IIsComputer.SetInfo
Viewing the List of Current Server-Defined MIME Type Mappings Using a VBScript Active Server Page
Dim IIsComputer Dim TargetComputer Dim MimeMapping TargetComputer = "Target_Server_Name" Set IIsComputer = GetObject("IIS://" & TargetComputer & "/MimeMap") Response.Write "Registered File Types:" & "<BR>" For Each MimeMapping in IISComputer.MimeMap Response.Write "Extension: " & MimeMapping.Extension & " MIME Content Type: "& MimeMapping.MimeType&"<BR>" Next
Adding a New Server-Defined MIME Mapping Using a VBScript Active Server Page
Dim IIsComputer Dim TargetComputer Dim MimeMapping Dim NewMimeMapping Dim MimeExtension Dim MimeType Dim i TargetComputer = "Target_Server_Name" MimeExtension = "New_MIME_Extension" MimeType = "New_MIME_Type" Set IIsComputer = GetObject("IIS://" & TargetComputer& "/MimeMap") NewMimeMapping = IIsComputer.GetEx("MimeMap") i = UBound(NewMimeMapping) + 1 ReDim Preserve NewMimeMapping(i) Set NewMimeMapping(i) = CreateObject("MimeMap") NewMimeMapping(i).MimeType = MimeType NewMimeMapping(i).Extension = MimeExtension IIsComputer.PutEx ADS_PROPERTY_UPDATE, "MimeMap", NewMimeMapping IIsComputer.SetInfo
Removing a Server-Defined MIME Mapping Using a VBScript Active Server Page
Dim IIsComputer Dim TargetComputer Dim MimeMapping Dim MapToDelete Dim NewMimeMapping Dim i MapToDelete = "Extension_To_Delete_From_MIME_Map" TargetComputer = "Target_Server_Name" Set IIsComputer = GetObject("IIS://" & TargetComputer & "/MimeMap") NewMimeMapping = IIsComputer.MimeMap For Each MimeMapping In IIsComputer.MimeMap If MimeMapping.Extension <> MapToDelete Then ReDim Preserve NewMimeMapping(i) Set NewMimeMapping(i) = CreateObject("MimeMap") NewMimeMapping(i).MimeType = MimeMapping.MimeType NewMimeMapping(i).Extension = MimeMapping.Extension i = i + 1 End If Next Dim MimeItem For Each MimeItem In NewMimeMapping Response.Write MimeItem.MimeType & "<BR>" Next IIsComputer.PutEx ADS_PROPERTY_UPDATE, "MimeMap", NewMimeMapping 'IIsComputer.SetInfo
Querying Current Logging Status Using a VBScript Active Server Page
Dim Site Dim ServerName Dim SiteIndex ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Response.Write Site.LogType
Setting Logging Status Using a VBScript Active Server Page
Dim Site Dim ServerName Dim SiteIndex ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Site.LogType = 1 Site.SetInfo
Querying Active Log Format Using a VBScript Active Server Page
Dim Site Dim Log Dim ServerName Dim SiteIndex ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Set Log = GetObject("IIS://" & ServerName & "/logging") For Each Item In Log If Site.LogPluginCLSID = Item.LogModuleID Then Response.Write Item.Name & "<BR>" End If Next
Setting Active Log Format Using a VBScript Active Server Page
Dim Site Dim Log Dim ServerName Dim SiteIndex Dim NewLogFormatName ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value NewLogFormatName = "NCSA Common Log File Format" 'NewLogFormatName = "ODBC Logging" 'NewLogFormatName = "Microsoft IIS Log File Format" 'NewLogFormatName = "W3C Extended Log File Format" Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Set Log = GetObject("IIS://" & ServerName & "/logging") For Each Item In Log If Item.Name = NewLogFormatName Then Site.LogPluginCLSID = Item.LogModuleID Site.SetInfo End If Next
Querying New Log Time Period Using a VBScript Active Server Page
Dim Site Dim ServerName Dim SiteIndex ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Response.Write Site.LogFilePeriod
Setting New Log Time Period Using a VBScript Active Server Page
Dim Site Dim ServerName Dim SiteIndex Dim NewLogFilePeriod ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value NewLogFilePeriod = 1 Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Site.LogFilePeriod = NewLogFilePeriod Site.SetInfo
Querying Maximum Log File Size Using a VBScript Active Server Page
Dim Site Dim ServerName Dim SiteIndex ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Response.Write Site.LogFileTruncateSize
Setting Maximum Log File Size Using a VBScript Active Server Page
Dim Site Dim ServerName Dim SiteIndex Dim NewLogFileSize ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value NewLogFileSize = 1048576 Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Site.LogFileTruncateSize = NewLogFileSize Site.SetInfo
Querying Extended Logging Options Using a VBScript Active Server Page
Dim Site Dim ServerName Dim SiteIndex ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Response.Write "Log Date: " & Site.LogExtFileDate & "<BR>" Response.Write "Log Time: " & Site.LogExtFileTime & "<BR>" Response.Write "Log Client IP Address: " & Site.LogExtFileClientIp & "<BR>" Response.Write "Log User Name: " & Site.LogExtFileUserName & "<BR>" Response.Write "Log Service Name: " & Site.LogExtFileSiteName & "<BR>" Response.Write "Log Server Name: " & Site.LogExtFileComputerName & "<BR>" Response.Write "Log Server IP: " & Site.LogExtFileServerIp & "<BR>" Response.Write "Log Server Port: " & Site.LogExtFileServerPort & "<BR>" Response.Write "Log Method: " &Site.LogExtFileMethod & "<BR>" Response.Write "Log URI Stem: " & Site.LogExtFileUriStem & "<BR>" Response.Write "Log URI Query: " & Site.LogExtFileUriQuery & "<BR>" Response.Write "Log Http Status: " & Site.LogExtFileHttpStatus & "<BR>" Response.Write "Log Win32 Status: " & Site.LogExtFileWin32Status & "<BR>" Response.Write "Log Bytes Sent: " & Site.LogExtFileBytesSent & "<BR>" Response.Write "Log Bytes Received: " & Site.LogExtFileBytesRecv & "<BR>" Response.Write "Log Time Taken: " & Site.LogExtFileTimeTaken & "<BR>" Response.Write "Log Protocol Version: " & Site.LogExtFileProtocolVersion & "<BR>" Response.Write "Log User Agent: " & Site.LogExtFileUserAgent & "<BR>" Response.Write "Log Cookie: " & Site.LogExtFileCookie & "<BR>" Response.Write "Log Referrer: " & Site.LogExtFileReferer& "<BR>"
Setting Extended Logging Options Using a VBScript Active Server Page
Dim Site Dim ServerName Dim SiteIndex ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value Set Site = GetObject("IIS://" & ServerName &"/W3SVC/" & SiteIndex) Site.LogExtFileDate = True Site.LogExtFileTime = True Site.LogExtFileClientIp = True Site.LogExtFileUserName = True Site.LogExtFileSiteName = True Site.LogExtFileComputerName = True Site.LogExtFileServerIp = True Site.LogExtFileServerPort = True Site.LogExtFileMethod = True Site.LogExtFileUriStem = True Site.LogExtFileUriQuery = True Site.LogExtFileHttpStatus = True Site.LogExtFileWin32Status = True Site.LogExtFileBytesSent = True Site.LogExtFileBytesRecv = True Site.LogExtFileTimeTaken = True Site.LogExtFileProtocolVersion = True Site.LogExtFileUserAgent = True Site.LogExtFileCookie = True Site.LogExtFileReferer = True Site.SetInfo
Querying ODBC Logging Information Using a VBScript Active Server Page
Dim Site Dim ServerName Dim SiteIndex ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Response.Write Site.LogOdbcDataSource & "<BR>" Response.Write Site.LogOdbcPassword & "<BR>" Response.Write Site.LogOdbcTableName & "<BR>" Response.Write Site.LogOdbcUserName & "<BR>"
Setting ODBC Logging Information Using a VBScript Active Server Page
Dim Site Dim ServerName Dim SiteIndex Dim ODBC_DSN Dim DBPassword Dim TableName Dim UserName ODBC_DSN = "Name_of_Data_Source_to_Use_With_ODBC_Logging" DBPassword = "DB_Access_Credential" TableName = "Logging_Table_in_DB" UserName = "DB_Access_Credential" ServerName = "IIS_Server_Name" SiteIndex = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & SiteIndex) Site.LogOdbcDataSource = ODBC_DSN Site.LogOdbcPassword = DBPassword Site.LogOdbcTableName = TableName Site.LogOdbcUserName = UserName Site.SetInfo
Enumerating WWW Virtual Sites on an IIS Server Using a VBScript Active Server Page
Dim Parent Dim Child Dim ServerName ServerName = "IIS_Server_Name" Set Parent = GetObject("IIS://" &ServerName & "/W3SVC") For Each Child In Parent If IsNumeric(Child.Name) then Response.Write "ProgrammaticID: " & Child.Name & "Friendly Name: " & Child.ServerComment & "<BR>" End If Next
Enumerating FTP Virtual Sites on an IIS Server Using a VBScript Active Server Page
Dim Parent Dim Child Dim ServerName ServerName = "IIS_Server_Name" Set Parent = GetObject("IIS://" & ServerName &"/MSFTPSVC") For Each Child In Parent If IsNumeric(Child.Name) Then Response.Write Child.Name & " - " & Child.ServerComment & "<BR>" End If Next
Finding the Index Number for a Site Based on the ServerComment Property Using a VBScript Active Server Page
Dim Sites Dim Site Dim SearchTerm Dim Counter Dim IndexValue Dim ServerName SearchTerm = "Site_Description_String" ServerName = "IIS_Server_Name" Counter = 0 Set Sites = GetObject("IIS:\" & ServerName &"\w3svc") For Each Site In Sites If IsNumeric(Site.Name) Then If LCase(Site.ServerComment) = LCase(SearchTerm) Then Counter = Counter + 1 IndexValue = Site.Name End If End If Next Select Case Counter Case 0 Response.Write "The referenced site could not be found. Please enter a new search term." & "<BR>" Case 1 Response.Write "The index value for site '" & SearchTerm & "' is "& IndexValue & "<BR>" Case Is > 1 Response.Write "More than one site uses the value '" & SearchTerm & "' forthe &" _ ; "ServerComments property. Assure all values are unique before continuing." & "<BR>" End Select
Creating a New Web Site Using a VBScript Active Server Page
On Error Resume Next Dim Parent Dim Child Dim NewSite Dim NewRoot Dim ServerName Dim Index Dim SiteName Dim SitePath SiteName = "Friendly_Site_Name" ServerName = "IIS_Server_Name" SitePath = "Site_Path" Set Parent = GetObject("IIS://" & ServerName & "/W3SVC") For Each Child In Parent If IsNumeric(Child.Name) Then If Index < Child.Name Then Index = Child.Name End If End If Next Index = Index + 1 Set NewSite = Parent.Create("IIsWebServer", Index) NewSite.ServerComment = SiteName NewSite.SetInfo Set NewRoot = NewSite.Create("IIsWebVirtualDir", "Root") NewRoot.Path = SitePath NewRoot.SetInfo
Creating a New FTP Site Using a VBScript Active Server Page
Dim Parent Dim Child Dim NewSite Dim NewRoot Dim ServerName Dim Index Dim SiteName Dim SitePath Dim SiteIPAddress Dim SiteTCPPort SiteName = "Friendly_Site_Name" ServerName = "IIS_Server_Name" SitePath = "Site_Path" SiteIPAddress = "" SiteTCPPort = "21" 'SiteIPAddress = "xxx.xxx.xxx.xxx" 'SiteTCPPort = "TCP_Port_for_Server" Set Parent = GetObject("IIS://" & ServerName &"/MSFTPSVC") For Each Child In Parent If IsNumeric(Child.Name) Then If Index < Child.Name Then Index = Child.Name End If End If Next Index = Index + 1 Set NewSite = Parent.Create("IIsFTPServer", Index) NewSite.ServerComment = SiteName NewSite.ServerBindings = Array(SiteIPAddress & ":" & SiteTCPPort & ":") NewSite.SetInfo Set NewRoot = NewSite.Create("IIsFTPVirtualDir", "Root") NewRoot.Path = SitePath NewRoot.SetInfo
Deleting a Site Using the ServerComment Property Using a VBScript Active Server Page
Dim Sites Dim Site Dim SearchTerm Dim Counter Dim IndexValue Dim ServerName SearchTerm = "Site_Description_String" ServerName = "IIS_Server_Name" Counter = 0 Set Sites = GetObject("IIS:\" & ServerName & "\w3svc") For Each Site In Sites If IsNumeric(Site.Name) Then If LCase(Site.ServerComment) = LCase(SearchTerm) Then Counter = Counter + 1 IndexValue = Site.Name End If End If Next Select Case Counter Case 0 Response.Write "The referenced site could not be found. Please enter a new search term." Case 1 Call Sites.Delete("IIsWebServer", IndexValue) Set Sites = Nothing Response.Write "Site '" & SearchTerm & "' was deleted successfully." Case Is > 1 Response.Write "More than one site uses the value '" & SearchTerm & "' for the " _ ; "ServerComments property. Assure all values are unique before continuing." End Select
Deleting an FTP Site Using a VBScript Active Server Page
Dim Sites Dim Site Dim SearchTerm Dim Counter Dim IndexValue Dim ServerName SearchTerm = "Site_Description_String" ServerName = "IIS_Server_Name" Counter = 0 Set Sites = GetObject("IIS:\" &ServerName & "\MSFTPSVC") For Each Site In Sites If IsNumeric(Site.Name) Then If LCase(Site.ServerComment) = LCase(SearchTerm) Then Counter = Counter + 1 IndexValue = Site.Name End If End If Next Select Case Counter Case 0 Response.Write "The referenced site could not be found. Please enter a new search term." Case 1 Call Sites.Delete("IIsFTPServer", IndexValue) Set Sites = Nothing Response.Write "Site '" & SearchTerm & "' was deleted successfully." Case Is > 1 Response.Write "More than one site uses the value '" & SearchTerm & "' for the " & ServerComments &_ " property. Assure all values are unique before continuing." End Select
Creating a New Virtual Directory Using a VBScript Active Server Page
Dim Parent Dim NewVDir Dim ServerName Dim VDirPath Dim VDirName Dim Index ServerName = "IIS_Server_Name" VDirPath = "Path_for_New_Virtual_Directory" VDirName = "Name_For_Virtual_Directory" Index = Site_Index_Integer Set Parent = GetObject("IIS://" & ServerName & "/W3SVC/" & Index & "/ROOT") Set NewVDir = Parent.Create("IIsWebVirtualDir", VDirName) NewVDir.SetInfo NewVDir.Path = VDirPath NewVDir.SetInfo
Creating a New FTP Virtual Directory Using a VBScript Active Server Page
Dim Parent Dim NewVDir Dim ServerName Dim VDirPath Dim VDirName Dim Index ServerName = "IIS_Server_Name" VDirPath = "Path_for_New_Virtual_Directory" VDirName = "Name_For_Virtual_Directory" Index = Site_Index_Integer Set Parent = GetObject("IIS://" & ServerName & "/MSFTPSVC/" & Index & "/ROOT") Set NewVDir = Parent.Create("IIsFTPVirtualDir", VDirName) NewVDir.SetInfo NewVDir.Path = VDirPath NewVDir.SetInfo
Removing an Existing Virtual Directory Using a VBScript Active Server Page
Dim Parent Dim ServerName Dim VDirName Dim Index ServerName = "IIS_Server_Name" VDirName = "Name_For_Virtual_Directory Index = Site_Index_Value Set Parent = GetObject("IIS://" & ServerName & "/W3SVC/" & Index & "/ROOT") Call Parent.Delete("IIsWebVirtualDir", VDirName) Set Parent = Nothing
Removing an Existing FTP Virtual Directory Using a VBScript Active Server Page
Dim Parent Dim ServerName Dim VDirName Dim Index ServerName = "IIS_Server_Name" VDirName = "Name_For_Virtual_Directory Index = Site_Index_Value Set Parent = GetObject("IIS://" & ServerName & "/MSFTPSVC/" & Index & "/ROOT") Call Parent.Delete("IIsFTPVirtualDir", VDirName)
Managing Web Directories and Files Using a VBScript Active Server Page
Dim VirtualDirectory Dim ServerName Dim Index ServerName = "IIS_Server_Name" Index = Site_Index_Value Set VirtualDirectory = GetObject("IIS://" & ServerName & "/W3SVC/" & Index & "/ROOT") For Each Item In VirtualDirectory Response.Write Item.Name& "<BR>" Next
Adding an Entry in the Metabase for a File System Directory Using a VBScript Active Server Page
Dim VirtualDirectory Dim WebDir Dim ServerName Dim FileName Dim Index Dim VirtualDirectoryName ServerName = "IIS_Server_Name" DirName = "Directory_Name_To_Enter" Index = Site_Index Set VirtualDirectory = GetObject("IIS://" & ServerName & "/W3SVC/" & Index & "/ROOT") Set WebDir = VirtualDirectory.Create("IIsWebDirectory", DirName) WebDir.SetInfo
Adding an Entry in the Metabase for a File in the File System Using a VBScript Active Server Page
Dim VirtualDirectory Dim WebFile Dim ServerName Dim FileName Dim Index Dim VirtualDirectoryName ServerName = "IIS_Server_Name" FileName = "File_Name_To_Enter" Index = Site_Index Set VirtualDirectory = GetObject("IIS://" & ServerName & "/W3SVC/" & Index & "/ROOT") Set WebFile = VirtualDirectory.Create("IIsWebFile", FileName) WebFile.SetInfo
Querying Site Status Using a VBScript Active Server Page
Dim Site Dim ServerName Dim Index ServerName = "IIS_Server_Name" Index = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & Index) Select Case Site.ServerState Case 1 Response.Write "Starting" Case 2 Response.Write "Started" Case 3 Response.Write "Stopping" Case 4 Response.Write "Stopped" Case 5 Response.Write "Pausing" Case 6 Response.Write "Paused" Case 7 Response.Write "Continuing" End Select
Starting a Site Using a VBScript Active Server Page
Dim Site Dim ServerName Dim Index ServerName = "IIS_Server_Name" Index = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & Index) If Site.ServerState = 4 or Site.ServerState = 3 Then Site.Start Response.Write "Request to start site "&Site.ServerComment&" was issued." End If
Stopping a Site Using a VBScript Active Server Page
Dim Site Dim ServerName Dim Index ServerName = "IIS_Server_Name" Index = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & Index) If Site.ServerState = 2 or Site.ServerState = 1 Then Site.Stop Response.Write "Request to stop site " & Site.ServerComment & " was issued." End If
Pausing a Site Using a VBScript Active Server Page
Dim Site Dim ServerName Dim Index ServerName = "IIS_Server_Name" Index = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & Index) If Site.ServerState = 1 or Site.ServerState = 2 Then Site.Pause Response.Write "Request to pause site " & Site.ServerComment & " was issued." End If
Continuing a Paused Site Using a VBScript Active Server Page
Dim Site Dim ServerName Dim Index ServerName = "IIS_Server_Name" Index = Site_Index_Value Set Site = GetObject("IIS://" & ServerName & "/W3SVC/" & Index) If Site.ServerState = 6 or Site.ServerState = 5 Then Site.Continue Response.Write "Request to continue paused site " & Site.ServerComment &" was issued." End If