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.

227 lines
5.9 KiB

  1. '---------------------------------------------------------------------------------------------------
  2. ' This function searches a computer for a specified Web Site, and
  3. ' displays information about the site.
  4. '
  5. ' findweb [--computer|-c COMPUTER]
  6. ' [WEBSITE]
  7. ' [--help|-?]
  8. '
  9. 'COMPUTER Computer on which users exists
  10. 'WEBSITE Virtual Web Sites on which directories will be created
  11. '
  12. 'Example 1 findweb -c LocalHost "Default Web Site"
  13. '
  14. '---------------------------------------------------------------------------------------------------
  15. ' Force explicit declaration of all variables.
  16. Option Explicit
  17. 'On Error Resume Next
  18. Dim oArgs, ArgNum
  19. Dim ArgComputer, ArgWebSites, ArgVirtualDirs, ArgDirNames(), ArgDirPaths(), DirIndex
  20. Dim ArgComputers
  21. Set oArgs = WScript.Arguments
  22. ArgComputers = Array("LocalHost")
  23. ArgWebSites = "1"
  24. ArgNum = 0
  25. While ArgNum < oArgs.Count
  26. Select Case LCase(oArgs(ArgNum))
  27. Case "--computer","-c":
  28. ArgNum = ArgNum + 1
  29. If (ArgNum >= oArgs.Count) Then
  30. Call DisplayUsage
  31. End If
  32. ArgComputers = Split(oArgs(ArgNum), ",", -1)
  33. Case "--help","-?"
  34. Call DisplayUsage
  35. Case Else:
  36. ArgWebSites = oArgs(ArgNum)
  37. End Select
  38. ArgNum = ArgNum + 1
  39. Wend
  40. Dim foundSite
  41. Dim compIndex
  42. Dim bindInfo
  43. Dim aBinding, binding
  44. Dim hostname
  45. for compIndex = 0 to UBound(ArgComputers)
  46. set foundSite = findWeb(ArgComputers(compIndex), ArgWebSites)
  47. if isObject(foundSite) then
  48. Trace " Web Site Number = " & foundSite.Name
  49. Trace " Web Site Description = " & foundSite.ServerComment
  50. aBinding = foundSite.ServerBindings
  51. if (IsArray(aBinding)) then
  52. if aBinding(0) = "" then
  53. binding = Null
  54. else
  55. binding = getBinding(ArgComputers(compIndex), aBinding(0))
  56. end if
  57. else
  58. if aBinding = "" then
  59. binding = Null
  60. else
  61. binding = getBinding(ArgComputers(compIndex), aBinding)
  62. end if
  63. end if
  64. if IsArray(binding) then
  65. Trace " Hostname = " & binding(2)
  66. Trace " Port = " & binding(1)
  67. Trace " IP Address = " & binding(0)
  68. end if
  69. else
  70. Trace "No matching web found."
  71. end if
  72. next
  73. function getBinding(hostName, bindstr)
  74. Dim one, two, ia, ip, hn
  75. Dim netw, host
  76. one = Instr(bindstr, ":")
  77. two = Instr((one + 1), bindstr, ":")
  78. ia = Mid(bindstr, 1, (one - 1))
  79. ip = Mid(bindstr, (one + 1), ((two - one) - 1))
  80. hn = Mid(bindstr, (two + 1))
  81. if ia = "" or hn = "" then
  82. Set netw = CreateObject("WScript.Network")
  83. if UCase(hostName) = "LOCALHOST" then
  84. host = resolveHostName(netw.ComputerName)
  85. else
  86. host = resolveHostName(hostName)
  87. end if
  88. if IsArray(host) then
  89. hn = host(0)
  90. ia = host(1)
  91. end if
  92. end if
  93. getBinding = Array(ia, ip, hn)
  94. end function
  95. Function findWeb(computer, webname)
  96. On Error Resume Next
  97. Dim websvc, site
  98. dim webinfo
  99. Dim aBinding, binding
  100. set websvc = GetObject("IIS://"&computer&"/W3svc")
  101. if (Err <> 0) then
  102. exit function
  103. end if
  104. ' First try to open the webname.
  105. set site = websvc.GetObject("IIsWebServer", webname)
  106. if (Err = 0) and (not isNull(site)) then
  107. if (site.class = "IIsWebServer") then
  108. ' Here we found a site that is a web server.
  109. set findWeb = site
  110. exit function
  111. end if
  112. end if
  113. err.clear
  114. for each site in websvc
  115. if site.class = "IIsWebServer" then
  116. '
  117. ' First, check to see if the ServerComment
  118. ' matches
  119. '
  120. If site.ServerComment = webname Then
  121. set findWeb = site
  122. exit function
  123. End If
  124. aBinding=site.ServerBindings
  125. if (IsArray(aBinding)) then
  126. if aBinding(0) = "" then
  127. binding = Null
  128. else
  129. binding = getBinding(computer, aBinding(0))
  130. end if
  131. else
  132. if aBinding = "" then
  133. binding = Null
  134. else
  135. binding = getBinding(computer, aBinding)
  136. end if
  137. end if
  138. if IsArray(binding) then
  139. if (binding(2) = webname) or (binding(0) = webname) then
  140. set findWeb = site
  141. exit function
  142. End If
  143. end if
  144. end if
  145. next
  146. End Function
  147. ' Receives a NetBIOS hostname end return the FQDN for that host
  148. Function resolveHostName(hostName)
  149. Dim ShellObj, FSObj
  150. Dim workFile, textFile, lines, index, tempSplit
  151. Dim result(2)
  152. Set ShellObj = CreateObject("WScript.Shell")
  153. Set FSObj = CreateObject("Scripting.FileSystemObject")
  154. workFile = FSObj.GetTempName
  155. ' Execute "nslookup <hostName>
  156. ShellObj.Run "%COMSPEC% /c nslookup " & hostName & " > " & workFile, 0, true
  157. Set ShellObj = nothing
  158. Set textFile = FSObj.OpenTextFile(workFile)
  159. lines = split(textFile.ReadAll, VBCrLf)
  160. textFile.Close
  161. Set textFile = nothing
  162. FSObj.DeleteFile workFile
  163. Set FSObj = nothing
  164. for index = 0 to UBound(lines)
  165. ' Look for "Name: host.domain"
  166. if instr(lines(index), "Name:") then
  167. tempSplit = split(lines(index), ":")
  168. result(0) = Trim(tempSplit(1))
  169. tempSplit = split(lines(index + 1), ":")
  170. result(1) = Trim(tempSplit(1))
  171. resolveHostName = result
  172. exit for
  173. end if
  174. next
  175. End Function
  176. '---------------------------------------------------------------------------------
  177. Sub Display(Msg)
  178. WScript.Echo Now & ". Error Code: " & Hex(Err) & " - " & Msg
  179. End Sub
  180. Sub Trace(Msg)
  181. WScript.Echo Msg
  182. End Sub
  183. Sub DisplayUsage()
  184. WScript.Echo " findweb [--computer|-c COMPUTER]"
  185. WScript.Echo " [WEBSITE]"
  186. WScript.Echo " [--help|-?]"
  187. WScript.Echo ""
  188. WScript.Echo "Finds the named web on the specified computer."
  189. WScript.Echo "Displays the site number, description, host name, port,"
  190. WScript.Echo "and IP Address"
  191. WScript.Echo ""
  192. WScript.Echo "Note, WEBSITE is the name of the Web Site to look for."
  193. WScript.Echo "The name can be specified as one of the following, in the priority specified:"
  194. WScript.Echo " Server Number (i.e. 1, 2, 10, etc.)"
  195. WScript.Echo " Server Description (i.e ""My Server"")"
  196. WScript.Echo " Server Host name (i.e. ""www.domain.com"")"
  197. WScript.Echo " IP Address (i.e., 127.0.0.1)"
  198. WScript.Echo ""
  199. WScript.Echo "Example findweb -c MACHINE www.mycompany.com"
  200. WScript.Quit
  201. End Sub
  202. '---------------------------------------------------------------------------------