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.

259 lines
7.9 KiB

  1. '------------------------------------------------------------------------------------------------
  2. '
  3. ' This is a simple script to create a new virtual web server.
  4. '
  5. ' Usage: mkw3site <--RootDirectory|-r ROOT DIRECTORY>
  6. ' <--Comment|-t SERVER COMMENT>
  7. ' [--computer|-c COMPUTER1[,COMPUTER2...]]
  8. ' [--HostName|-h HOST NAME]
  9. ' [--port|-o PORT NUM]
  10. ' [--IPAddress|-i IP ADDRESS]
  11. ' [--SiteNumber|-n SITENUMBER]
  12. ' [--DontStart]
  13. ' [--verbose|-v]
  14. ' [--help|-?]
  15. '
  16. ' IP ADDRESS The IP Address to assign to the new server. Optional.
  17. ' HOST NAME The host name of the web site for host headers.
  18. ' WARNING: Only use Host Name if DNS is set up find the server.
  19. ' PORT NUM The port to which the server should bind
  20. ' ROOT DIRECTORY Full path to the root directory for the new server.
  21. ' SERVER COMMENT The server comment -- this is the name that appers in the MMC.
  22. ' SITENUMBER The Site Number is the number in the path that the web server
  23. ' will be created at. i.e. w3svc/3
  24. '
  25. ' Example 1: mkw3site -r D:\Roots\Company11 --DontStart -t "My Company Site"
  26. ' Example 2: mkw3site -r C:\Inetpub\wwwroot -t Test -o 8080
  27. '------------------------------------------------------------------------------------------------
  28. ' Force explicit declaration of all variables
  29. Option Explicit
  30. On Error Resume Next
  31. Dim ArgIPAddress, ArgRootDirectory, ArgServerComment, ArgSkeletalDir, ArgHostName, ArgPort
  32. Dim ArgComputers, ArgStart
  33. Dim ArgSiteNumber
  34. Dim oArgs, ArgNum
  35. Dim verbose
  36. ArgIPAddress = ""
  37. ArgHostName = ""
  38. ArgPort = 80
  39. ArgStart = True
  40. ArgComputers = Array(1)
  41. ArgComputers(0) = "LocalHost"
  42. ArgSiteNumber = 0
  43. verbose = false
  44. Set oArgs = WScript.Arguments
  45. ArgNum = 0
  46. While ArgNum < oArgs.Count
  47. Select Case LCase(oArgs(ArgNum))
  48. Case "--port","-o":
  49. ArgNum = ArgNum + 1
  50. ArgPort = oArgs(ArgNum)
  51. Case "--ipaddress","-i":
  52. ArgNum = ArgNum + 1
  53. ArgIPAddress = oArgs(ArgNum)
  54. Case "--rootdirectory","-r":
  55. ArgNum = ArgNum + 1
  56. ArgRootDirectory = oArgs(ArgNum)
  57. Case "--comment","-t":
  58. ArgNum = ArgNum + 1
  59. ArgServerComment = oArgs(ArgNum)
  60. Case "--hostname","-h":
  61. ArgNum = ArgNum + 1
  62. ArgHostName = oArgs(ArgNum)
  63. Case "--computer","-c":
  64. ArgNum = ArgNum + 1
  65. ArgComputers = Split(oArgs(ArgNum), ",", -1)
  66. Case "--sitenumber","-n":
  67. ArgNum = ArgNum + 1
  68. ArgSiteNumber = CLng(oArgs(ArgNum))
  69. Case "--dontstart":
  70. ArgStart = False
  71. Case "--help","-?":
  72. Call DisplayUsage
  73. Case "--verbose", "-v":
  74. verbose = true
  75. Case Else:
  76. WScript.Echo "Unknown argument "& oArgs(ArgNum)
  77. Call DisplayUsage
  78. End Select
  79. ArgNum = ArgNum + 1
  80. Wend
  81. If (ArgRootDirectory = "") Or (ArgServerComment = "") Then
  82. if (ArgRootDirectory = "") then
  83. WScript.Echo "Missing Root Directory"
  84. else
  85. WScript.Echo "Missing Server Comment"
  86. end if
  87. Call DisplayUsage
  88. WScript.Quit(1)
  89. End If
  90. Call ASTCreateWebSite(ArgIPAddress, ArgRootDirectory, ArgServerComment, ArgHostName, ArgPort, ArgComputers, ArgStart)
  91. Sub ASTCreateWebSite(IPAddress, RootDirectory, ServerComment, HostName, PortNum, Computers, Start)
  92. Dim w3svc, WebServer, NewWebServer, NewDir, Bindings, BindingString, NewBindings, ComputerIndex, Index, SiteObj, bDone
  93. Dim comp
  94. On Error Resume Next
  95. For ComputerIndex = 0 To UBound(Computers)
  96. comp = Computers(ComputerIndex)
  97. If ComputerIndex <> UBound(Computers) Then
  98. Trace "Creating web site on " & comp & "."
  99. End If
  100. ' Grab the web service object
  101. Err.Clear
  102. Set w3svc = GetObject("IIS://" & comp & "/w3svc")
  103. If Err.Number <> 0 Then
  104. Display "Unable to open: "&"IIS://" & comp & "/w3svc"
  105. End If
  106. BindingString = IpAddress & ":" & PortNum & ":" & HostName
  107. Trace "Making sure this web server doesn't conflict with another..."
  108. For Each WebServer in w3svc
  109. If WebServer.Class = "IIsWebServer" Then
  110. Bindings = WebServer.ServerBindings
  111. If BindingString = Bindings(0) Then
  112. Trace "The server bindings you specified are duplicated in another virtual web server."
  113. WScript.Quit (1)
  114. End If
  115. End If
  116. Next
  117. Index = 1
  118. bDone = False
  119. Trace "Creating new web server..."
  120. ' If the user specified a SiteNumber, then use that. Otherwise,
  121. ' test successive numbers under w3svc until an unoccupied slot is found
  122. If ArgSiteNumber <> 0 Then
  123. Set NewWebServer = w3svc.Create("IIsWebServer", ArgSiteNumber)
  124. NewWebServer.SetInfo
  125. If (Err.Number <> 0) Then
  126. WScript.Echo "Couldn't create a web site with the specified number: " & ArgSiteNumber
  127. WScript.Quit (1)
  128. Else
  129. Err.Clear
  130. ' Verify that the newly created site can be retrieved
  131. Set SiteObj = GetObject("IIS://"&comp&"/w3svc/" & ArgSiteNumber)
  132. If (Err.Number = 0) Then
  133. bDone = True
  134. Trace "Web server created. Path is - "&"IIS://"&comp&"/w3svc/" & ArgSiteNumber
  135. Else
  136. WScript.Echo "Couldn't create a web site with the specified number: " & ArgSiteNumber
  137. WScript.Quit (1)
  138. End If
  139. End If
  140. Else
  141. While (Not bDone)
  142. Err.Clear
  143. Set SiteObj = GetObject("IIS://"&comp&"/w3svc/" & Index)
  144. If (Err.Number = 0) Then
  145. ' A web server is already defined at this position so increment
  146. Index = Index + 1
  147. Else
  148. Err.Clear
  149. Set NewWebServer = w3svc.Create("IIsWebServer", Index)
  150. NewWebServer.SetInfo
  151. If (Err.Number <> 0) Then
  152. ' If call to Create failed then try the next number
  153. Index = Index + 1
  154. Else
  155. Err.Clear
  156. ' Verify that the newly created site can be retrieved
  157. Set SiteObj = GetObject("IIS://"&comp&"/w3svc/" & Index)
  158. If (Err.Number = 0) Then
  159. bDone = True
  160. Trace "Web server created. Path is - "&"IIS://"&comp&"/w3svc/" & Index
  161. Else
  162. Index = Index + 1
  163. End If
  164. End If
  165. End If
  166. ' sanity check
  167. If (Index > 10000) Then
  168. Trace "Seem to be unable to create new web server. Server number is "&Index&"."
  169. WScript.Quit (1)
  170. End If
  171. Wend
  172. End If
  173. NewBindings = Array(0)
  174. NewBindings(0) = BindingString
  175. NewWebServer.ServerBindings = NewBindings
  176. NewWebServer.ServerComment = ServerComment
  177. NewWebServer.SetInfo
  178. ' Now create the root directory object.
  179. Trace "Setting the home directory..."
  180. Set NewDir = NewWebServer.Create("IIsWebVirtualDir", "ROOT")
  181. NewDir.Path = RootDirectory
  182. NewDir.AccessRead = true
  183. Err.Clear
  184. NewDir.SetInfo
  185. NewDir.AppCreate2(2)
  186. If (Err.Number = 0) Then
  187. Trace "Home directory set."
  188. Else
  189. Display "Error setting home directory."
  190. End If
  191. Trace "Web site created!"
  192. If Start = True Then
  193. Trace "Attempting to start new web server..."
  194. Err.Clear
  195. Set NewWebServer = GetObject("IIS://" & comp & "/w3svc/" & Index)
  196. NewWebServer.Start
  197. If Err.Number <> 0 Then
  198. Display "Error starting web server!"
  199. Err.Clear
  200. Else
  201. Trace "Web server started succesfully!"
  202. End If
  203. End If
  204. Next
  205. End Sub
  206. ' Display the usage message
  207. Sub DisplayUsage
  208. WScript.Echo "Usage: mkw3site <--RootDirectory|-r ROOT DIRECTORY>"
  209. WScript.Echo " <--Comment|-t SERVER COMMENT>"
  210. WScript.Echo " [--computer|-c COMPUTER1[,COMPUTER2...]]"
  211. WScript.Echo " [--port|-o PORT NUM]"
  212. WScript.Echo " [--IPAddress|-i IP ADDRESS]"
  213. WScript.Echo " [--HostName|-h HOST NAME]"
  214. WScript.Echo " [--SiteNumber|-n SITENUMBER]"
  215. WScript.Echo " [--DontStart]"
  216. WScript.Echo " [--verbose|-v]"
  217. WScript.Echo " [--help|-?]"
  218. WScript.Echo "WARNING: Only use Host Name if DNS is set up find the server."
  219. WScript.Echo ""
  220. WScript.Echo "Example 1: mkw3site -r D:\Roots\Company11 --DontStart -t ""My Company Site"""
  221. WScript.Echo ""
  222. WScript.Quit (1)
  223. End Sub
  224. Sub Display(Msg)
  225. WScript.Echo Now & ". Error Code: " & Hex(Err) & " - " & Msg
  226. End Sub
  227. Sub Trace(Msg)
  228. if verbose = true then
  229. WScript.Echo Now & " : " & Msg
  230. end if
  231. End Sub