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
7.2 KiB

  1. '''''''''''''''''''''''''''''''''''''''''''''
  2. '
  3. ' FTP Virtual Directory Creation Utility
  4. '
  5. '''''''''''''''''''''''''''''''''''''''''''''
  6. ' Description:
  7. ' ------------
  8. ' This sample admin script allows you to create an FTP virtual directory.
  9. '
  10. ' To Run:
  11. ' -------
  12. ' This is the format for this script:
  13. '
  14. ' cscript mkftpdir.vbs <rootdir> [-n <instancenum>][-c <comment>][-p <portnum>][-X (don't start)]
  15. '
  16. ' NOTE: If you want to execute this script directly from Windows, use
  17. ' 'wscript' instead of 'cscript'.
  18. '
  19. ' Call this script with "-?" for usage instructions
  20. '
  21. '''''''''''''''''''''''''''''''''''''''''''''
  22. ' Force explicit declaration of all variables.
  23. Option Explicit
  24. On Error Resume Next
  25. Dim oArgs, ArgNum
  26. Dim ArgComputer, ArgFtpSites, ArgVirtualDirs, ArgDirNames(), ArgDirPaths(), DirIndex
  27. Dim ArgComputers
  28. Set oArgs = WScript.Arguments
  29. ArgComputers = Array("LocalHost")
  30. ArgNum = 0
  31. While ArgNum < oArgs.Count
  32. If (ArgNum + 1) >= oArgs.Count Then
  33. Call DisplayUsage
  34. End If
  35. Select Case LCase(oArgs(ArgNum))
  36. Case "--computer", "-c":
  37. ArgNum = ArgNum + 1
  38. ArgComputers = Split(oArgs(ArgNum), ",", -1)
  39. Case "--ftpsite", "-f":
  40. ArgNum = ArgNum + 1
  41. ArgFtpSites = oArgs(ArgNum)
  42. Case "--virtualdir", "-v":
  43. ArgNum = ArgNum + 1
  44. ArgVirtualDirs = Split(oArgs(ArgNum), ",", -1)
  45. Case "--help", "-?"
  46. Call DisplayUsage
  47. End Select
  48. ArgNum = ArgNum + 1
  49. Wend
  50. ArgNum = 0
  51. DirIndex = 0
  52. ReDim ArgDirNames((UBound(ArgVirtualDirs) + 1) \ 2)
  53. ReDim ArgDirPaths((UBound(ArgVirtualDirs) + 1) \ 2)
  54. If IsArray(ArgVirtualDirs) Then
  55. While ArgNum <= UBound(ArgVirtualDirs)
  56. ArgDirNames(DirIndex) = ArgVirtualDirs(ArgNum)
  57. If (ArgNum + 1) > UBound(ArgVirtualDirs) Then
  58. WScript.Echo "Error understanding virtual directories"
  59. Call DisplayUsage
  60. End If
  61. ArgNum = ArgNum + 1
  62. ArgDirPaths(DirIndex) = ArgVirtualDirs(ArgNum)
  63. ArgNum = ArgNum + 1
  64. DirIndex = DirIndex + 1
  65. Wend
  66. End If
  67. If (ArgFtpSites = "") Or (IsArray(ArgDirNames) = False Or IsArray(ArgDirPaths) = False) Then
  68. Call DisplayUsage
  69. Else
  70. Dim compIndex
  71. For compIndex = 0 To UBound(ArgComputers)
  72. Call ASTCreateVirtualFtpDir(ArgComputers(compIndex), ArgFtpSites, ArgDirNames, ArgDirPaths)
  73. Next
  74. End If
  75. '------------------------------------------------------------
  76. Sub Display(Msg)
  77. WScript.Echo Now & ". Error Code: " & Hex(Err) & " - " & Msg
  78. End Sub
  79. Sub Trace(Msg)
  80. WScript.Echo Now & " : " & Msg
  81. End Sub
  82. Sub DisplayUsage()
  83. WScript.Echo "Usage: mkftpdir [--computer|-c COMPUTER1,COMPUTER2]"
  84. WScript.Echo " <--ftpsite|-f FTPSITE1>"
  85. WScript.Echo " <--virtualdir|-v NAME1,PATH1,NAME2,PATH2,...>"
  86. WScript.Echo " [--help|-?]"
  87. WScript.Echo ""
  88. WScript.Echo "Note, FTPSITE is the Ftp Site on which the directory will be created."
  89. WScript.Echo "The name can be specified as one of the following, in the priority specified:"
  90. WScript.Echo " Server Number (i.e. 1, 2, 10, etc.)"
  91. WScript.Echo " Server Description (i.e ""My Server"")"
  92. WScript.Echo " Server Host name (i.e. ""ftp.domain.com"")"
  93. WScript.Echo " IP Address (i.e., 127.0.0.1)"
  94. WScript.Echo ""
  95. WScript.Echo ""
  96. WScript.Echo "Example : mkftpdir -c MyComputer -f ""Default Ftp Site"""
  97. WScript.Echo " -v ""dir1"",""c:\inetpub\ftproot\dir1"",""dir2"",""c:\inetpub\ftproot\dir2"""
  98. WScript.Quit
  99. End Sub
  100. '------------------------------------------------------------
  101. Sub ASTCreateVirtualFtpDir(ComputerName, FtpSiteName, DirNames, DirPaths)
  102. Dim computer, ftpSite, FtpSiteID, vRoot, vDir, DirNum
  103. On Error Resume Next
  104. Set ftpSite = findFtp(ComputerName, FtpSiteName)
  105. If IsObject(ftpSite) Then
  106. Set vRoot = ftpSite.GetObject("IIsFtpVirtualDir", "Root")
  107. Trace "Accessing root for " & ftpSite.ADsPath
  108. If (Err <> 0) Then
  109. Display "Unable to access root for " & ftpSite.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("IIsFtpVirtualDir", 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 "Ftp 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 " & FtpSiteName & " 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 findFtp(computer, ftpname)
  154. On Error Resume Next
  155. Dim ftpsvc, site
  156. Dim ftpinfo
  157. Dim aBinding, binding
  158. Set ftpsvc = GetObject("IIS://" & computer & "/MsFtpSvc")
  159. If (Err <> 0) Then
  160. Exit Function
  161. End If
  162. ' First try to open the ftpname.
  163. Set site = ftpsvc.GetObject("IIsFtpServer", ftpname)
  164. If (Err = 0) And (Not IsNull(site)) Then
  165. If (site.Class = "IIsFtpServer") Then
  166. ' Here we found a site that is a ftp server.
  167. Set findFtp = site
  168. Exit Function
  169. End If
  170. End If
  171. Err.Clear
  172. For Each site In ftpsvc
  173. If site.Class = "IIsFtpServer" Then
  174. ' First, check to see if the ServerComment matches
  175. If site.ServerComment = ftpname Then
  176. Set findFtp = site
  177. Exit Function
  178. End If
  179. aBinding = site.ServerBindings
  180. If (IsArray(aBinding)) Then
  181. If aBinding(0) = "" Then
  182. binding = Null
  183. Else
  184. binding = getBinding(aBinding(0))
  185. End If
  186. Else
  187. If aBinding = "" Then
  188. binding = Null
  189. Else
  190. binding = getBinding(aBinding)
  191. End If
  192. End If
  193. If IsArray(binding) Then
  194. If (binding(2) = ftpname) Or (binding(0) = ftpname) Then
  195. Set findFtp = site
  196. Exit Function
  197. End If
  198. End If
  199. End If
  200. Next
  201. End Function