home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / mkwebdir.vbs < prev    next >
Text File  |  1997-10-25  |  7KB  |  231 lines

  1. '---------------------------------------------------------------------------------------------------
  2. ' This function creates a virtual web directory on the specified web site
  3. ' and with the specified path
  4. '
  5. 'mkwebdir [--computer|-c COMPUTER1, COMPUTER2, COMPUTER3]
  6. '         <--website|-w WEBSITE>
  7. '         <--virtualdir|-v NAME1,PATH1,NAME2,PATH2,...>
  8. '         [--help|-?]
  9. '
  10. 'COMPUTER                Computer on which users exists
  11. 'WEBSITE1,WEBSITE2       Virtual Web Sites on which directories will be created
  12. 'NAME1,PATH1,NAME2,PATH2 Virtual Directories names and paths to create
  13. '
  14. 'Example 1        mkwebdir -c MyComputer -w "Default Web Site"
  15. '                       -v "Virtual Dir1","c:\inetpub\wwwroot\dir1","Virtual Dir2","c:\inetpub\wwwroot\dir2"
  16. '
  17. '---------------------------------------------------------------------------------------------------
  18.  
  19.  
  20. ' Force explicit declaration of all variables.
  21. Option Explicit
  22.  
  23. On Error Resume Next
  24.  
  25. Dim oArgs, ArgNum
  26.  
  27. Dim ArgComputer, ArgWebSites, ArgVirtualDirs, ArgDirNames(), ArgDirPaths(), DirIndex
  28. Dim ArgComputers
  29.  
  30. Set oArgs = WScript.Arguments
  31. ArgComputers = Array("LocalHost")
  32.  
  33. ArgNum = 0
  34. While ArgNum < oArgs.Count
  35.  
  36.     If (ArgNum + 1) >= oArgs.Count Then
  37.         Call DisplayUsage
  38.     End If    
  39.  
  40.     Select Case LCase(oArgs(ArgNum))
  41.         Case "--computer","-c":
  42.             ArgNum = ArgNum + 1
  43.             ArgComputers = Split(oArgs(ArgNum), ",", -1)
  44.         Case "--website","-w":
  45.             ArgNum = ArgNum + 1
  46.             ArgWebSites = oArgs(ArgNum)
  47.         Case "--virtualdir","-v":
  48.             ArgNum = ArgNum + 1
  49.             ArgVirtualDirs = Split(oArgs(ArgNum), ",", -1)
  50.         Case "--help","-?"
  51.             Call DisplayUsage
  52.     End Select    
  53.  
  54.     ArgNum = ArgNum + 1
  55. Wend
  56.  
  57. ArgNum = 0
  58. DirIndex = 0
  59.  
  60. ReDim ArgDirNames((UBound(ArgVirtualDirs)+1) \ 2)
  61. ReDim ArgDirPaths((UBound(ArgVirtualDirs)+1) \ 2)
  62.  
  63. if isArray(ArgVirtualDirs) then
  64.     While ArgNum <= UBound(ArgVirtualDirs)
  65.         ArgDirNames(DirIndex) = ArgVirtualDirs(ArgNum)
  66.         If (ArgNum + 1) > UBound(ArgVirtualDirs) Then
  67.             WScript.Echo "Error understanding virtual directories"
  68.             Call DisplayUsage
  69.         End If    
  70.         ArgNum = ArgNum + 1
  71.         ArgDirPaths(DirIndex) = ArgVirtualDirs(ArgNum)
  72.         ArgNum = ArgNum + 1
  73.         DirIndex = DirIndex + 1
  74.     Wend
  75. end if 
  76.  
  77. If (ArgWebSites = "") Or (IsArray(ArgDirNames) = False or IsArray(ArgDirPaths) = False) Then
  78.     Call DisplayUsage
  79. Else
  80.     Dim compIndex
  81.     for compIndex = 0 to UBound(ArgComputers)
  82.         Call ASTCreateVirtualWebDir(ArgComputers(compIndex),ArgWebSites,ArgDirNames,ArgDirPaths)
  83.     next
  84. End If
  85.  
  86. '---------------------------------------------------------------------------------
  87. Sub Display(Msg)
  88.     WScript.Echo Now & ". Error Code: " & Hex(Err) & " - " & Msg
  89. End Sub
  90.  
  91. Sub Trace(Msg)
  92.     WScript.Echo Now & " : " & Msg    
  93. End Sub
  94.  
  95. Sub DisplayUsage()
  96.     WScript.Echo "Usage: mkwebdir [--computer|-c COMPUTER1,COMPUTER2]"
  97.     WScript.Echo "                <--website|-w WEBSITE1>"
  98.     WScript.Echo "                <--virtualdir|-v NAME1,PATH1,NAME2,PATH2,...>"
  99.     WScript.Echo "                [--help|-?]"    
  100.  
  101.     WScript.Echo ""
  102.     WScript.Echo "Note, WEBSITE is the Web Site on which the directory will be created."
  103.     WScript.Echo "The name can be specified as one of the following, in the priority specified:"
  104.     WScript.Echo " Server Number (i.e. 1, 2, 10, etc.)"
  105.     WScript.Echo " Server Description (i.e ""My Server"")"
  106.     WScript.Echo " Server Host name (i.e. ""www.domain.com"")"
  107.     WScript.Echo " IP Address (i.e., 127.0.0.1)"
  108.     WScript.Echo ""
  109.     WScript.Echo ""
  110.     WScript.Echo "Example : mkwebdir -c MyComputer -w ""Default Web Site"""
  111.     WScript.Echo "           -v ""dir1"",""c:\inetpub\wwwroot\dir1"",""dir2"",""c:\inetpub\wwwroot\dir2"""
  112.  
  113.     WScript.Quit
  114. End Sub
  115. '---------------------------------------------------------------------------------
  116.  
  117.  
  118. Sub ASTCreateVirtualWebDir(ComputerName,WebSiteName,DirNames,DirPaths)
  119.     Dim Computer, webSite, WebSiteID, vRoot, vDir, DirNum
  120.     On Error Resume Next
  121.     
  122.     set webSite = findWeb(ComputerName, WebSiteName)
  123.     if IsObject(webSite) then
  124.         set vRoot = webSite.GetObject("IIsWebVirtualDir", "Root")
  125.         Trace "Accessing root for " & webSite.ADsPath
  126.         If (Err <> 0) Then
  127.             Display "Unable to access root for " & webSite.ADsPath
  128.         Else
  129.             DirNum = 0
  130.             If (IsArray(DirNames) = True) And (IsArray(DirPaths) = True) And (UBound(DirNames) = UBound(DirPaths)) Then
  131.                 While DirNum < UBound(DirNames)
  132.                     'Create the new virtual directory
  133.                     Set vDir = vRoot.Create("IIsWebVirtualDir",DirNames(DirNum))
  134.                     If (Err <> 0) Then
  135.                         Display "Unable to create " & vRoot.ADsPath & "/" & DirNames(DirNum) &"."
  136.                     Else
  137.                         'Set the new virtual directory path
  138.                         vDir.AccessRead = true
  139.                         vDir.Path = DirPaths(DirNum)
  140.                         If (Err <> 0) Then
  141.                             Display "Unable to bind path " & DirPaths(DirNum) & " to " & vRootName & "/" & DirNames(DirNum) & ". Path may be invalid."
  142.                         Else
  143.                             'Save the changes
  144.                             vDir.SetInfo
  145.                             If (Err <> 0) Then
  146.                                 Display "Unable to save configuration for " & vRootName & "/" & DirNames(DirNum) &"."
  147.                             Else
  148.                                 Trace "Web virtual directory " & vRootName & "/" & DirNames(DirNum) & " created successfully."
  149.                             End If
  150.                         End If
  151.                     End If
  152.                     Err = 0
  153.                     DirNum = DirNum + 1
  154.                 Wend
  155.             End If
  156.         End If
  157.     else
  158.         Display "Unable to find "& WebSiteName &" on "& ComputerName
  159.     End if
  160.     Trace "Done."
  161. End Sub
  162.  
  163. function getBinding(bindstr)
  164.  
  165.     Dim one, two, ia, ip, hn
  166.     
  167.     one=Instr(bindstr,":")
  168.     two=Instr((one+1),bindstr,":")
  169.     
  170.     ia=Mid(bindstr,1,(one-1))
  171.     ip=Mid(bindstr,(one+1),((two-one)-1))
  172.     hn=Mid(bindstr,(two+1))
  173.     
  174.     getBinding=Array(ia,ip,hn)
  175. end function
  176.  
  177. Function findWeb(computer, webname)
  178.     On Error Resume Next
  179.  
  180.     Dim websvc, site
  181.     dim webinfo
  182.     Dim aBinding, binding
  183.  
  184.     set websvc = GetObject("IIS://"&computer&"/W3svc")
  185.     if (Err <> 0) then
  186.         exit function
  187.     end if
  188.     ' First try to open the webname.
  189.     set site = websvc.GetObject("IIsWebServer", webname)
  190.     if (Err = 0) and (not isNull(site)) then
  191.         if (site.class = "IIsWebServer") then
  192.             ' Here we found a site that is a web server.
  193.             set findWeb = site
  194.             exit function
  195.         end if
  196.     end if
  197.     err.clear
  198.     for each site in websvc
  199.         if site.class = "IIsWebServer" then
  200.             '
  201.             ' First, check to see if the ServerComment
  202.             ' matches
  203.             '
  204.             If site.ServerComment = webname Then
  205.                 set findWeb = site
  206.                 exit function
  207.             End If
  208.             aBinding=site.ServerBindings
  209.             if (IsArray(aBinding)) then
  210.                 if aBinding(0) = "" then
  211.                     binding = Null
  212.                 else
  213.                     binding = getBinding(aBinding(0))
  214.                 end if
  215.             else 
  216.                 if aBinding = "" then
  217.                     binding = Null
  218.                 else
  219.                     binding = getBinding(aBinding)
  220.                 end if
  221.             end if
  222.             if IsArray(binding) then
  223.                 if (binding(2) = webname) or (binding(0) = webname) then
  224.                     set findWeb = site
  225.                     exit function
  226.                 End If
  227.             end if 
  228.         end if
  229.     next
  230. End Function
  231.