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.

1232 lines
39 KiB

  1. <?xml version="1.0"?>
  2. <package>
  3. <component id="IIS Script Helper">
  4. <?component error="true" debug="true" ?>
  5. <registration progid="Microsoft.IIsScriptHelper" classid="{BC47120F-1612-4CA5-A89F-FDFF76C28AB6}" description="IIS Script Helper" version="1.0">
  6. </registration>
  7. <public>
  8. <property internalname="WScript" name="ScriptHost">
  9. </property>
  10. <property name="ProviderObj">
  11. <get/>
  12. </property>
  13. <property name="Switches">
  14. <get/>
  15. </property>
  16. <property internalname="aNamedArguments" name="NamedArguments">
  17. <get/>
  18. </property>
  19. <property name="GlobalHelpRequested">
  20. <get/>
  21. </property>
  22. <property name="FSObj">
  23. <get/>
  24. </property>
  25. <property name="ERROR_UNKNOWN_SWITCH">
  26. <get/>
  27. </property>
  28. <property name="ERROR_NOT_ENOUGH_ARGS">
  29. <get/>
  30. </property>
  31. <method name="BuildNameSpace">
  32. <parameter name="strPath"/>
  33. </method>
  34. <method name="CheckScriptEngine">
  35. </method>
  36. <method name="CreateFSDir">
  37. <parameter name="strRoot"/>
  38. </method>
  39. <method name="DumpCmdLineOptions">
  40. </method>
  41. <method name="FindSite">
  42. <parameter name="strType"/>
  43. <parameter name="aArgs"/>
  44. </method>
  45. <method name="GetAbsolutePath">
  46. <parameter name="strPath"/>
  47. </method>
  48. <method name="GetEnvironmentVar">
  49. <parameter name="strVar"/>
  50. </method>
  51. <method name="GetSwitch">
  52. <parameter name="strSwitchName"/>
  53. </method>
  54. <method name="InitAuthentication">
  55. <parameter name="Server"/>
  56. <parameter name="User"/>
  57. <parameter name="Password"/>
  58. </method>
  59. <method name="IsHelpRequested">
  60. <parameter name="strSwitch"/>
  61. </method>
  62. <method name="IsHelpSwitch">
  63. <parameter name="strSwitch"/>
  64. </method>
  65. <method name="IsValidIPAddress">
  66. <parameter name="strIPAddress"/>
  67. </method>
  68. <method name="IsValidPortNumber">
  69. <parameter name="intPort"/>
  70. </method>
  71. <method name="NormalizeFilePath">
  72. <parameter name="strPath"/>
  73. </method>
  74. <method name="ParseBindings">
  75. <parameter name="bindings"/>
  76. </method>
  77. <method name="ParseCmdLineOptions">
  78. <parameter name="ArgObj"/>
  79. <parameter name="strCmdLine"/>
  80. </method>
  81. <method name="WMIConnect">
  82. <parameter name="strServer"/>
  83. <parameter name="strUser"/>
  84. <parameter name="strPassword"/>
  85. </method>
  86. </public>
  87. <object id="FSObj" progid="Scripting.FileSystemObject" events="false"/>
  88. <object id="ShellObj" progid="WScript.Shell" events="false"/>
  89. <object id="NetObj" progid="WScript.Network" events="false"/>
  90. <object id="DictObj" progid="Scripting.Dictionary" events="false"/>
  91. <resource id="ProductInfoRegValue">ProductSuite</resource>
  92. <resource id="ProductInfoRegKey">System\CurrentControlSet\Control\ProductOptions</resource>
  93. <resource id="L_RegProc_ErrorMessage">Error querying the WMI Registry provider.</resource>
  94. <resource id="L_OnlyIIS6Supported_ErrorMessage">The IIS Admin scripts only support IIS 6.0.</resource>
  95. <resource id="L_CredentialsIgnored_Message">Using local machine will cause supplied credentials to be ignored.</resource>
  96. <resource id="L_Warning_Text">WARNING</resource>
  97. <resource id="L_WriteReg_ErrorMessage">Error trying to write the registry settings!</resource>
  98. <resource id="L_MetabasePath_Message">Metabase Path</resource>
  99. <resource id="L_SiteName_Text">Site Name</resource>
  100. <resource id="L_NotUnique2_Message">identify these sites:</resource>
  101. <resource id="L_NotUnique1_Message">The following site names are not unique. Please use the Metabase Paths to</resource>
  102. <resource id="L_Done_Message">Done.</resource>
  103. <resource id="L_ConnectObject_ErrorMessage">Error trying to get WMI SWbemService object</resource>
  104. <resource id="L_Error_ErrorMessage">Error</resource>
  105. <resource id="L_Locator_ErrorMessage">Error trying to get WMI SWbemLocator object</resource>
  106. <resource id="L_Connecting_Message">Connecting to server ... </resource>
  107. <resource id="L_OkWriteReg_Message">Successfully registered CScript</resource>
  108. <resource id="L_UseCScript_Message">To run this script type: "CScript.Exe IIsCnfg.vbs [params]"</resource>
  109. <resource id="CIMv2_NAMESPACE">root/CIMv2</resource>
  110. <resource id="WMI_NAMESPACE">root/MicrosoftIISv2</resource>
  111. <resource id="LOCATOR_OBJ">WbemScripting.SWbemLocator</resource>
  112. <resource id="WBemImpersonationLevelImpersonate">3</resource>
  113. <resource id="WQL">WQL</resource>
  114. <resource id="L_RegisterCScript_Message">Register CScript</resource>
  115. <resource id="L_AskChangeScriptProcessor_Message">Would you like to register CScript as your default host for VBscript?</resource>
  116. <resource id="L_WrongScriptProcessor_Message">This script does not work with WScript.</resource>
  117. <resource id="CONST_NO_MATCHES_FOUND">0</resource>
  118. <resource id="PATTERN_VBPRINTF">%\d</resource>
  119. <script id="IIs Script Helper" language="VBScript">
  120. <![CDATA[
  121. '
  122. ' Copyright (c) Microsoft Corporation. All rights reserved.
  123. '
  124. ' VBScript Source File
  125. '
  126. ' Script Component Name: IIsScHlp.wsc
  127. '
  128. Option Explicit
  129. On Error Resume Next
  130. Dim LocatorObj, ProviderObj
  131. Dim dictSwitches, dictHelpRequested
  132. Dim aNamedArguments
  133. Dim fGlobalHelpRequested
  134. Dim strServer, strUser, strPassword
  135. ' Parser errors
  136. Const ERROR_NOT_ENOUGH_ARGS = 1
  137. Const ERROR_UNKNOWN_SWITCH = 2
  138. ' Object initialization
  139. fGlobalHelpRequested = False
  140. Set LocatorObj = Nothing
  141. Set ProviderObj = Nothing
  142. Set dictSwitches = Nothing
  143. Set dictHelpRequested = Nothing
  144. aNamedArguments = Array()
  145. ' Property get methods
  146. Function get_ProviderObj()
  147. Set get_ProviderObj = ProviderObj
  148. End Function
  149. Function get_Switches()
  150. Set get_Switches = dictSwitches
  151. End Function
  152. Function get_aNamedArguments()
  153. get_aNamedArguments = aNamedArguments
  154. End Function
  155. Function get_GlobalHelpRequested()
  156. get_GlobalHelpRequested = fGlobalHelpRequested
  157. End Function
  158. Function get_FSObj()
  159. Set get_FSObj = FSObj
  160. End Function
  161. Function get_ERROR_UNKNOWN_SWITCH()
  162. get_ERROR_UNKNOWN_SWITCH = ERROR_UNKOWN_SWITCH
  163. End Function
  164. Function get_ERROR_NOT_ENOUGH_ARGS()
  165. get_ERROR_NOT_ENOUGH_ARGS = ERROR_NOT_ENOUGH_ARGS
  166. End Function
  167. '''''''''''''''''''''''''''''''''
  168. ' Class Definitions
  169. ''''''''''''''''''''''
  170. Class OptionItem
  171. Public Name
  172. Public ShortName
  173. Public RequiredArgs
  174. Public GroupID
  175. Public fSearchChildren
  176. Public aChildOptions
  177. Public Sub SetInfo(strName, strShortName, strReqArg, intGroupID)
  178. If Left(strName, 1) = "[" Then
  179. Name = Mid(strName, 2)
  180. Else
  181. Name = CStr(strName)
  182. End If
  183. ShortName = CStr(strShortName)
  184. If Right(strReqArg, 1) = "]" Then
  185. RequiredArgs = Mid(strReqArg, 1, Len(strReqArg) - 1)
  186. Else
  187. RequiredArgs = CStr(strReqArg)
  188. End If
  189. GroupID = CInt(intGroupID)
  190. fSearchChildren = False
  191. aChildOptions = Empty
  192. End Sub
  193. Public Sub AddChild(element)
  194. If IsEmpty(aChildOptions) Then
  195. aChildOptions = Array(element)
  196. Else
  197. ReDim Preserve aChildOptions(Ubound(aChildOptions) + 1)
  198. Set aChildOptions(Ubound(aChildOptions)) = element
  199. End If
  200. End Sub
  201. Public Sub Visit()
  202. ' This options was recognized. If it has child options, make them available
  203. If Not IsEmpty(aChildOptions) Then
  204. fSearchChildren = True
  205. End If
  206. End Sub
  207. End Class
  208. Class Options
  209. Private intOptionIndex
  210. Public aOptions
  211. Public Sub SetOptions(strCmdLineKeys)
  212. Dim aCmdLineOptions, aOption
  213. Dim intCount, i
  214. aCmdLineOptions = Split(strCmdLineKeys, ";")
  215. ReDim aOptions(UBound(aCmdLineOptions))
  216. intOptionIndex = LBound(aCmdLineOptions)
  217. InsertOptionsInArray aOptions, aCmdLineOptions, Empty
  218. End Sub
  219. Public Function GetInfo(strName)
  220. Set GetInfo = Lookup(aOptions, strName)
  221. End Function
  222. '
  223. ' Private functions/subrotines
  224. '
  225. Private Function Lookup(aArray, strName)
  226. Dim oOption
  227. Dim oResult
  228. Dim i
  229. Set oResult = Nothing
  230. For i = LBound(aArray) to UBound(aArray)
  231. Set oOption = aArray(i)
  232. If UCase(oOption.Name) = UCase(strName) Or UCase(oOption.ShortName) = UCase(strName) Then
  233. Set oResult = oOption
  234. Exit For
  235. End If
  236. If oOption.fSearchChildren Then
  237. Set oResult = Lookup(oOption.aChildOptions, strName)
  238. If Not oResult Is Nothing Then
  239. Exit For
  240. End If
  241. End If
  242. Next
  243. Set Lookup = oResult
  244. End Function
  245. ' InsertOptionsInArray(
  246. ' array to receive the options,
  247. ' options array to be parser,
  248. ' start index of the options array above,
  249. ' current scope (-1 to root)
  250. ')
  251. Private Sub InsertOptionsInArray(aArray, aCmdLineOptions, intScope)
  252. Dim intCount, i
  253. Dim aOption, oOption
  254. intCount = 0
  255. Do While intOptionIndex <= UBound(aCmdLineOptions)
  256. aOption = Split(aCmdLineOptions(intOptionIndex), ":")
  257. Set oOption = New OptionItem
  258. oOption.SetInfo aOption(0), aOption(1), aOption(2), intScope
  259. ' First, do we see a start of a block ('[')?
  260. If Left(aOption(0), 1) = "[" Then
  261. intOptionIndex = intOptionIndex + 1
  262. InsertOptionsInArray oOption, aCmdLineOptions, intScope + 1
  263. End If
  264. If IsArray(aArray) Then
  265. Set aArray(intCount) = oOption
  266. Else
  267. ' aArray is actually an object
  268. aArray.AddChild oOption
  269. End If
  270. ' Now, do we see an end of a block (']')?
  271. If Right(aOption(UBound(aOption)), 1) = "]" Then
  272. Exit Sub
  273. End If
  274. intCount = intCount + 1
  275. intOptionIndex = intOptionIndex + 1
  276. Loop
  277. ReDim Preserve aArray(intCount - 1)
  278. End Sub
  279. End Class
  280. Class ParserError
  281. Public SwitchName
  282. Public ErrorCode
  283. End Class
  284. ''''''''''''''''''''''''''''''''''''
  285. ' Methods
  286. '''''''''''''''''''''''''
  287. ' Initialization
  288. Function InitAuthentication(Server, User, Password)
  289. Dim DefaultNamespaceObj, RegistryObj
  290. Dim IISNameSpaceObj, ComputerObj
  291. Dim iMajorVersion, iResult
  292. Dim aResult
  293. On Error Resume Next
  294. iResult = 0
  295. strServer = Server
  296. strUser = User
  297. strPassword = Password
  298. If Server = "." Or UCase(Server) = UCase(GetEnvironmentVar("%COMPUTERNAME%")) Then
  299. If User <> "" Or Password <> "" Then
  300. WScript.Echo getResource("L_Warning_Text") & ": " & getResource("L_CredentialsIgnored_Message")
  301. strUser = ""
  302. strPassword = ""
  303. End If
  304. End If
  305. ' Initializes the WMI Locator object
  306. Set LocatorObj = CreateObject(getResource("LOCATOR_OBJ"))
  307. If Err.Number Then
  308. WScript.Echo getResource("L_Locator_ErrorMessage")
  309. WScript.Echo getResource("L_Error_ErrorMessage") & " &H" & Hex(Err.Number) & ": " & Err.Description
  310. InitAuthentication = Err.Number
  311. Exit Function
  312. End If
  313. LocatorObj.Security_.ImpersonationLevel = getResource("WBemImpersonationLevelImpersonate")
  314. ' Check if target machine has IIS6 installed (server and above)
  315. Set IISNameSpaceObj = LocatorObj.ConnectServer(strServer, getResource("WMI_NAMESPACE"), strUser, strPassword)
  316. If Err.Number Then
  317. ' Error connecting to the IIS namespace. If NOT_FOUND, this is probably not a Win2002 box
  318. If Err.Number = &H8004100E Then ' INVALID_NAMESPACE
  319. WScript.Echo getResource("L_OnlyIIS6Supported_ErrorMessage")
  320. Else
  321. WScript.Echo getResource("L_ConnectObject_ErrorMessage")
  322. WScript.Echo getResource("L_Error_ErrorMessage") & " &H" & Hex(Err.Number) & ": " & Err.Description
  323. End If
  324. InitAuthentication = Err.Number
  325. Exit Function
  326. End If
  327. Set ComputerObj = IISNameSpaceObj.get("IIsWebInfo='W3SVC/Info'")
  328. If Err.Number Then
  329. WScript.Echo getResource("L_OnlyIIS6Supported_ErrorMessage")
  330. InitAuthentication = Err.Number
  331. Exit Function
  332. End If
  333. iMajorVersion = ComputerObj.MD_SERVER_VERSION_MAJOR
  334. If Err.Number Or iMajorVersion <> 6 Then
  335. WScript.Echo getResource("L_OnlyIIS6Supported_ErrorMessage")
  336. InitAuthentication = 1
  337. Exit Function
  338. End If
  339. ' Set DefaultNamespaceObj = LocatorObj.ConnectServer(strServer, "root\default", strUser, strPassword)
  340. ' If Err.Number Then
  341. ' WScript.Echo getResource("L_ConnectObject_ErrorMessage")
  342. ' WScript.Echo getResource("L_Error_ErrorMessage") & " &H" & Hex(Err.Number) & ": " & Err.Description
  343. ' InitAuthentication = Err.Number
  344. ' Exit Function
  345. ' End If
  346. '
  347. ' Set RegistryObj = DefaultNamespaceObj.get("StdRegProv")
  348. ' iResult = RegistryObj.GetMultiStringValue(, getResource("ProductInfoRegKey"), _
  349. ' getResource("ProductInfoRegValue"), _
  350. ' aResult)
  351. ' If iResult <> 0 Or Err Then
  352. ' WScript.Echo getResource("L_RegProc_ErrorMessage")
  353. ' If iResult Then
  354. ' InitAuthentication = iResult
  355. ' Else
  356. ' InitAuthentication = Err.Number
  357. ' End If
  358. ' Exit Function
  359. ' Else
  360. ' If UBound(aResult) < 0 Then
  361. ' ' Target machine is PRO
  362. ' WScript.Echo getResource("L_OnlyIIS6Supported_ErrorMessage")
  363. ' InitAuthentication = &H80070032
  364. ' Exit Function
  365. ' ElseIf aResult(0) = "Personal" Then
  366. ' ' Target machine is PER
  367. ' WScript.Echo getResource("L_OnlyIIS6Supported_ErrorMessage")
  368. ' InitAuthentication = &H80070032
  369. ' Exit Function
  370. ' Else
  371. ' ' Target machine is SRV or above
  372. ' End If
  373. ' End If
  374. ' If we get here, everything went fine.
  375. InitAuthentication = 0
  376. End Function
  377. ''''''''''''''''''''''''''''''
  378. ' ParseCmdLineOptions
  379. ''''''''''''''''''''''''''
  380. Function ParseCmdLineOptions(strCmdLine)
  381. Dim oOptions, oOption, oError
  382. Dim strItem, strValue
  383. Dim intCount, intIndex, i
  384. Dim ArgObj
  385. Dim aValues
  386. Set ArgObj = WScript.Arguments
  387. If ArgObj.Count = 0 Then Exit Function
  388. Set dictSwitches = CreateObject("Scripting.Dictionary")
  389. Set dictHelpRequested = CreateObject("Scripting.Dictionary")
  390. ReDim aNamedArguments(ArgObj.Count - 1)
  391. Set oOptions = New Options
  392. oOptions.SetOptions strCmdLine
  393. ' intCount has the number of named arguments in the command line
  394. intCount = 0
  395. ' Parse command line options
  396. For intIndex = 0 to ArgObj.Count - 1
  397. strItem = ArgObj.Item(intIndex)
  398. ' Is this a help switch?
  399. If IsHelpSwitch(strItem) Then
  400. fGlobalHelpRequested = True
  401. Exit For
  402. End If
  403. ' Is this item a switch?
  404. If (Left(strItem, 1) = "/" Or Left(strItem, 1) = "-") And Len(strItem) > 1 Then
  405. ' Check for required argument
  406. strItem = Mid(strItem, 2)
  407. ' Do we have a switch with syntax '-switch:value'?
  408. If InStr(strItem, ":") <> 0 Then
  409. Dim aSwitch
  410. aSwitch = Split(strItem, ":")
  411. strItem = aSwitch(0)
  412. strValue = aSwitch(1)
  413. Else
  414. strValue = Null
  415. End If
  416. Set oOption = oOptions.GetInfo(strItem)
  417. If Not oOption Is Nothing And fGlobalHelpRequested = False Then
  418. ' Check if we already processed this switch before
  419. If dictSwitches.Exists(oOption.Name) Then
  420. dictSwitches.Remove(oOption.Name)
  421. End If
  422. ' Option exists. Mark as visited
  423. oOption.Visit
  424. ' Check for argument requirement
  425. If IsNumeric(oOption.RequiredArgs) Then
  426. ' Is there an argument in the -switch:value,value,... format?
  427. If oOption.RequiredArgs = 0 Then
  428. ' First, look for help switch
  429. If intIndex + 1 < ArgObj.Count Then
  430. If IsHelpSwitch(ArgObj(intIndex + 1)) Then
  431. intIndex = intIndex + 1
  432. dictHelpRequested.Add oOption.Name, True
  433. End If
  434. End If
  435. ' Option does not require an argument
  436. dictSwitches.Add oOption.Name, ""
  437. Else
  438. If Not IsNull(strValue) Then
  439. ' Check how many arguments we get
  440. aValues = Split(strValue, ",")
  441. If CInt(oOption.RequiredArgs) <> (UBound(aValues) + 1) Then
  442. Set oError = New ParserError
  443. oError.SwitchName = oOption.Name
  444. oError.ErrorCode = ERROR_NOT_ENOUGH_ARGS
  445. Set ParseCmdLineOptions = oError
  446. Exit Function
  447. ' WScript.Echo "ERROR: Switch /" & oOption.Name & " expects " & _
  448. ' oOption.RequiredArgs & " arguments. Got only " & UBound(aValues)
  449. ' WScript.Quit(0)
  450. End If
  451. If InStr(strValue, ",") <> 0 Then
  452. dictSwitches.Add oOption.Name, aValues
  453. Else
  454. dictSwitches.Add oOption.Name, strValue
  455. End If
  456. Else
  457. ' We don't have '-switch:value1,value2,...'.
  458. ' Loop to get all RequiredArgs arguments asked for
  459. If oOption.RequiredArgs > 1 Then
  460. ReDim aValues(oOption.RequiredArgs - 1)
  461. For i = 0 to oOption.RequiredArgs - 1
  462. If intIndex + 1 < ArgObj.Count Then
  463. ' Get it. Add option to dictionary
  464. intIndex = intIndex + 1
  465. aValues(i) = ArgObj(intIndex)
  466. ' Is this option a help switch?
  467. If IsHelpSwitch(ArgObj(intIndex)) Then
  468. dictHelpRequested.Add oOption.Name, True
  469. ReDim Preserve aValues(UBound(aValues) - i -1)
  470. Exit For
  471. End If
  472. Else
  473. Set oError = New ParserError
  474. oError.SwitchName = oOption.Name
  475. oError.ErrorCode = ERROR_NOT_ENOUGH_ARGS
  476. Set ParseCmdLineOptions = oError
  477. Exit Function
  478. ' Wscript.Echo "ERROR: Switch /" & oOption.Name & " requires " & _
  479. ' oOption.RequiredArgs & " argument(s)"
  480. ' WScript.Quit(-1)
  481. End If
  482. Next
  483. dictSwitches.Add oOption.Name, aValues
  484. Else
  485. ' Just one argument (most common scenario)
  486. If intIndex + 1 < ArgObj.Count Then
  487. ' Get it. Add option to dictionary
  488. intIndex = intIndex + 1
  489. If IsHelpSwitch(ArgObj(intIndex)) Then
  490. dictHelpRequested.Add oOption.Name, True
  491. End If
  492. dictSwitches.Add oOption.Name, ArgObj(intIndex)
  493. Else
  494. Set oError = New ParserError
  495. oError.SwitchName = oOption.Name
  496. oError.ErrorCode = ERROR_NOT_ENOUGH_ARGS
  497. Set ParseCmdLineOptions = oError
  498. Exit Function
  499. ' Wscript.Echo "ERROR: Switch /" & oOption.Name & " requires " & _
  500. ' oOption.RequiredArgs & " argument(s)"
  501. ' WScript.Quit(-1)
  502. End If
  503. End If
  504. End If
  505. End If
  506. Else
  507. ' RequiredArgs not numeric
  508. ' We should read parameters until we find another switch
  509. If Not IsNull(strValue) Then
  510. ' Check how many arguments we get
  511. If InStr(strValue, ",") <> 0 Then
  512. aValues = Split(strValue, ",")
  513. dictSwitches.Add oOption.Name, aValues
  514. Else
  515. If IsHelpSwitch(strValue) Then
  516. dictHelpRequested.Add oOption.Name, True
  517. Else
  518. dictSwitches.Add oOption.Name, strValue
  519. End If
  520. End If
  521. Else
  522. ' We don't have '-switch:value1,value2,...'.
  523. ' Loop to get all RequiredArgs until the end of the command line arguments
  524. ' or until we find another switch
  525. i = 0
  526. intIndex = intIndex + 1
  527. ReDim aValues(ArgObj.Count - intIndex - 1)
  528. Do While intIndex < ArgObj.Count
  529. If IsHelpSwitch(ArgObj(intIndex)) Then
  530. dictHelpRequested.Add oOption.Name, True
  531. Else
  532. ' Exit if we find another switch
  533. If Left(ArgObj(intIndex), 1) = "/" Or Left(ArgObj(intIndex), 1) = "-" Then
  534. intIndex = intIndex - 1
  535. Exit Do
  536. Else
  537. aValues(i) = ArgObj(intIndex)
  538. End If
  539. End If
  540. intIndex = intIndex + 1
  541. i = i + 1
  542. Loop
  543. ReDim Preserve aValues(i - 1)
  544. dictSwitches.Add oOption.Name, aValues
  545. End If
  546. End If
  547. Else
  548. ' Item not present in the list of options
  549. Set oError = New ParserError
  550. oError.SwitchName = strItem
  551. oError.ErrorCode = ERROR_UNKNOWN_SWITCH
  552. Set ParseCmdLineOptions = oError
  553. Exit Function
  554. ' WScript.Echo "ERROR: Unknown switch: /" & strItem
  555. ' WScript.Quit(-1)
  556. End If
  557. Else
  558. ' This is not a switch (named argument)
  559. ' Add argument to the array of named arguments
  560. aNamedArguments(intCount) = strItem
  561. intCount = intCount + 1
  562. End If
  563. Next
  564. ReDim Preserve aNamedArguments(intCount - 1)
  565. ' Release Options object
  566. Set oOptions = Nothing
  567. Set ParseCmdLineOptions = Nothing
  568. End Function
  569. ''''''''''''''''''''''''''''''''''''''''''''''
  570. ' GetSwitch(switchName)
  571. ' Return the value associated with a switch
  572. ' passed in the command line
  573. '''''''''''''''''''''''''''''''''''''''''''''
  574. Function GetSwitch(strSwitchName)
  575. If IsObject(dictSwitches(strSwitchName)) Then
  576. Set GetSwitch = dictSwitches(strSwitchName)
  577. Else
  578. GetSwitch = dictSwitches(strSwitchName)
  579. End If
  580. End Function
  581. ''''''''''''''''''''''''''''''''''''''''''''''
  582. ' IsHelpRequested(switchName)
  583. ' Return if the help switch was activated for
  584. ' a certain switch
  585. '''''''''''''''''''''''''''''''''''''''''''''
  586. Function IsHelpRequested(strSwitch)
  587. Dim fHelpRequested
  588. Dim fResult
  589. fResult = False
  590. If dictHelpRequested.Exists(strSwitch) Then
  591. fResult = dictHelpRequested(strSwitch)
  592. End If
  593. IsHelpRequested = fResult
  594. End Function
  595. '''''''''''''''''''''''''''''''
  596. ' DumpCmdLineOptions()
  597. ' Show all command line options
  598. ' Used for debugging
  599. ''''''''''''''''''''''''''''''
  600. Sub DumpCmdLineOptions()
  601. Dim k
  602. Dim value
  603. If IsNull(dictSwitches) Or dictSwitches Is Nothing Then Exit Sub
  604. WScript.Echo "Switches:"
  605. For Each k in dictSwitches.Keys
  606. If IsArray(dictSwitches(k)) Then
  607. value = Join(dictSwitches(k), " and ")
  608. Else
  609. value = dictSwitches(k)
  610. End If
  611. If IsHelpRequested(k) Then
  612. WScript.Echo k & " = " & value & " (HELP switch set)"
  613. Else
  614. WScript.Echo k & " = " & value
  615. End If
  616. Next
  617. WScript.Echo
  618. WScript.Echo "Named arguments:"
  619. For k = LBound(aNamedArguments) to UBound(aNamedArguments)
  620. WScript.Echo k & ". " & aNamedArguments(k)
  621. Next
  622. End Sub
  623. '''''''''''''''''''''''''''
  624. ' CheckScriptEngine
  625. '
  626. ' This can detect the type of exe the
  627. ' script is running under and warns the
  628. ' user of the popups.
  629. '''''''''''''''''''''''''''
  630. Sub CheckScriptEngine()
  631. Dim ScriptHost
  632. Dim CurrentPathExt
  633. Dim EnvObject
  634. Dim RegCScript
  635. Dim RegPopupType ' This is used to set the pop-up box flags.
  636. RegPopupType = 32 + 4
  637. On Error Resume Next
  638. ScriptHost = WScript.FullName
  639. ScriptHost = Right(ScriptHost, Len(ScriptHost) - InStrRev(ScriptHost, "\"))
  640. If (UCase(ScriptHost) = "WSCRIPT.EXE") Then
  641. WScript.Echo getResource("L_WrongScriptProcessor_Message")
  642. ' Create a pop-up box and ask if they want to register cscript as the default host.
  643. ' -1 is the time to wait. 0 means wait forever.
  644. RegCScript = ShellObj.PopUp(getResource("L_AskChangeScriptProcessor_Message"), 0, _
  645. getResource("L_RegisterCScript_Message"), RegPopupType)
  646. If (Err.Number <> 0) Then
  647. WScript.Echo getResource("L_UseCScript_Message")
  648. WScript.Quit(Err.Number)
  649. End If
  650. ' Check to see if the user pressed yes or no. YES is 6, NO is 7
  651. If (RegCScript = 6) Then
  652. ShellObj.RegWrite "HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command\", "%WINDIR%\System32\CScript.exe //nologo ""%1"" %*", "REG_EXPAND_SZ"
  653. ShellObj.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSFile\Shell\Open\Command\", "%WINDIR%\System32\CScript.exe //nologo ""%1"" %*", "REG_EXPAND_SZ"
  654. ' Check if PathExt already existed
  655. CurrentPathExt = ShellObj.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PATHEXT")
  656. If Err.Number = &H80070002 Then
  657. Err.Clear
  658. Set EnvObject = ShellObj.Environment("PROCESS")
  659. CurrentPathExt = EnvObject.Item("PATHEXT")
  660. End If
  661. ShellObj.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PATHEXT", CurrentPathExt & ";.VBS", "REG_SZ"
  662. If (Err.Number <> 0) Then
  663. WScript.Echo getResource("L_WriteReg_ErrorMessage")
  664. WScript.Quit (Err.Number)
  665. Else
  666. WScript.Echo getResource("L_OkWriteReg_Message")
  667. End If
  668. Else
  669. WScript.Echo getResource("L_UseCScript_Message")
  670. End If
  671. Dim ProcString
  672. Dim ArgIndex
  673. Dim ArgObj
  674. Dim Result
  675. ProcString = "Cscript //nologo " & WScript.ScriptFullName
  676. Set ArgObj = WScript.Arguments
  677. For ArgIndex = 0 To ArgCount - 1
  678. ProcString = ProcString & " " & Args(ArgIndex)
  679. Next
  680. 'Now, run the original executable under CScript.exe
  681. Result = ShellObj.Run(ProcString, 0, True)
  682. WScript.Quit (Result)
  683. End If
  684. End Sub
  685. ''''''''''''''''''''''''''''''''''''''''
  686. ' FindSite
  687. '
  688. ' Return a web/ftp site paths given
  689. ' site names or site comments
  690. ''''''''''''''''''''''''''''''''''''''
  691. Function FindSite(strType, aArgs)
  692. Dim Server, Servers
  693. Dim strQuery, strSvcName, line
  694. Dim aSites, aResult, aComments
  695. Dim bFoundDuplicate, bCheckForDuplicates
  696. Dim i, j, iCount
  697. On Error Resume Next
  698. bCheckForDuplicates = False
  699. If UCase(strType) = "WEB" Then
  700. strQuery = "select Name, ServerComment from IIsWebServerSetting where "
  701. strSvcName = "W3SVC"
  702. Else
  703. strQuery = "select Name, ServerComment from IIsFtpServerSetting where "
  704. strSvcName = "MSFTPSVC"
  705. End If
  706. For i = LBound(aArgs) to UBound(aArgs)
  707. strQuery = strQuery & "(Name=""" & aArgs(i) & """ or ServerComment=""" & aArgs(i) & """)"
  708. If (i <> UBound(aArgs)) Then
  709. strQuery = strQuery & " or "
  710. End If
  711. ' Verify if we need to check for duplicate (occurs only when the user supply a site
  712. ' name instead of metabase path)
  713. ' Is this a site name?
  714. If (InStr(UCase(aArgs(i)), strSvcName) = 0) Then
  715. bCheckForDuplicates = True
  716. End If
  717. Next
  718. ' Semi-sync query. (flags = ForwardOnly Or ReturnImediately = &H30)
  719. Set Servers = ProviderObj.ExecQuery(strQuery, , &H30)
  720. If (Err.Number <> 0) Then
  721. WScript.Echo L_Query_ErrorMessage
  722. WScript.Echo getResource("L_Error_ErrorMessage") & " &H" & Hex(Err.Number) & ": " & Err.Description
  723. WScript.Quit(Err.Number)
  724. End If
  725. ReDim aResult(0)
  726. ReDim aComments(0)
  727. bFoundDuplicate = False
  728. i = 0
  729. For Each Server in Servers
  730. If Err Then
  731. Exit For
  732. End If
  733. ' Check for duplicates
  734. If bCheckForDuplicates Then
  735. For j = 0 to i - 1
  736. If (UCase(Server.ServerComment) = UCase(aComments(j))) Then
  737. If Not bFoundDuplicate Then
  738. WScript.Echo getResource("L_NotUnique1_Message")
  739. WScript.Echo getResource("L_NotUnique2_Message")
  740. WScript.Echo
  741. WScript.Echo getResource("L_SiteName_Text") & Space(20) & getResource("L_MetabasePath_Message")
  742. WScript.Echo "================================================================="
  743. WScript.Echo Server.ServerComment & Space(29 - Len(Server.ServerComment)) & aResult(j)
  744. bFoundDuplicate = True
  745. End If
  746. WScript.Echo Server.ServerComment & Space(29 - Len(Server.ServerComment)) & Server.Name
  747. Exit For
  748. End If
  749. Next
  750. End If
  751. aComments(i) = Server.ServerComment
  752. aResult(i) = Server.Name
  753. i = i + 1
  754. ReDim Preserve aComments(i)
  755. ReDim Preserve aResult(i)
  756. Next
  757. ReDim Preserve aComments(i - 1)
  758. ReDim Preserve aResult(i - 1)
  759. If bFoundDuplicate Then
  760. FindSite = ""
  761. Else
  762. FindSite = aResult
  763. End If
  764. End Function
  765. '''''''''''''''''''''''''''
  766. ' IsHelpSwitch
  767. ''''''''''''''''''''
  768. Function IsHelpSwitch(strSwitch)
  769. Dim fResult
  770. fResult = False
  771. If Left(strSwitch, 1) = "/" or Left(strSwitch, 1) = "-" Then
  772. Select Case UCase(Right(strSwitch, Len(strSwitch) - 1))
  773. Case "?"
  774. fResult = True
  775. Case "H"
  776. fResult = True
  777. Case "HELP"
  778. fResult = True
  779. Case Else
  780. fResult = False
  781. End Select
  782. End If
  783. IsHelpSwitch = fResult
  784. End Function
  785. '''''''''''''''''''''''''''
  786. ' CreateFSDir
  787. '
  788. ''''''''''''''''''''''''''
  789. Function CreateFSDir(strRoot)
  790. Dim FolderObj
  791. Dim intResult, iIndex
  792. Dim strRemotePath, strFSPath
  793. Dim strDrive, strDrvLetter
  794. 'On Error Resume Next
  795. intResult = 0
  796. If Mid(strRoot, 2, 2) <> ":\" Then
  797. ' Invalid Path - using Win32Error ERROR_INVALID_ACCESS
  798. Err.Raise &H8007000C
  799. Exit Function
  800. End If
  801. If strServer <> "." Then
  802. ' Server is remote. Find out first drive letter is available for mapping
  803. strDrive = "NO DRIVE"
  804. For strDrvLetter = Asc("C") to Asc("Z")
  805. If Not FSObj.DriveExists(Chr(strDrvLetter)) Then
  806. strDrive = Chr(strDrvLetter)
  807. Exit For
  808. End If
  809. Next
  810. If strDrive = "NO DRIVE" Then
  811. ' No drive letter available
  812. ' &H8007000F is Win32 error ERROR_INVALID_DRIVE
  813. Err.Raise &H8007000F
  814. Exit Function
  815. End If
  816. ' Look for drive specification
  817. strRemotePath = "\\" & strServer & "\" & Mid(strRoot, 1, 1) & "$"
  818. ' Map network drive
  819. strDrive = strDrive & ":"
  820. NetObj.MapNetworkDrive strDrive, strRemotePath, False, strUser, strPassword
  821. strFSPath = strDrive & Mid(strRoot, 3)
  822. Else
  823. strFSPath = strRoot
  824. End If
  825. If Not FSObj.FolderExists(strFSPath) Then
  826. 'WScript.Echo L_CreatingRootDir_Message
  827. ' Have to create path, piece by piece
  828. Dim aPathParts, strPathPart
  829. aPathParts = Split(strFSPath, "\", -1)
  830. strPathPart = aPathParts(0)
  831. iIndex = 1
  832. Do While iIndex <= UBound(aPathParts)
  833. strPathPart = strPathPart & "\" & aPathParts(iIndex)
  834. If Not FSObj.FolderExists(strPathPart) Then
  835. Set FolderObj = FSObj.CreateFolder(strPathPart)
  836. End If
  837. iIndex = iIndex + 1
  838. Loop
  839. End If
  840. If strServer <> "." Then
  841. NetObj.RemoveNetworkDrive strDrive, True
  842. End If
  843. CreateFSDir = intResult
  844. End Function
  845. '''''''''''''''''''''''''''
  846. ' ParseBindings
  847. '
  848. ' Try to get IP address, port number
  849. ' and host name from the
  850. ' ServerBindings property
  851. '''''''''''''''''''''''''''
  852. Function ParseBindings(bindings)
  853. Dim firstColon, secondColon
  854. Dim strIP, strPort, strHost
  855. firstColon = Instr(bindings, ":")
  856. secondColon = Instr(firstColon + 1, bindings, ":")
  857. strIP = Mid(bindings, 1, firstColon - 1)
  858. strPort = Mid(bindings, firstColon + 1, secondColon - firstColon - 1)
  859. strHost = Mid(bindings, secondColon + 1)
  860. ParseBindings = Array(strIP, strPort, strHost)
  861. End Function
  862. ''''''''''''''''''''''''''''''
  863. ' WMIConnect()
  864. '''''''''''''''''''''
  865. Function WMIConnect()
  866. 'On Error Resume Next
  867. If Not IsObject(LocatorObj) Then
  868. Exit Function
  869. End If
  870. WScript.StdOut.Write getResource("L_Connecting_Message")
  871. Set ProviderObj = LocatorObj.ConnectServer(strServer, getResource("WMI_NAMESPACE"), strUser, strPassword)
  872. ' If (Err.Number <> 0) Then
  873. ' 'WScript.Echo getResource("L_ConnectObject_ErrorMessage")
  874. ' 'WScript.Echo getResource("L_Error_ErrorMessage") & " &H" & Hex(Err.Number) & ": " & Err.Description
  875. ' WMIConnect = Err.Number
  876. ' 'WScript.Quit(Err.Number)
  877. ' Else
  878. ' WMIConnect = 0
  879. ' End If
  880. WScript.StdOut.WriteLine getResource("L_Done_Message")
  881. End Function
  882. '''''''''''''''''''''''''
  883. ' ValidateIPAddress
  884. ' Returns TRUE if IP Address is associated with one of the network adapters
  885. '''''''''''''''''''
  886. Function IsValidIPAddress(strIPAddress)
  887. Dim CIMv2ProviderObj, IPConfig, IPConfigSet
  888. Dim strQuery, iCounter
  889. Dim regExpObj, Matches, Match
  890. Dim bResult
  891. On Error Resume Next
  892. bResult = False
  893. ' First test the IP address against a mask
  894. Set regExpObj = New RegExp
  895. regExpObj.Pattern = "(\d+)\.(\d+)\.(\d+)\.(\d+)"
  896. regExpObj.Global = True
  897. Set Matches = regExpObj.Execute(strIPAddress)
  898. If Matches.Count <> 1 Then
  899. IsValidIPAddress = bResult
  900. Exit Function
  901. End If
  902. For Each Match in Matches(0).SubMatches
  903. If Match < 0 Or Match > 255 Then
  904. IsValidIPAddress = bResult
  905. Exit Function
  906. End If
  907. Next
  908. ' Check if IP address belongs to the target machine
  909. If Not IsObject(LocatorObj) Then
  910. IsValidIPAddress = bResult
  911. Exit Function
  912. End If
  913. Set CIMv2ProviderObj = LocatorObj.ConnectServer(strServer, "root/CIMv2", strUser, strPassword)
  914. If Err.Number Then
  915. WScript.Echo getResource("L_ConnectObject_ErrorMessage")
  916. WScript.Echo getResource("L_Error_ErrorMessage") & " &H" & Hex(Err.Number) & ": " & Err.Description
  917. 'WScript.Quit(Err.Number)
  918. End If
  919. strQuery = "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE"
  920. ' Semi-sync query. (flags = ForwardOnly Or ReturnImediately = &H30)
  921. Set IPConfigSet = CIMv2ProviderObj.ExecQuery(strQuery, , &H30)
  922. For Each IPConfig in IPConfigSet
  923. If Not IsNull(IPConfig.IPAddress) Then
  924. iCounter = LBound(IPConfig.IPAddress)
  925. Do While iCounter <= UBound(IPConfig.IPAddress)
  926. If IPConfig.IPAddress(iCounter) = strIPAddress Then
  927. bResult = True
  928. Exit For
  929. End If
  930. iCounter = iCounter + 1
  931. Loop
  932. End If
  933. Next
  934. IsValidIPAddress = bResult
  935. End Function
  936. Function IsValidPortNumber(intPort)
  937. Dim bResult
  938. bResult = False
  939. If IsNumeric(intPort) And intPort > 0 And intPort < 65535 Then
  940. bResult = True
  941. End If
  942. IsValidPortNumber = bResult
  943. End Function
  944. Function GetEnvironmentVar(strVar)
  945. GetEnvironmentVar = ShellObj.ExpandEnvironmentStrings(strVar)
  946. End Function
  947. Sub BuildNameSpace(strPath)
  948. Dim aPath
  949. Dim strNewPath, strFSPath, strVDirPath
  950. Dim strQuery
  951. Dim VDirObj, Dir, NewWebDir
  952. Dim iStart, i, iErrNumber
  953. ' Skip the *SVC/n/ROOT part
  954. iStart = InStr(InStr(strPath, "ROOT"), strPath, "/")
  955. ' If strPath is equal to *SVC/n/ROOT, there's nothing left to do.
  956. If iStart = 0 Or iStart = Len(strPath) Then
  957. Exit Sub
  958. End If
  959. ' strPath now start from the first node after ROOT in the metabase path
  960. strNewPath = Mid(strPath, iStart + 1)
  961. strVDirPath = Mid(strPath, 1, iStart - 1)
  962. ' Grab root directory for *SVC/n/ROOT
  963. Set VDirObj = ProviderObj.Get("IIsWebVirtualDirSetting='" & strVDirPath & "'")
  964. strFSPath = VDirObj.Path
  965. Set VDirObj = Nothing
  966. aPath = Split(strNewPath, "/", -1)
  967. For i = LBound(aPath) to UBound(aPath)
  968. strFSPath = strFSPath & "\" & aPath(i)
  969. Next
  970. ' First, make sure the file system path exists
  971. If Not FSObj.FolderExists(strFSPath) Then
  972. ' FS Path not found
  973. Err.Raise &H80070003 ' The system cannot find the path specified
  974. Exit Sub
  975. End If
  976. ' FS Path exists. Now let's build the web directories for each path component
  977. If strServer = "." Then
  978. strVDirPath = "IIS://" & GetEnvironmentVar("%COMPUTERNAME%") & "/" & strVDirPath
  979. Else
  980. strVDirPath = "IIS://" & strServer & "/" & strVDirPath
  981. End If
  982. On Error Resume Next
  983. ' Search for the first path component that doesn't exist.
  984. For i = LBound(aPath) to UBound(aPath)
  985. ' For each path component, check if the component exists in the metabase
  986. Set Dirs = GetObject(strVDirPath & "/" & aPath(i))
  987. If Err = &H80070003 Then
  988. Err.Clear
  989. Exit For
  990. End If
  991. strVDirPath = strVDirPath & "/" & aPath(i)
  992. Next
  993. On Error Goto 0
  994. ' Create all path components that doesn't exist
  995. For i = i to UBound(aPath)
  996. Set Dir = GetObject(strVDirPath)
  997. Set NewWebDir = Dir.Create("IIsWebDirectory", aPath(i))
  998. If Err Then
  999. iErrNumber = Err.Number
  1000. On Error Goto 0
  1001. Err.Raise iErrNumber
  1002. End If
  1003. NewWebDir.SetInfo
  1004. If Err Then
  1005. iErrNumber = Err.Number
  1006. On Error Goto 0
  1007. Err.Raise iErrNumber
  1008. End If
  1009. strVDirPath = strVDirPath & "/" & aPath(i)
  1010. Next
  1011. End Sub
  1012. Function GetAbsolutePath(strPath)
  1013. GetAbsolutePath = FSObj.GetAbsolutePathName(strPath)
  1014. End Function
  1015. Function NormalizeFilePath(strPath)
  1016. Dim strPathName
  1017. strPathName = GetAbsolutePath(strPath)
  1018. If FSObj.FolderExists(strPathName) Then
  1019. ' Should not be a folder path
  1020. Err.Raise &H80070002 ' Could not find FILE specified
  1021. End If
  1022. ' Parent folder should exist
  1023. If Not FSObj.FolderExists(FSObj.GetParentFolderName(strPathName)) Then
  1024. Err.Raise &H80070003 ' Could not find PATH specified
  1025. End If
  1026. NormalizeFilePath = strPathName
  1027. End Function
  1028. ]]>
  1029. </script>
  1030. </component>
  1031. </package>