Login :: Register
General Topics
HostedbyMaximumASP170x35
2005 IIS MVPs
Other IIS Sites
  Do do I add or remove a Script Mapping? 

Do do I add or remove a Script Mapping?

The script below will allow you to add or remove a script mapping for an IIS 5 Computer.

usage: cscript ScriptMap.vbs                                                    
                [--computer|-c COMPUTER1]                                       
                [--SiteNumber|-n SITENUMBER]                                    
                [--Add|-a ScriptMapFull]                                        
                [--Delete|-d ScriptMapExt]                                      
                [--ReplaceFromfile|-r ScriptMapFile]                            
                [--help|-?]                                                     
                                                                                
The ScriptMapFull must resolve to a full script map entry such as:              
.htw,C:\WINNT\System32\webhits.dll,3,GET,HEAD,POST                              
                                                                                
If there are any spaces in the path wrap the whole script map in            
.htw,C:\WINNT\System32\webhits.dll,3,GET,HEAD,POST                          
                                                                                
Format of a Script map = Extension,Path,Flags,Exclusions                        
                                                                                
Flags:                                                                          
1 = The script is allowed to run in directories given Script permission. If     
    this value is not set, then the script can only be executed in directories  
    that are flagged for Execute permission.                                    
4 = The server attempts to access the PATH_INFO portion of the URL, as a file,  
    before starting the scripting engine. If the file can't be opened, or       
    doesn't exist, an error is returned to the client.                          
5 = Both of the above conditions are TRUE.                                      
                                                                                
Exclusions are one or more of OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE            
                                                                                
The ScriptMapExt must resolve to the extension of the script map such as:       
.htr                                                                            
                                                                                
Example: ScriptMaps.vbs -a .chris,c:\test\test.dll,1,GET,HEAD,POS  

option Explicit
' 28 September 2000
' Updated : April 25, 2002
'    Fixed a bug in the script mappings being read/written from the / and not the /ROOT node in the metabase
'    Fixed a bug in the command line parameters not being lowercase and not working : Add, Delete

' www.iisfaq.com - lots more scripts there

Const DebugMode = false
Const COMMAND_VIEW       = 0
Const COMMAND_ADD    = 1
Const COMMAND_DELETE      = 2
Const COMMAND_ReplaceFromfile     = 3
Dim ArgScriptMap,ArgScriptMapFileSpec, FSO, IISOBJ, ArgComputer,ArgSiteNumber, ObjectPath, ArgCommand, Docs
Sub DisplayUsage
    WScript.Echo
WScript.Echo "usage: cscript ScriptMap.vbs"
WScript.Echo "                [--computer|-c COMPUTER1]"
WScript.Echo "                [--SiteNumber|-n SITENUMBER]"
WScript.Echo "                [--Add|-a ScriptMapFull]"
WScript.Echo "                [--Delete|-d ScriptMapExt]"
WScript.Echo "                [--ReplaceFromfile|-r ScriptMapFile]"
WScript.Echo "                [--help|-?]"
WScript.Echo ""
WScript.Echo "The ScriptMapFull must resolve to a full script map entry such as:"
WScript.Echo ".htw,C:\WINNT\System32\webhits.dll,3,GET,HEAD,POST"
WScript.Echo ""
WScript.Echo "If there are any spaces in the path wrap the whole script map in """""
WScript.Echo """.htw,C:\WINNT\System32\webhits.dll,3,GET,HEAD,POST"""
WScript.Echo ""
WScript.Echo "Format of a Script map = Extension,Path,Flags,Exclusions"
WScript.Echo ""
WScript.Echo "Flags:"
WScript.Echo "1 = The script is allowed to run in directories given Script permission. If "
WScript.Echo "    this value is not set, then the script can only be executed in directories "
WScript.Echo "    that are flagged for Execute permission."
WScript.Echo "4 = The server attempts to access the PATH_INFO portion of the URL, as a file, "
WScript.Echo "    before starting the scripting engine. If the file can't be opened, or "
WScript.Echo "    doesn't exist, an error is returned to the client. "
WScript.Echo "5 = Both of the above conditions are TRUE."
WScript.Echo ""
WScript.Echo "Exclusions are one or more of OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE"
WScript.Echo ""
WScript.Echo "The ScriptMapExt must resolve to the extension of the script map such as:"
WScript.Echo ".htr"
WScript.Echo ""
WScript.Echo "Example 1: ScriptMaps.vbs -a "".chris,c:\test\test.dll,1,GET,HEAD,POS"""
WScript.Echo ""
WScript.Quit(1)
End Sub
Sub checkCmdLine()
Dim OArgs, ArgNum
Set oArgs = WScript.Arguments
ArgNum = 0
While ArgNum < oArgs.Count
Select Case LCase(oArgs(ArgNum))
  Case "--computer","-c":
   ArgNum = ArgNum + 1
   if (ArgNum = oArgs.count) then
    Call DisplayUsage()
   end if
   ArgComputer = oArgs(ArgNum)
  Case "--sitenumber","-n":
   ArgNum = ArgNum + 1
   if (ArgNum = oArgs.count) then
    Call DisplayUsage()
   end if
   ArgSiteNumber = oArgs(ArgNum)
  Case "--add","-a":
   ArgCommand = COMMAND_ADD
   ArgNum = ArgNum + 1
   if (ArgNum = oArgs.count) then
    Call DisplayUsage()
   end if
   ArgScriptMap = oArgs(ArgNum)
  Case "--delete","-d":
   ArgCommand = COMMAND_DELETE
   ArgNum = ArgNum + 1
   if (ArgNum = oArgs.count) then
    Call DisplayUsage()
   end if
   ArgScriptMap = oArgs(ArgNum)
  Case "--replacefromfile","-r":
   ArgCommand = COMMAND_ReplaceFromfile
   ArgNum = ArgNum + 1
   if (ArgNum = oArgs.count) then
    WScript.Echo "Please specify the ScriptMapFileSpec!" & vbcrlf
    Call DisplayUsage()
   end if
   ArgScriptMapFileSpec = oArgs(ArgNum)
  Case "--help","-?":
   Call DisplayUsage
  Case Else:
   WScript.Echo "Unknown argument "& oArgs(ArgNum)
   Call DisplayUsage
End Select
ArgNum = ArgNum + 1
Wend
if (ArgSiteNumber = "") then
WScript.Echo "Site number not given defaulting to W3SVC/1 which may or may not exist!"
WScript.Echo
end if
if (Argcommand = COMMAND_ReplaceFromfile) then
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
if (FSO.FileExists(ArgScriptMapfileSpec) = false) then
  WScript.Echo "The specified SCRIPT MAP FILESPEC does not exist: " & ArgScriptMapfileSpec
  WScript.Quit(3)
end if
Set FSO = Nothing
end if
end sub
Sub SplitDefaultDocuments
end Sub
Function VerifyScriptMapToAdd()
    Dim ArgExt, ArgPath, ArgOption, ArgExclusions, Bits, pos
' We need to split the line up into its components and check each one.
Bits = split(ArgScriptMap, ",")
if (UBound(Bits) > 1) then
  ' The first entry should be the extension, the extension includes a "." at the start
  if (left(Bits(0),1) <> ".") then
   WScript.Echo "* Invalid extension - extension must start with a ."
   WScript.echo
   Call DisplayUsage
  end if
  ArgExt = bits(0)
  VerifyScriptMapToAdd = ArgExt
  ArgPath = bits(1)
  ArgOption = Bits(2)
  for pos = 3 to UBound(Bits)
      if (Pos > 3) then
      ArgExclusions = ArgExclusions & ","
   end if
   ArgExclusions = ArgExclusions & Bits(pos)
  next
     'WScript.Echo "Ext = " & ArgExt
     'WScript.Echo "Path = " & ArgPath
     'WScript.Echo "Option = " & ArgOption
     'WScript.Echo "Exclusion = " & ArgExclusions
else
  WScript.Echo "* Invalid Script Map to add, insufficiant parts to the script map!"
  WScript.echo
  Call DisplayUsage
  VerifyScriptMapToAdd = ""
end if
end Function

Function DoesScriptMapExist(Extension)
    Dim Pos, Items, Ext, ScriptMaps
DoesScriptMapExist = false
Ext = ucase(Extension)
ScriptMaps = IISOBJ.Scriptmaps
for pos = lbound(ScriptMaps) to UBOund(ScriptMaps)
  Items = Split(Scriptmaps(Pos), ",")
  'WScript.Echo "EXT="&Ext & " -> " & Items(0)
        if (ucase(items(0)) = Ext) then
   DoesScriptMapExist = true
   exit Function
  end if
next
end function
Sub DoView
Dim Pos, ScriptMaps
WScript.Echo "Script Maps on " & ObjectPath & vbcrlf
        ScriptMaps = IISOBJ.ScriptMaps
for pos = lbound(ScriptMaps) to UBOund(ScriptMaps)
          WScript.Echo  Pos & " = " & ScriptMaps(POS)
next
end Sub
Sub DoAdd
Dim Pos, LocalDocs, currentDocs, DocumentAdded, Ext, NewScriptMaps
Ext = VerifyScriptMapToAdd
    if (DoesScriptMapExist(Ext) = false) then
else
  WScript.Echo "Extension """ & Ext & """ already exists, ScriptMap not added!"
  WScript.Quit(2)
end if
NewScriptMaps = IISOBJ.ScriptMaps
Redim preserve NewScriptMaps(Ubound(NewScriptMaps)+1)
NewScriptMaps(ubound(NewScriptMaps)) = ArgScriptMap
WScript.Echo "New Script Map added for extension " & Ext & vbcrlf  & vbcrlf & ArgScriptMap ' Save the added script map
IISOBJ.ScriptMaps = NewScriptMaps
IISOBJ.SetInfo
if (ERR <> 0) then
              WScript.Echo "TERMINATING -- Error setting data - " & Err.Description & " (" & Err & ")"
  WScript.Quit(1)
  exit sub
end if
    IISOBJ.GetInfo
    WScript.Echo
    DoView()
end Sub
Function DoDeleteItem(Ext)
    Dim Extension, xpos, Pos, Items, ScriptMaps, NewScriptMaps
Extension = ucase(Ext)
ScriptMaps = IISOBJ.Scriptmaps
xpos =0
for pos = lbound(ScriptMaps) to UBOund(ScriptMaps)
  Items = Split(Scriptmaps(Pos), ",")
                if (ucase(items(0)) <> Extension) then
   if (xpos = 0) then
    reDim NewScriptMaps(0)
   else
                            Redim Preserve NewScriptMaps(UBound(NewScriptMaps)+1)
   end if
                        NewScriptMaps(UBound(NewScriptMaps)) = ScriptMaps(Pos)
   xpos = xpos + 1
  end if
next
if (DebugMode) then
  for pos = lbound(NewScriptMaps) to UBOund(NewScriptMaps)
   WScript.Echo  Pos & " = " & NewScriptMaps(Pos)
  next
end if
DoDeleteItem = NewScriptMaps
end Function
Sub doDelete
Dim xpos, Pos, Found, LocalDocs, newDocs, newScriptMap
        if (DoesScriptMapExist(ArgScriptMap) = false) then
  WScript.Echo "Extension """ & ArgScriptMap & """ does not exist!" & vbcrlf 
  Call DoView()
  WScript.Quit(2)
  exit Sub
end if
        newScriptMap  =DoDeleteItem(ArgScriptMap)
        if (DebugMode) then
         for pos = lbound(NewScriptMap) to UBOund(NewScriptMap)
   WScript.Echo Pos & " = " & NewScriptMap(Pos)
  next
end if
    IISOBJ.ScriptMaps = NewScriptMap
IISOBJ.SetInfo
if (ERR <> 0) then
  WScript.Echo "TERMINATING -- Error setting data - " & Err.Description & " (" & Err & ")"
  WScript.Quit(1)
end if
' Refresh the data
        IISOBJ.GetInfo
        WScript.Echo
        DoView()
end Sub
Sub doReplaceFromfile()
    Dim Pos, NewScriptMaps, Textfile
pos = 0
Set FSO =WScript.CreateObject("Scripting.FileSystemObject")
Set Textfile = FSO.OpenTextFile(ArgScriptMapfileSpec)
While Textfile.AtEndOfStream = false
  if (pos = 0) then
   Redim NewScriptMaps(0)
  else
   Redim Preserve NewScriptMaps(UBound(NewScriptMaps)+1)
  end if
        NewScriptMaps(UBound(NewScriptMaps)) = Textfile.ReadLine
        pos= pos + 1
wend
' for pos = lbound(NewScriptMaps) to UBOund(NewScriptMaps)
'  WScript.Echo Pos & " " & NewScriptMaps(Pos)
' next

    IISOBJ.ScriptMaps = NewScriptMaps
IISOBJ.SetInfo
if (ERR <> 0) then
  WScript.Echo "TERMINATING -- Error setting data - " & Err.Description & " (" & Err & ")"
  WScript.Quit(1)
end if
WScript.Echo "ScriptMaps have been replaced from the file " & ArgScriptMapFilespec
' Refresh the data
        IISOBJ.GetInfo
        WScript.Echo
        DoView()
End Sub
ArgComputer   = "localHost"
ArgSiteNumber = "1"
ArgCommand    = COMMAND_VIEW
CheckCmdLine()
'on error resume next
ObjectPath = "IIS://" & ArgComputer & "/W3SVC/" & ArgSiteNumber & "/ROOT"
SET IISOBJ = getObject(ObjectPath)
If (Err <> 0) Then
WScript.Echo "TERMINATING --- Error accessing " & ObjectPath & " " & Err.Description & " (" & Err & ")"
wscript.quit(2)
End If
Docs = Split(IISOBJ.DefaultDoc, ",")
Select case argCommand
   case COMMAND_VIEW : doView
   case COMMAND_ADD : doAdd
   case COMMAND_DELETE : doDelete
   case COMMAND_ReplaceFromfile : doReplaceFromfile
end select

 This site and its contents are Copyright 1999-2004 by IISFAQ Microsoft Corporation in no way endorses or is affiliated with IISFAQ