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.

322 lines
11 KiB

  1. '
  2. ' Copyright (c) Microsoft Corporation. All rights reserved.
  3. '
  4. ' VBScript Source File
  5. '
  6. ' Script Name: IIsRepl.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_ScriptHelper_ErrorMessage = "Could not create an instance of the IIsScriptHelper object."
  16. Const L_ChkScpHelperReg_ErrorMessage = "Please register the Microsoft.IIsScriptHelper component."
  17. Const L_NotEnoughParams_ErrorMessage = "Not enough parameters."
  18. Const L_InvalidSwitch_ErrorMessage = "Invalid switch: "
  19. Const L_BackingUp_Message = "Backing up server "
  20. Const L_Restoring_Message = "Restoring on server "
  21. Const L_Shell_ErrorMessage = "Could not create an instance of the WScript.Shell object."
  22. Const L_ReturnVal_ErrorMessage = "Call returned with code "
  23. Const L_Backup_ErrorMessage = "Failure creating backup."
  24. Const L_BackupComplete_Message = "Backup complete."
  25. Const L_Restore_ErrorMessage = "Failure restoring backup."
  26. Const L_RestoreComplete_Message = "Restore complete."
  27. Const L_FS_ErrorMessage = "Could not create an instance of the Scripting.FileSystemObject object."
  28. Const L_Network_ErrorMessage = "Could not create an instance of the WScript.Network object."
  29. Const L_NoDrive_ErrorMessage = "No drives available for mapping on local machine."
  30. Const L_DriveLetter_Message = "Mapping local drive "
  31. Const L_UnMap_Message = "Unmapping local drive "
  32. Const L_ScriptComplete_Message = "IISRepl.vbs complete."
  33. Const L_SrcAdmin_Message = " to admin share on server "
  34. Const L_Copy_Message = "Copying backup files..."
  35. Const L_Copy_ErrorMessage = "Error copying files."
  36. '''''''''''''''''''''
  37. ' Help
  38. ' General help messages
  39. Const L_SeeHelp_Message = "Type IIsRepl /? for help."
  40. Const L_Help_HELP_General01_Text = "Description: Replicate IIS configuration between machines"
  41. Const L_Help_HELP_General02_Text = "Syntax: IIsRepl [/su <source user> /sp <source password> /ss <source server>]"
  42. Const L_Help_HELP_General03_Text = " /du <destination user> /dp <destination password> /ds <destination server>"
  43. Const L_Help_HELP_General04_Text = "Parameters:"
  44. Const L_Help_HELP_General06_Text = "Value Description"
  45. Const L_Help_HELP_General07_Text = "/su <source user> Source machine user name [Default: current login]"
  46. Const L_Help_HELP_General08_Text = "/sp <source password> Source machine password [Default: current login]"
  47. Const L_Help_HELP_General09_Text = "/ss <source server> Source machine [Default: this machine]"
  48. Const L_Help_HELP_General10_Text = "/du <destination user> Destination machine user name"
  49. Const L_Help_HELP_General11_Text = "/dp <destination password> Destination machine password"
  50. Const L_Help_HELP_General12_Text = "/ds <destination server> Destination machine"
  51. '
  52. ' Main block
  53. '
  54. Dim oScriptHelper, oCmdLib, oShell, oFS, oNetwork, oError
  55. Dim aArgs, arg
  56. Dim strCmdLineOptions, strDrvLetter, strSourceDrive, strSourcePath, strDestDrive
  57. Dim strSourceUser, strSourcePwd, strSourceServer, strDestUser, strDestPwd, strDestServer
  58. Dim strBackupCommand, strDestPath, strCopyCommand, strRestoreCommand,strDelCommand
  59. Dim intResult
  60. Set oShell = WScript.CreateObject("WScript.Shell")
  61. If Err.Number <> 0 Then
  62. WScript.Echo L_Shell_ErrorMessage
  63. WScript.Quit(ERR_GENERAL_FAILURE)
  64. End If
  65. Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
  66. If Err.Number <> 0 Then
  67. WScript.Echo L_FS_ErrorMessage
  68. WScript.Quit(ERR_GENERAL_FAILURE)
  69. End If
  70. Set oNetwork = WScript.CreateObject("WScript.Network")
  71. If Err.Number <> 0 Then
  72. WScript.Echo L_Network_ErrorMessage
  73. WScript.Quit(ERR_GENERAL_FAILURE)
  74. End If
  75. ' Instantiate script helper object
  76. Set oScriptHelper = CreateObject("Microsoft.IIsScriptHelper")
  77. If Err.Number <> 0 Then
  78. WScript.Echo L_ScriptHelper_ErrorMessage
  79. WScript.Echo L_ChkScpHelperReg_ErrorMessage
  80. WScript.Quit(ERR_GENERAL_FAILURE)
  81. End If
  82. Set oScriptHelper.ScriptHost = WScript
  83. ' Check if we are being run with cscript.exe instead of wscript.exe
  84. oScriptHelper.CheckScriptEngine
  85. ' Minimum number of parameters must exist
  86. If WScript.Arguments.Count < 1 Then
  87. WScript.Echo L_NotEnoughParams_ErrorMessage
  88. WScript.Echo L_SeeHelp_Message
  89. WScript.Quit(ERR_GENERAL_FAILURE)
  90. End If
  91. strCmdLineOptions = "[sourceuser:su:1;sourcepassword:sp:1;sourceserver:ss:1];" & _
  92. "[destuser:du:1;destpassword:dp:1];destserver:ds:1"
  93. Set oError = oScriptHelper.ParseCmdLineOptions(strCmdLineOptions)
  94. If Not oError Is Nothing Then
  95. If oError.ErrorCode = oScriptHelper.ERROR_NOT_ENOUGH_ARGS Then
  96. ' Not enough arguments for a specified switch
  97. WScript.Echo L_NotEnoughParams_ErrorMessage
  98. WScript.Echo L_SeeHelp_Message
  99. Else
  100. ' Invalid switch
  101. WScript.Echo L_InvalidSwitch_ErrorMessage & oError.SwitchName
  102. WScript.Echo L_SeeHelp_Message
  103. End If
  104. WScript.Quit(ERR_GENERAL_FAILURE)
  105. End If
  106. If oScriptHelper.GlobalHelpRequested Then
  107. DisplayHelpMessage
  108. WScript.Quit(ERR_OK)
  109. End If
  110. For Each arg In oScriptHelper.Switches
  111. Select Case arg
  112. Case "sourceuser"
  113. ' Source User information
  114. strSourceUser = oScriptHelper.GetSwitch(arg)
  115. Case "sourcepassword"
  116. ' Source Password information
  117. strSourcePwd = oScriptHelper.GetSwitch(arg)
  118. Case "sourceserver"
  119. ' Source Server information
  120. strSourceServer = oScriptHelper.GetSwitch(arg)
  121. Case "destuser"
  122. ' Destination User information
  123. strDestUser = oScriptHelper.GetSwitch(arg)
  124. Case "destpassword"
  125. ' Destination Password information
  126. strDestPwd = oScriptHelper.GetSwitch(arg)
  127. Case "destserver"
  128. ' Destination Server information
  129. strDestServer = oScriptHelper.GetSwitch(arg)
  130. End Select
  131. Next
  132. intResult = ERR_OK
  133. ' Do the first backup
  134. strBackupCommand = "cmd /c iisback /backup"
  135. If strSourceServer <> "" Then
  136. strBackupCommand = strBackupCommand & " /s " & strSourceServer
  137. Else
  138. strSourceServer = "127.0.0.1"
  139. End If
  140. If strSourceUser <> "" Then
  141. strBackupCommand = strBackupCommand & " /u " & strSourceUser
  142. End If
  143. If strSourcePwd <> "" Then
  144. strBackupCommand = strBackupCommand & " /p " & strSourcePwd
  145. End If
  146. ' need overwrite in case a previous attempt failed
  147. strBackupCommand = strBackupCommand & " /b iisreplback /overwrite"
  148. ' backup the source server
  149. WScript.Echo L_BackingUp_Message & strSourceServer
  150. WScript.Echo strBackupCommand
  151. intResult = oShell.Run(strBackupCommand, 1, TRUE)
  152. If intResult <> 0 Then
  153. WScript.Echo L_ReturnVal_ErrorMessage & intResult
  154. WScript.Echo L_Backup_ErrorMessage
  155. WScript.Quit(intResult)
  156. End If
  157. WScript.Echo L_BackupComplete_Message
  158. ' Now map drive to source server
  159. ' Find a drive letter
  160. strSourceDrive = "NO DRIVE"
  161. For strDrvLetter = Asc("C") to Asc("Z")
  162. If Not oFS.DriveExists(Chr(strDrvLetter)) Then
  163. strSourceDrive = Chr(strDrvLetter)
  164. Exit For
  165. End If
  166. Next
  167. If strSourceDrive = "NO DRIVE" Then
  168. ' No drive letter available
  169. WScript.Echo L_NoDrive_ErrorMessage
  170. WScript.Quit(ERR_GENERAL_FAILURE)
  171. End If
  172. strSourceDrive = strSourceDrive & ":"
  173. strSourcePath = "\\" & strSourceServer & "\ADMIN$"
  174. ' Map the drive
  175. WScript.Echo L_DriveLetter_Message & strSourceDrive & L_SrcAdmin_Message & strSourceServer
  176. If strSourceUser Then
  177. oNetwork.MapNetworkDrive strSourceDrive, strSourcePath, FALSE, strSourceUser, strSourcePwd
  178. Else
  179. oNetwork.MapNetworkDrive strSourceDrive, strSourcePath
  180. End If
  181. ' Now map drive to destination server
  182. ' Find a drive letter
  183. strDestDrive = "NO DRIVE"
  184. For strDrvLetter = Asc("C") to Asc("Z")
  185. If Not oFS.DriveExists(Chr(strDrvLetter)) Then
  186. strDestDrive = Chr(strDrvLetter)
  187. Exit For
  188. End If
  189. Next
  190. If strDestDrive = "NO DRIVE" Then
  191. ' No drive letter available
  192. WScript.Echo L_NoDrive_ErrorMessage
  193. WScript.Quit(ERR_GENERAL_FAILURE)
  194. End If
  195. strDestDrive = strDestDrive & ":"
  196. strDestPath = "\\" & strDestServer & "\ADMIN$"
  197. ' Map the drive
  198. WScript.Echo L_DriveLetter_Message & strDestDrive & L_SrcAdmin_Message & strDestServer
  199. oNetwork.MapNetworkDrive strDestDrive, strDestPath, FALSE, strDestUser, strDestPwd
  200. strCopyCommand = "cmd /c copy /Y " & strSourceDrive & "\system32\inetsrv\metaback\iisreplback.* "
  201. strCopyCommand = strCopyCommand & strDestDrive & "\system32\inetsrv\metaback"
  202. ' Copy the files
  203. WScript.Echo L_CopyMessage
  204. WScript.Echo strCopyCommand
  205. intResult = oShell.Run(strCopyCommand, 1, TRUE)
  206. If intResult <> 0 Then
  207. WScript.Echo L_ReturnVal_ErrorMessage & intResult
  208. WScript.Echo L_Copy_ErrorMessage
  209. WScript.Quit(intResult)
  210. End If
  211. strDelCommand = "cmd /c del /f /q " & strSourceDrive & "\system32\inetsrv\metaback\iisreplback.*"
  212. intResult = oShell.Run(strDelCommand, 1, TRUE)
  213. ' Unmap drive to source server
  214. WScript.Echo L_UnMap_Message & strSourceDrive
  215. oNetwork.RemoveNetworkDrive strSourceDrive
  216. ' Now do the restore on the destination server
  217. strRestoreCommand = "cmd /c iisback /restore /s " & strDestServer
  218. strRestoreCommand = strRestoreCommand & " /u " & strDestUser
  219. strRestoreCommand = strRestoreCommand & " /p " & strDestPwd
  220. strRestoreCommand = strRestoreCommand & " /b iisreplback"
  221. WScript.Echo L_Restoring_Message & strDestServer
  222. WScript.Echo strRestoreCommand
  223. intResult = oShell.Run(strRestoreCommand, 1, TRUE)
  224. If intResult <> 0 Then
  225. WScript.Echo L_ReturnVal_ErrorMessage & intResult
  226. WScript.Echo L_Restore_ErrorMessage
  227. WScript.Quit(intResult)
  228. End If
  229. WScript.Echo L_RestoreComplete_Message
  230. strDelCommand = "cmd /c del /f /q " & strDestDrive & "\system32\inetsrv\metaback\iisreplback.*"
  231. intResult = oShell.Run(strDelCommand, 1, TRUE)
  232. ' Unmap drive to destination server
  233. WScript.Echo L_UnMap_Message & strDestDrive
  234. oNetwork.RemoveNetworkDrive strDestDrive
  235. WScript.Echo L_ScriptComplete_Message
  236. ' Return value to command processor
  237. WScript.Quit(intResult)
  238. '''''''''''''''''''''''''
  239. ' End Of Main Block
  240. '''''''''''''''''''''
  241. '''''''''''''''''''''''''''
  242. ' DisplayHelpMessage
  243. '''''''''''''''''''''''''''
  244. Sub DisplayHelpMessage()
  245. WScript.Echo L_Help_HELP_General01_Text
  246. WScript.Echo
  247. WScript.Echo L_Help_HELP_General02_Text
  248. WScript.Echo L_Help_HELP_General03_Text
  249. WScript.Echo
  250. WScript.Echo L_Help_HELP_General04_Text
  251. WScript.Echo
  252. WScript.Echo L_Help_HELP_General06_Text
  253. WScript.Echo L_Help_HELP_General07_Text
  254. WScript.Echo L_Help_HELP_General08_Text
  255. WScript.Echo L_Help_HELP_General09_Text
  256. WScript.Echo L_Help_HELP_General10_Text
  257. WScript.Echo L_Help_HELP_General11_Text
  258. WScript.Echo L_Help_HELP_General12_Text
  259. End Sub