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.

505 lines
18 KiB

  1. '
  2. ' Copyright (c) Microsoft Corporation. All rights reserved.
  3. '
  4. ' VBScript Source File
  5. '
  6. ' Script Name: IIsCnfg.vbs
  7. '
  8. Option Explicit
  9. On Error Resume Next
  10. ' Error codes
  11. Const ERR_OK = 0
  12. Const ERR_GENERAL_FAILURE = 1
  13. '''''''''''''''''''''
  14. ' Messages
  15. Const L_ConfImported_Text = "Configuration imported from"
  16. Const L_ConfExported_Text = "Configuration exported from"
  17. Const L_InFile_Text = "in file"
  18. Const L_To_Text = "to"
  19. Const L_InTheMetabase_Text = "in the Metabase"
  20. Const L_Error_ErrorMessage = "Error"
  21. Const L_GetComputerObject_ErrorMessage = "Could not get computer object"
  22. Const L_Import_ErrorMessage = "Error while importing configuration."
  23. Const L_Export_ErrorMessage = "Error while exporting configuration."
  24. Const L_OnlyOneOper_ErrorMessage = "Please specify only one operation at a time."
  25. Const L_ScriptHelper_ErrorMessage = "Could not create an instance of the IIsScriptHelper object."
  26. Const L_ChkScpHelperReg_ErrorMessage = "Please register the Microsoft.IIsScriptHelper component."
  27. Const L_CmdLib_ErrorMessage = "Unable to create instance of the CmdLib object."
  28. Const L_WMIConnect_ErrorMessage = "Could not connect to WMI provider."
  29. Const L_RequiredArgsMissing_ErrorMessage = "Required arguments are missing."
  30. Const L_FileExpected_ErrorMessage = "Argument is a folder path while expecting a file path."
  31. Const L_ParentFolderDoesntExist_ErrorMessage = "Parent folder doesn't exist."
  32. Const L_FileDoesntExist_ErrorMessage = "Input file doesn't exist."
  33. Const L_FileAlreadyExist_ErrorMessage = "Export file specified already exists."
  34. Const L_NotEnoughParams_ErrorMessage = "Not enough parameters."
  35. Const L_InvalidSwitch_ErrorMessage = "Invalid switch: "
  36. '''''''''''''''''''''
  37. ' Help
  38. Const L_Empty_Text = ""
  39. ' General help messages
  40. Const L_SeeHelp_Message = "Type IIsCnfg /? for help."
  41. Const L_SeeImportHelp_Message = "Type IIsCnfg /import /? for help."
  42. Const L_SeeExportHelp_Message = "Type IIsCnfg /export /? for help."
  43. Const L_Help_HELP_General01_Text = "Description: Import and export IIS configuration."
  44. Const L_Help_HELP_General02_Text = "Syntax: IIsCnfg [/s <server> [/u <username> [/p <password>]]] /<operation>"
  45. Const L_Help_HELP_General03_Text = " [arguments]"
  46. Const L_Help_HELP_General04_Text = "Parameters:"
  47. Const L_Help_HELP_General05_Text = ""
  48. Const L_Help_HELP_General06_Text = "Value Description"
  49. Const L_Help_HELP_General07_Text = "/s <server> Connect to machine <server>. [Default: this system]"
  50. Const L_Help_HELP_General08_Text = "/u <username> Connect as <username> or <domain>\<username>."
  51. Const L_Help_HELP_General09_Text = " [Default: current user]"
  52. Const L_Help_HELP_General10_Text = "/p <password> Password for the <username> user."
  53. Const L_Help_HELP_General11_Text = "<operation> /import Import configuration from a configuration"
  54. Const L_Help_HELP_General11p1_Text = " file."
  55. Const L_Help_HELP_General12_Text = " /export Export configuration into a configuration"
  56. Const L_Help_HELP_General12p1_Text = " file."
  57. Const L_Help_HELP_General22_Text = "For detailed usage:"
  58. Const L_Help_HELP_General23_Text = "IIsCnfg /import /?"
  59. Const L_Help_HELP_General24_Text = "IIsCnfg /export /?"
  60. ' Common help messages
  61. Const L_Help_HELP_Common13_Text = "/d <DecryptPass> Specifies the password used to decrypt encrypted"
  62. Const L_Help_HELP_Common13p1_Text = " configuration data. [Default: """"]"
  63. Const L_Help_HELP_Common14_Text = "/f <File> Configuration file."
  64. Const L_Help_HELP_Common15_Text = "/sp <SourcePath> The full metabase path to start reading from the"
  65. Const L_Help_HELP_Common15p1_Text = " configuration file."
  66. Const L_Help_HELP_Common21_Text = "Examples:"
  67. ' Import help messages
  68. Const L_Help_HELP_Import01_Text = "Description: Import configuration from a configuration file."
  69. Const L_Help_HELP_Import02_Text = "Syntax: IIsCnfg [/s <server> [/u <username> [/p <password>]]] /import"
  70. Const L_Help_HELP_Import02p1_Text = " [/d <DeCryptPass>] /f <File> /sp <SourcePath> /dp <DestPath>"
  71. Const L_Help_HELP_Import02p2_Text = " [/inherited] [/children] [/merge]"
  72. Const L_Help_HELP_Import16_Text = "/dp <DestPath> The metabase path destination for imported properties."
  73. Const L_Help_HELP_Import16p1_Text = " If the keytype of the SourcePath and the DestPath"
  74. Const L_Help_HELP_Import16p2_Text = " do not match, an error occurs."
  75. Const L_Help_HELP_Import17_Text = "/inherited Import inherited settings if set."
  76. Const L_Help_HELP_Import18_Text = "/children Import configuration for child nodes."
  77. Const L_Help_HELP_Import19_Text = "/merge Merge imported configuration with existing"
  78. Const L_Help_HELP_Import19p1_Text = " configuration."
  79. Const L_Help_HELP_Import22_Text = "IIsCnfg /import /f c:\config.xml /sp /lm/w3svc/5/Root/401Kapp"
  80. Const L_Help_HELP_Import22p1_Text = " /dp /lm/w3svc/1/Root/401Kapp"
  81. ' Export help messages
  82. Const L_Help_HELP_Export01_Text = "Description: Export configuration into a configuration file."
  83. Const L_Help_HELP_Export02_Text = "Syntax: IIsCnfg [/s <server> [/u <username> [/p <password>]]] /export"
  84. Const L_Help_HELP_Export02p1_Text = " [/d <DeCryptPass>] /f <File> /sp <SourcePath> [/inherited]"
  85. 'Const L_Help_HELP_Export02p2_Text = " [/children] [/append]"
  86. Const L_Help_HELP_Export02p2_Text = " [/children]"
  87. Const L_Help_HELP_Export17_Text = "/inherited Export inherited settings if set."
  88. Const L_Help_HELP_Export18_Text = "/children Export configuration for child nodes."
  89. Const L_Help_HELP_Export22_Text = "IIsCnfg /export /f c:\config.xml /sp /lm/w3svc/5/Root/401Kapp"
  90. ''''''''''''''''''''''''
  91. ' Operation codes
  92. Const OPER_IMPORT = 1
  93. Const OPER_EXPORT = 2
  94. ' Import/Export flags
  95. Const IMPORT_EXPORT_INHERITED = 1
  96. Const IMPORT_EXPORT_NODE_ONLY = 2
  97. Const IMPORT_EXPORT_MERGE = 4
  98. Const IMPORT_EXPORT_APPEND = 4
  99. '
  100. ' Main block
  101. '
  102. Dim oScriptHelper, oCmdLib
  103. Dim strServer, strUser, strPassword, strSite
  104. Dim strFile, strDecPass, strSourcePath, strDestPath
  105. Dim intOperation, intResult, intFlags
  106. Dim aArgs, arg
  107. Dim strCmdLineOptions
  108. Dim oError
  109. ' Default values
  110. strServer = "."
  111. strUser = ""
  112. strPassword = ""
  113. intOperation = 0
  114. strFile = ""
  115. strDecPass = ""
  116. strSourcePath = ""
  117. strDestPath = ""
  118. intFlags = IMPORT_EXPORT_NODE_ONLY
  119. ' Instantiate script helper object
  120. Set oScriptHelper = CreateObject("Microsoft.IIsScriptHelper")
  121. If Err.Number <> 0 Then
  122. WScript.Echo L_ScriptHelper_ErrorMessage
  123. WScript.Echo L_ChkScpHelperReg_ErrorMessage
  124. WScript.Quit(ERR_GENERAL_FAILURE)
  125. End If
  126. Set oScriptHelper.ScriptHost = WScript
  127. ' Check if we are being run with cscript.exe instead of wscript.exe
  128. oScriptHelper.CheckScriptEngine
  129. ' Minimum number of parameters must exist
  130. If WScript.Arguments.Count < 1 Then
  131. WScript.Echo L_RequiredArgsMissing_ErrorMessage
  132. WScript.Echo L_SeeHelp_Message
  133. WScript.Quit(ERR_GENERAL_FAILURE)
  134. End If
  135. strCmdLineOptions = "[server:s:1;user:u:1;password:p:1];decpass:d:1;file:f:1;sourcepath:sp:1;" & _
  136. "inherited:i:0;children:c:0;[import::0;destpath:dp:1;merge:m:0];" & _
  137. "export::0"
  138. Set oError = oScriptHelper.ParseCmdLineOptions(strCmdLineOptions)
  139. If Not oError Is Nothing Then
  140. If oError.ErrorCode = oScriptHelper.ERROR_NOT_ENOUGH_ARGS Then
  141. ' Not enough arguments for a specified switch
  142. WScript.Echo L_NotEnoughParams_ErrorMessage
  143. WScript.Echo L_SeeHelp_Message
  144. Else
  145. ' Invalid switch
  146. WScript.Echo L_InvalidSwitch_ErrorMessage & oError.SwitchName
  147. WScript.Echo L_SeeHelp_Message
  148. End If
  149. WScript.Quit(ERR_GENERAL_FAILURE)
  150. End If
  151. If oScriptHelper.GlobalHelpRequested Then
  152. DisplayHelpMessage
  153. WScript.Quit(ERR_OK)
  154. End If
  155. For Each arg In oScriptHelper.Switches
  156. Select Case arg
  157. Case "server"
  158. ' Server information
  159. strServer = oScriptHelper.GetSwitch(arg)
  160. Case "user"
  161. ' User information
  162. strUser = oScriptHelper.GetSwitch(arg)
  163. Case "password"
  164. ' Password information
  165. strPassword = oScriptHelper.GetSwitch(arg)
  166. Case "import"
  167. If (intOperation <> 0) Then
  168. WScript.Echo L_OnlyOneOper_ErrorMessage
  169. WScript.Echo L_SeeHelp_Message
  170. WScript.Quit(ERR_GENERAL_FAILURE)
  171. End If
  172. intOperation = OPER_IMPORT
  173. If oScriptHelper.IsHelpRequested(arg) Then
  174. DisplayImportHelpMessage
  175. WScript.Quit(ERR_OK)
  176. End If
  177. Case "export"
  178. If (intOperation <> 0) Then
  179. WScript.Echo L_OnlyOneOper_ErrorMessage
  180. WScript.Echo L_SeeHelp_Message
  181. WScript.Quit(ERR_GENERAL_FAILURE)
  182. End If
  183. intOperation = OPER_EXPORT
  184. If oScriptHelper.IsHelpRequested(arg) Then
  185. DisplayExportHelpMessage
  186. WScript.Quit(ERR_OK)
  187. End If
  188. Case "file"
  189. strFile = oScriptHelper.GetSwitch(arg)
  190. Case "decpass"
  191. strDecPass = oScriptHelper.GetSwitch(arg)
  192. Case "sourcepath"
  193. strSourcePath = oScriptHelper.GetSwitch(arg)
  194. Case "destpath"
  195. strDestPath = oScriptHelper.GetSwitch(arg)
  196. Case "inherited"
  197. intFlags = intFlags Or IMPORT_EXPORT_INHERITED
  198. Case "children"
  199. intFlags = intFlags And Not IMPORT_EXPORT_NODE_ONLY
  200. Case "merge"
  201. intFlags = intFlags Or IMPORT_EXPORT_MERGE
  202. Case "append"
  203. intFlags = intFlags Or IMPORT_EXPORT_APPEND
  204. End Select
  205. Next
  206. ' Check Parameters
  207. If intOperation = 0 Then
  208. WScript.Echo L_OperationRequired_ErrorMessage
  209. WScript.Echo L_SeeHelp_Message
  210. WScript.Quit(ERR_GENERAL_FAILURE)
  211. End If
  212. If strFile = "" Or strSourcePath = "" Or (intOperation = OPER_IMPORT And strDestPath = "") Then
  213. WScript.Echo L_RequiredArgsMissing_ErrorMessage
  214. If intOperation = OPER_IMPORT Then
  215. WScript.Echo L_SeeImportHelp_Message
  216. Else
  217. WScript.Echo L_SeeExportHelp_Message
  218. End If
  219. WScript.Quit(ERR_GENERAL_FAILURE)
  220. End If
  221. ' Check if /p is specified but /u isn't. In this case, we should bail out with an error
  222. If oScriptHelper.Switches.Exists("password") And Not oScriptHelper.Switches.Exists("user") Then
  223. WScript.Echo L_PassWithoutUser_ErrorMessage
  224. WScript.Quit(ERR_GENERAL_FAILURE)
  225. End If
  226. ' Check if /u is specified but /p isn't. In this case, we should ask for a password
  227. If oScriptHelper.Switches.Exists("user") And Not oScriptHelper.Switches.Exists("password") Then
  228. Set oCmdLib = CreateObject("Microsoft.CmdLib")
  229. Set oCmdLib.ScriptingHost = WScript.Application
  230. If Err.Number Then
  231. WScript.Echo L_CmdLib_ErrorMessage
  232. WScript.Quit(Err.Number)
  233. Else
  234. strPassword = oCmdLib.GetPassword
  235. Set oCmdLib = Nothing
  236. End If
  237. End If
  238. ' Initializes authentication with remote machine
  239. intResult = oScriptHelper.InitAuthentication(strServer, strUser, strPassword)
  240. If intResult <> 0 Then
  241. WScript.Quit(intResult)
  242. End If
  243. ' Choose operation
  244. Select Case intOperation
  245. Case OPER_IMPORT
  246. intResult = Import(strPassword, strFile, strSourcePath, strDestPath, intFlags)
  247. Case OPER_EXPORT
  248. intResult = Export(strPassword, strFile, strSourcePath, intFlags)
  249. End Select
  250. ' Return value to command processor
  251. WScript.Quit(intResult)
  252. '''''''''''''''''''''''''
  253. ' End Of Main Block
  254. '''''''''''''''''''''
  255. '''''''''''''''''''''''''''
  256. ' DisplayHelpMessage
  257. '''''''''''''''''''''''''''
  258. Sub DisplayHelpMessage()
  259. WScript.Echo L_Help_HELP_General01_Text
  260. WScript.Echo L_Empty_Text
  261. WScript.Echo L_Help_HELP_General02_Text
  262. WScript.Echo L_Help_HELP_General03_Text
  263. WScript.Echo L_Empty_Text
  264. WScript.Echo L_Help_HELP_General04_Text
  265. WScript.Echo L_Help_HELP_General05_Text
  266. WScript.Echo L_Help_HELP_General06_Text
  267. WScript.Echo L_Help_HELP_General07_Text
  268. WScript.Echo L_Help_HELP_General08_Text
  269. WScript.Echo L_Help_HELP_General09_Text
  270. WScript.Echo L_Help_HELP_General10_Text
  271. WScript.Echo L_Help_HELP_General11_Text
  272. WScript.Echo L_Help_HELP_General11p1_Text
  273. WScript.Echo L_Help_HELP_General12_Text
  274. WScript.Echo L_Help_HELP_General12p1_Text
  275. WScript.Echo L_Empty_Text
  276. WScript.Echo L_Help_HELP_General22_Text
  277. WScript.Echo L_Empty_Text
  278. WScript.Echo L_Help_HELP_General23_Text
  279. WScript.Echo L_Help_HELP_General24_Text
  280. End Sub
  281. Sub DisplayImportHelpMessage()
  282. WScript.Echo L_Help_HELP_Import01_Text
  283. WScript.Echo L_Empty_Text
  284. WScript.Echo L_Help_HELP_Import02_Text
  285. WScript.Echo L_Help_HELP_Import02p1_Text
  286. WScript.Echo L_Help_HELP_Import02p2_Text
  287. WScript.Echo L_Empty_Text
  288. WScript.Echo L_Help_HELP_General06_Text
  289. WScript.Echo L_Help_HELP_General07_Text
  290. WScript.Echo L_Help_HELP_General08_Text
  291. WScript.Echo L_Help_HELP_General09_Text
  292. WScript.Echo L_Help_HELP_General10_Text
  293. WScript.Echo L_Help_HELP_Common13_Text
  294. WScript.Echo L_Help_HELP_Common13p1_Text
  295. WScript.Echo L_Help_HELP_Common14_Text
  296. WScript.Echo L_Help_HELP_Common15_Text
  297. WScript.Echo L_Help_HELP_Common15p1_Text
  298. WScript.Echo L_Help_HELP_Import16_Text
  299. WScript.Echo L_Help_HELP_Import16p1_Text
  300. WScript.Echo L_Help_HELP_Import16p2_Text
  301. WScript.Echo L_Help_HELP_Import17_Text
  302. WScript.Echo L_Help_HELP_Import18_Text
  303. WScript.Echo L_Help_HELP_Import19_Text
  304. WScript.Echo L_Help_HELP_Import19p1_Text
  305. WScript.Echo L_Empty_Text
  306. WScript.Echo L_Help_HELP_Common21_Text
  307. WScript.Echo L_Empty_Text
  308. WScript.Echo L_Help_HELP_Import22_Text
  309. WScript.Echo L_Help_HELP_Import22p1_Text
  310. End Sub
  311. Sub DisplayExportHelpMessage()
  312. WScript.Echo L_Help_HELP_Export01_Text
  313. WScript.Echo L_Empty_Text
  314. WScript.Echo L_Help_HELP_Export02_Text
  315. WScript.Echo L_Help_HELP_Export02p1_Text
  316. WScript.Echo L_Help_HELP_Export02p2_Text
  317. WScript.Echo L_Empty_Text
  318. WScript.Echo L_Help_HELP_General06_Text
  319. WScript.Echo L_Help_HELP_General07_Text
  320. WScript.Echo L_Help_HELP_General08_Text
  321. WScript.Echo L_Help_HELP_General09_Text
  322. WScript.Echo L_Help_HELP_General10_Text
  323. WScript.Echo L_Help_HELP_Common13_Text
  324. WScript.Echo L_Help_HELP_Common13p1_Text
  325. WScript.Echo L_Help_HELP_Common14_Text
  326. WScript.Echo L_Help_HELP_Common15_Text
  327. WScript.Echo L_Help_HELP_Common15p1_Text
  328. WScript.Echo L_Help_HELP_Export17_Text
  329. WScript.Echo L_Help_HELP_Export18_Text
  330. WScript.Echo L_Empty_Text
  331. WScript.Echo L_Help_HELP_Common21_Text
  332. WScript.Echo L_Empty_Text
  333. WScript.Echo L_Help_HELP_Export22_Text
  334. End Sub
  335. '''''''''''''''''''''''''''
  336. ' Import Function
  337. '''''''''''''''''''''''''''
  338. Function Import(strPassword, strFile, strSourcePath, strDestPath, intFlags)
  339. Dim ComputerObj
  340. Dim strFilePath
  341. On Error Resume Next
  342. ' Normalize path first
  343. strFilePath = oScriptHelper.NormalizeFilePath(strFile)
  344. If Err Then
  345. Select Case Err.Number
  346. Case &H80070002
  347. WScript.Echo L_FileExpected_ErrorMessage
  348. Case &H80070003
  349. WScript.Echo L_ParentFolderDoesntExist_ErrorMessage
  350. End Select
  351. Import = Err.Number
  352. Exit Function
  353. End If
  354. If Not oScriptHelper.FSObj.FileExists(strFilePath) Then
  355. WScript.Echo L_FileDoesntExist_ErrorMessage
  356. Import = &H80070002
  357. Exit Function
  358. End If
  359. oScriptHelper.WMIConnect
  360. If Err.Number Then
  361. WScript.Echo L_WMIConnect_ErrorMessage
  362. WScript.Echo L_Error_ErrorMessage & " &H" & Hex(Err.Number) & ": " & Err.Description
  363. Import = Err.Number
  364. Exit Function
  365. End If
  366. Set ComputerObj = oScriptHelper.ProviderObj.get("IIsComputer='LM'")
  367. If Err.Number Then
  368. WScript.Echo L_GetComputerObj_ErrorMessage
  369. WScript.Echo L_Error_ErrorMessage & " &H" & Hex(Err.Number) & ": " & Err.Description
  370. Import = Err.Number
  371. Exit Function
  372. End If
  373. ' Call Import method from the computer object
  374. ComputerObj.Import strPassword, strFilePath, strSourcePath, strDestPath, intFlags
  375. If Err.Number Then
  376. WScript.Echo L_Import_ErrorMessage
  377. WScript.Echo L_Error_ErrorMessage & " &H" & Hex(Err.Number) & ": " & Err.Description
  378. Import = Err.Number
  379. Exit Function
  380. End If
  381. WScript.Echo L_ConfImported_Text & " " & strSourcePath & " " & L_InFile_Text & " " & _
  382. strFile & " " & L_To_Text & " " & strDestPath & " " & L_InTheMetabase_Text & "."
  383. End Function
  384. '''''''''''''''''''''''''''
  385. ' Export Function
  386. '''''''''''''''''''''''''''
  387. Function Export(strPassword, strFile, strSourcePath, intFlags)
  388. Dim ComputerObj
  389. Dim strFilePath
  390. On Error Resume Next
  391. ' Normalize path first
  392. strFilePath = oScriptHelper.NormalizeFilePath(strFile)
  393. If Err Then
  394. Select Case Err.Number
  395. Case &H80070002
  396. WScript.Echo L_FileExpected_ErrorMessage
  397. Case &H80070003
  398. WScript.Echo L_ParentFolderDoesntExist_ErrorMessage
  399. End Select
  400. Export = Err.Number
  401. Exit Function
  402. End If
  403. If oScriptHelper.FSObj.FileExists(strFilePath) Then
  404. WScript.Echo L_FileAlreadyExist_ErrorMessage
  405. Export = &H80070050
  406. Exit Function
  407. End If
  408. oScriptHelper.WMIConnect
  409. If Err.Number Then
  410. WScript.Echo L_WMIConnect_ErrorMessage
  411. WScript.Echo L_Error_ErrorMessage & " &H" & Hex(Err.Number) & ": " & Err.Description
  412. Export = Err.Number
  413. Exit Function
  414. End If
  415. Set ComputerObj = oScriptHelper.ProviderObj.get("IIsComputer='LM'")
  416. If Err.Number Then
  417. WScript.Echo L_GetComputerObj_ErrorMessage
  418. WScript.Echo L_Error_ErrorMessage & " &H" & Hex(Err.Number) & ": " & Err.Description
  419. Export = Err.Number
  420. Exit Function
  421. End If
  422. ' Call Import method from the computer object
  423. ComputerObj.Export strPassword, strFilePath, strSourcePath, intFlags
  424. If Err.Number Then
  425. WScript.Echo L_Export_ErrorMessage
  426. WScript.Echo L_Error_ErrorMessage & " &H" & Hex(Err.Number) & ": " & Err.Description
  427. Export = Err.Number
  428. Exit Function
  429. End If
  430. WScript.Echo L_ConfExported_Text & " " & strSourcePath & " " & L_To_Text & " " & strFile & "."
  431. End Function