Leaked source code of windows server 2003
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.

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