Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
6.8 KiB

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