脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - VBS - 改进后的mkw3site.vbs(创建虚拟目录)

改进后的mkw3site.vbs(创建虚拟目录)

2020-07-09 09:34VBS代码网 VBS

本文主要讲解使用vbs代码创建虚拟目录的方法,如果您需要设置权限,请修改40-56 的代码。

  1. '---------------------------------------------------------------------------------------------------  
  2. ' 创建虚拟目录  POWER BY JARON , 江都资讯网 , 1999-2002.   
  3. ' 如果您需要设置权限,请修改40-56 的代码。 ** 根据 Microsoft Corp. 的 AdminScripts 改写  
  4. '  
  5. ' 用法: 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. ' SITENUMBERThe 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.  
  29.  
  30. ' Force explicit declaration of all variables  
  31. Option Explicit   
  32.  
  33. On Error Resume Next   
  34.  
  35. Dim ArgIPAddress, ArgRootDirectory, ArgServerComment, ArgSkeletalDir, ArgHostName, ArgPort  
  36. Dim ArgComputers, ArgStart  
  37. Dim ArgSiteNumber  
  38. Dim oArgs, ArgNum  
  39. Dim verbose  
  40. ' 设置可写、脚本执行权限  
  41. Dim prop(15,2)  
  42. Dim propNum  
  43. prop(propNum,0) = "AccessRead"  
  44. prop(propNum,1) = true' 可读设为TRUE,不可读设为FALSE  
  45. propNum = propNum + 1  
  46. prop(propNum, 0) = "AccessWrite"  
  47. prop(propNum, 1) = true ' 可写设为TRUE,不可写设为FALSE  
  48. propNum = propNum + 1  
  49. prop(propNum, 0) = "AccessScript"  
  50. prop(propNum, 1) = true ' 可运行脚本文件设为TRUE,不可运行脚本文件设为FALSE  
  51. propNum = propNum + 1  
  52. prop(propNum, 0) = "AccessExecute"  
  53. prop(propNum, 1) = false ' 可运行执行文件设为TRUE,不可运行执行文件设为FALSE  
  54. propNum = propNum + 1  
  55. prop(propNum, 0) = "EnableDirBrowsing"  
  56. prop(propNum, 1) = true ' 允许列出目录设为TRUE,不允许列出目录设为FALSE  
  57. propNum = propNum + 1   
  58.  
  59. ArgIPAddress = ""  
  60. ArgHostName = ""  
  61. ArgPort = 80  
  62. ArgStart = True  
  63. ArgComputers = Array(1)  
  64. ArgComputers(0) = "LocalHost"  
  65. ArgSiteNumber = 0  
  66. verbose = false   
  67.  
  68. Set oArgs = WScript.Arguments  
  69. ArgNum = 0   
  70.  
  71. While ArgNum < oArgs.Count   
  72.  
  73. Select Case LCase(oArgs(ArgNum))  
  74. Case "--port","-o":  
  75. ArgNum = ArgNum + 1  
  76. ArgPort = oArgs(ArgNum)  
  77. Case "--ipaddress","-i":  
  78. ArgNum = ArgNum + 1  
  79. ArgIPAddress = oArgs(ArgNum)  
  80. Case "--rootdirectory","-r":   
  81. ArgNum = ArgNum + 1  
  82. ArgRootDirectory = oArgs(ArgNum)  
  83. Case "--comment","-t":  
  84. ArgNum = ArgNum + 1  
  85. ArgServerComment = oArgs(ArgNum)  
  86. Case "--hostname","-h":  
  87. ArgNum = ArgNum + 1  
  88. ArgHostName = oArgs(ArgNum)  
  89. Case "--computer","-c":  
  90. ArgNum = ArgNum + 1  
  91. ArgComputers = Split(oArgs(ArgNum), ",", -1)  
  92. Case "--sitenumber","-n":  
  93. ArgNum = ArgNum + 1  
  94. ArgSiteNumber = CLng(oArgs(ArgNum))  
  95. Case "--dontstart":  
  96. ArgStart = False  
  97. Case "--help","-?":  
  98. Call DisplayUsage  
  99. Case "--verbose""-v":  
  100. verbose = true  
  101. Case Else:  
  102. WScript.Echo "Unknown argument "& oArgs(ArgNum)  
  103. Call DisplayUsage  
  104. End Select   
  105.  
  106. ArgNum = ArgNum + 1  
  107. Wend   
  108.  
  109. If (ArgRootDirectory = "") Or (ArgServerComment = "") Then  
  110. if (ArgRootDirectory = "") then  
  111. WScript.Echo "Missing Root Directory"  
  112. else  
  113. WScript.Echo "Missing Server Comment"  
  114. end if  
  115. Call DisplayUsage  
  116. WScript.Quit(1)  
  117. End If   
  118.  
  119. Call ASTCreateWebSite(ArgIPAddress, ArgRootDirectory, ArgServerComment, ArgHostName, ArgPort, ArgComputers, ArgStart)   
  120.  
  121. Sub ASTCreateWebSite(IPAddress, RootDirectory, ServerComment, HostName, PortNum, Computers, Start)  
  122. Dim w3svc, WebServer, NewWebServer, NewDir, Bindings, BindingString, NewBindings, ComputerIndex, Index, SiteObj, bDone  
  123. Dim comp  
  124. On Error Resume Next  
  125. For ComputerIndex = 0 To UBound(Computers)  
  126. comp = Computers(ComputerIndex)  
  127. If ComputerIndex <> UBound(Computers) Then  
  128. Trace "Creating web site on " & comp & "."  
  129. End If   
  130.  
  131. ' Grab the web service object  
  132. Err.Clear  
  133. Set w3svc = GetObject("IIS://" & comp & "/w3svc")  
  134. If Err.Number <> 0 Then  
  135. Display "Unable to open: "&"IIS://" & comp & "/w3svc"  
  136. End If  
  137. BindingString = IpAddress & ":" & PortNum & ":" & HostName  
  138. Trace "Making sure this web server doesn't conflict with another..."  
  139. For Each WebServer in w3svc  
  140. If WebServer.Class = "IIsWebServer" Then  
  141. Bindings = WebServer.ServerBindings  
  142. If BindingString = Bindings(0) Then  
  143. Trace "The server bindings you specified are duplicated in another virtual web server."  
  144. WScript.Quit (1)  
  145. End If  
  146. End If  
  147. Next   
  148.  
  149. Index = 1  
  150. bDone = False  
  151. Trace "Creating new web server..."   
  152.  
  153. ' If the user specified a SiteNumber, then use that.  Otherwise,  
  154. ' test successive numbers under w3svc until an unoccupied slot is found  
  155. If ArgSiteNumber <> 0 Then  
  156. Set NewWebServer = w3svc.Create("IIsWebServer", ArgSiteNumber)  
  157. NewWebServer.SetInfo  
  158. If (Err.Number <> 0) Then  
  159. WScript.Echo "Couldn't create a web site with the specified number: " & ArgSiteNumber  
  160. WScript.Quit (1)  
  161. Else  
  162. Err.Clear  
  163. ' Verify that the newly created site can be retrieved  
  164. Set SiteObj = GetObject("IIS://"&comp&"/w3svc/" & ArgSiteNumber)  
  165. If (Err.Number = 0) Then  
  166. bDone = True  
  167. Trace "Web server created. Path is - "&"IIS://"&comp&"/w3svc/" & ArgSiteNumber  
  168. Else  
  169. WScript.Echo "Couldn't create a web site with the specified number: " & ArgSiteNumber  
  170. WScript.Quit (1)  
  171. End If  
  172. End If  
  173. Else  
  174. While (Not bDone)  
  175. Err.Clear  
  176. Set SiteObj = GetObject("IIS://"&comp&"/w3svc/" & Index)   
  177.  
  178. If (Err.Number = 0) Then  
  179. ' A web server is already defined at this position so increment  
  180. Index = Index + 1  
  181. Else  
  182. Err.Clear  
  183. Set NewWebServer = w3svc.Create("IIsWebServer", Index)  
  184. NewWebServer.SetInfo  
  185. If (Err.Number <> 0) Then  
  186. ' If call to Create failed then try the next number  
  187. Index = Index + 1  
  188. Else  
  189. Err.Clear  
  190. ' Verify that the newly created site can be retrieved  
  191. Set SiteObj = GetObject("IIS://"&comp&"/w3svc/" & Index)  
  192. If (Err.Number = 0) Then  
  193. bDone = True  
  194. Trace "Web server created. Path is - "&"IIS://"&comp&"/w3svc/" & Index  
  195. Else  
  196. Index = Index + 1  
  197. End If  
  198. End If  
  199. End If   
  200.  
  201. ' sanity check  
  202. If (Index > 10000) Then  
  203. Trace "Seem to be unable to create new web server.  Server number is "&Index&"."  
  204. WScript.Quit (1)  
  205. End If  
  206. Wend  
  207. End If  
  208. NewBindings = Array(0)  
  209. NewBindings(0) = BindingString  
  210. NewWebServer.ServerBindings = NewBindings  
  211. NewWebServer.ServerComment = ServerComment  
  212. NewWebServer.SetInfo   
  213.  
  214. ' Now create the root directory object.  
  215. Trace "Setting the home directory..."  
  216. Set NewDir = NewWebServer.Create("IIsWebVirtualDir""ROOT")  
  217. NewDir.Path = RootDirectory  
  218. NewDir.AccessRead = true  
  219. Err.Clear  
  220. NewDir.SetInfo  
  221. NewDir.AppCreate (True)   
  222.  
  223. If (Err.Number = 0) Then  
  224. Trace "Home directory set."  
  225. Else  
  226. Display "Error setting home directory."  
  227. End If   
  228.  
  229. Trace "Web site created!"   
  230.  
  231. If Start = True Then  
  232. Trace "Attempting to start new web server..."  
  233. Err.Clear  
  234. Set NewWebServer = GetObject("IIS://" & comp & "/w3svc/" & Index)  
  235. NewWebServer.Start  
  236. If Err.Number <> 0 Then  
  237. Display "Error starting web server!"  
  238. Err.Clear  
  239. Else  
  240. Trace "Web server started succesfully!"  
  241. End If  
  242. End If  
  243. Next  
  244. Call ASTSetPerms(comp, Index,ArgRootDirectory , prop, propNum)  
  245. End Sub   
  246.  
  247. Sub ASTSetPerms(comp, ArgSiteNumber,ArgRootDirectory , propList, propCount)  
  248. 'On Error Resume Next  
  249. Dim oAdmin  
  250. Dim fullPath  
  251. fullPath = "IIS://"&comp&"/w3svc/" & ArgSiteNumber & "/ROOT"  
  252. Trace "Opening path " & fullPath  
  253. Set oAdmin = GetObject(fullPath)  
  254. If Err.Number <> 0 Then  
  255. Display Error_NoNode  
  256. WScript.Quit (1)  
  257. End If   
  258.  
  259. Dim name, val  
  260. if propCount > 0 then  
  261. Dim i   
  262.  
  263. for i = 0 to propCount-1  
  264. name = propList(i,0)  
  265. val = propList(i,1)  
  266. if verbose = true then  
  267. Trace "Setting "&fullPath&"/"&name&" = "& val  
  268. end if  
  269. oAdmin.Put name, (val)  
  270. If Err <> 0 Then  
  271. Display "Unable to set property "&name  
  272. End If  
  273. next  
  274. oAdmin.SetInfo  
  275. If Err <> 0 Then  
  276. Display "不能保存更新信息."  
  277. End If  
  278. end if  
  279. End Sub   
  280.  
  281. ' Display the usage message  
  282. Sub DisplayUsage  
  283. WScript.Quit (1)  
  284. End Sub   
  285.  
  286. Sub Display(Msg)  
  287. WScript.Echo Now & ". Error Code: " & Hex(Err) & " - " & Msg  
  288. End Sub   
  289.  
  290. Sub Trace(Msg)  
  291. if verbose = true then  
  292. WScript.Echo Now & " : " & Msg  
  293. end if  
  294. End Sub  

延伸 · 阅读

精彩推荐