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.

369 lines
16 KiB

  1. OPTION EXPLICIT
  2. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  3. '-----Windows Script Host script to generate components needed to run WSH
  4. '-----under Windows PE.
  5. '-----Copyright 2001, Microsoft Corporation
  6. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  7. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  8. '-----DIM, DEFINE VARIABLES, SET SOME GLOBAL OBJECTS
  9. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  10. DIM strCmdArg, strCmdSwitch, arg, strCmdArray, CDDriveCollection, CDDrive, CDSource, FSO, Folder, HDDColl
  11. DIM HDD, FirstHDD, strAppend, WSHShell, strDesktop, strOptDest, strDestFolder
  12. DIM FILE, strCMDExpand, strCMDMid, strJobTitle, strNeedCD, iAmPlatform, iArchDir
  13. DIM iAmQuiet,iHaveSource, iHaveDest, iWillBrowse, WshSysEnv, strOSVer, strWantToView, strFolderName, intOneMore
  14. DIM strCMDado, strCMDmsadc, strCMDOle_db, strIDir, strSysDir
  15. Const ForAppending = 8
  16. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  17. '-----OFFER/TAKE CMDLINE PARAMETERS
  18. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  19. If WScript.Arguments.Count <> 0 Then
  20. For each arg in WScript.Arguments
  21. strCmdArg = (arg)
  22. strCmdArray = Split(strCmdArg, ":", 2, 1)
  23. IF lcase(strCmdArray(0)) = "/s" or lcase(strCmdArray(0)) = "-s" THEN
  24. iHaveSource = 1
  25. CDSource = TRIM(strCmdArray(1))
  26. END IF
  27. IF lcase(strCmdArray(0)) = "/d" or lcase(strCmdArray(0)) = "-d" THEN
  28. iHaveDest = 1
  29. strOptDest = TRIM(strCmdArray(1))
  30. END IF
  31. IF lcase(strCmdArray(0)) = "/?" OR lcase(strCmdArray(0)) = "-?" THEN
  32. MsgBox "The following command-line arguments are accepted by this script:"&vbCRLF&vbCRLF&_
  33. """/s:filepath"" - alternate source location other than the CD-ROM drive."&vbCRLF&vbCRLF&"Examples:"&vbCRLF&_
  34. "/S:C:\"&vbCRLF&_
  35. "-s:Z:\"&vbCRLF&_
  36. "or"&vbCRLF&_
  37. "-S:\\Myserver\Myshare"&vbCRLF&vbCRLF&_
  38. "The script will still attempt to verify the presence of Windows XP files."&vbCrLF&vbCrLF&_
  39. "/D - Destination. Opposite of CD - specifies build destination. Otherwise placed on desktop."&vbCRLF&vbCRLF&_
  40. "/64 - build for Itanium. Generates scripts for Windows on the Itanium Processor Family."&vbCRLF&vbCRLF&_
  41. "/Q - run without any dialog. This will not confirm success, will notify on failure."&vbCRLF&vbCRLF&_
  42. "/E - explore completed files. Navigate to the created files when completed.", vbInformation, "Command-line arguments"
  43. WScript.Quit
  44. END IF
  45. IF lcase(strCmdArray(0)) = "/64" OR lcase(strCmdArray(0)) = "-64" THEN
  46. iAmPlatform = "Itanium"
  47. END IF
  48. IF lcase(strCmdArray(0)) = "/q" OR lcase(strCmdArray(0)) = "-q" THEN
  49. iAmQuiet = 1
  50. END IF
  51. IF lcase(strCmdArray(0)) = "/e" OR lcase(strCmdArray(0)) = "-e" THEN
  52. iWillBrowse = 1
  53. END IF
  54. Next
  55. ELSE
  56. iHaveSource = 0
  57. END IF
  58. IF strOptDest = "" THEN
  59. iHaveDest = 0
  60. ELSEIF INSTR(UCASE(strOptDest), "I386\") <> 0 OR INSTR(UCASE(strOptDest), "IA64\") <> 0 OR INSTR(UCASE(strOptDest), "SYSTEM32") <> 0 THEN
  61. MsgBox "The destination path needs to be the root of your newly created WinPE install - remove any extraneous path information, such as ""I386"" or ""System32""", vbCritical, "Destination Path Incorrect"
  62. WScript.Quit
  63. END IF
  64. IF iAmQuiet <> 1 THEN
  65. iAmQuiet = 0
  66. END IF
  67. IF iAmPlatform <> "Itanium" THEN
  68. iAmPlatform = "x86"
  69. END IF
  70. IF Right(strOptDest, 1) = "\" THEN
  71. strOptDest = Left(strOptDest, LEN(strOptDest)-1)
  72. END IF
  73. IF Right(CDSource, 1) = "\" THEN
  74. CDSource = Left(CDSource, LEN(CDSource)-1)
  75. END IF
  76. IF iAmPlatform = "Itanium" THEN
  77. iArchDir = "ia64"
  78. ELSEIF iAmPlatform = "x86" THEN
  79. iArchDir = "i386"
  80. END IF
  81. strJobTitle = "WSH Component Generation"
  82. SET WshShell = WScript.CreateObject("WScript.Shell")
  83. SET WshSysEnv = WshShell.Environment("SYSTEM")
  84. SET FSO = CreateObject("Scripting.FileSystemObject")
  85. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  86. '-----ERROR OUT IF NOT RUNNING ON Windows 2000 OR HIGHER
  87. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  88. strOSVer = WshSysEnv("OS")
  89. IF strOSVer <> "Windows_NT" THEN
  90. MsgBox "This script must be run on Windows 2000 or Windows XP", vbCritical, "Incorrect Windows Version"
  91. WScript.Quit
  92. END IF
  93. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  94. '-----GENERATE COLLECTION OF CD-ROM DRIVES VIA WMI. PICK FIRST AVAILABLE
  95. '-----ERROR OUT IF NO DRIVES FOUND
  96. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  97. IF iHaveSource = 0 THEN
  98. SET CDDriveCollection = GetObject("winmgmts:").ExecQuery("SELECT * FROM Win32_CDROMDrive")
  99. IF CDDriveCollection.Count <= 0 THEN
  100. MsgBox "No CD-ROM drives found. Exiting Script.", vbCritical, "No CD-ROM drive found"
  101. WScript.Quit
  102. END IF
  103. FOR EACH CDDrive IN CDDriveCollection
  104. CDSource = CDDrive.Drive(0)
  105. EXIT FOR
  106. NEXT
  107. END IF
  108. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  109. '-----PROMPT FOR WINDOWS CD - QUIT IF CANCELLED
  110. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  111. IF iAmQuiet = 0 THEN
  112. strNeedCD = MsgBox("This script will place a folder on your desktop containing all necessary files needed to "&_
  113. "install WSH (Windows Script Host) support under Windows PE."&vbCrLF&vbCrLF&"Please ensure that your "&_
  114. "Windows XP Professional CD or Windows XP Professional binaries are available now on: "&vbCrLF&CDSource&vbCrLF&vbCrLF&"This script is only designed to be used with Windows PE/Windows XP RC1 "&_
  115. "or newer.", 65, strJobTitle)
  116. END IF
  117. IF strNeedCD = 2 THEN
  118. WScript.Quit
  119. END IF
  120. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  121. '-----TEST VIA WMI TO INSURE MEDIA IS PRESENT AND READABLE
  122. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  123. IF iHaveSource = 0 THEN
  124. TestForMedia()
  125. END IF
  126. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  127. '-----TESTS FOR EXISTANCE OF SEVERAL KEY FILES, AND A FILE COUNT IN I386 or IA64 TO INSURE
  128. '-----WINDOWS XP PRO MEDIA
  129. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  130. Validate(iArchDir)
  131. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  132. '-----FIND THE USER'S DESKTOP. PUT NEW FOLDER THERE. APPEND TIMESTAMP IF THE FOLDER
  133. '-----ALREADY EXISTS.
  134. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  135. strDesktop = WshShell.SpecialFolders("Desktop")
  136. strFolderName = "WSH Build Files ("&iArchDir&")"
  137. IF iHaveDest = 0 THEN
  138. strDestFolder = strDesktop&"\"&strFolderName
  139. IF FSO.FolderExists(strDestFolder) THEN
  140. GetUnique()
  141. strDestFolder = strDestFolder&strAppend
  142. strFolderName = strFolderName&strAppend
  143. END IF
  144. FSO.CreateFolder(strDestFolder)
  145. ELSE
  146. strDestFolder = strOptDest
  147. IF NOT FSO.FolderExists(strDestFolder) THEN
  148. FSO.CreateFolder(strDestFolder)
  149. END IF
  150. END IF
  151. IF iHaveDest = 1 THEN
  152. strIDir = strDestFolder&"\"&iArchDir
  153. strSysDir = strDestFolder&"\"&iArchDir&"\System32"
  154. strDestFolder = strSysDir
  155. END IF
  156. IF FSO.FileExists(strDestFolder&"\autoexec.cmd") THEN
  157. SET FILE = FSO.OpenTextFile(strDestFolder&"\autoexec.cmd", ForAppending, true)
  158. FILE.WriteLine("call WSH.bat")
  159. FILE.Close()
  160. END IF
  161. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  162. '-----SET COMMON VARIABLES SO THE STRINGS AREN'T SO LARGE
  163. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  164. strCMDExpand = "EXPAND """&CDSource&"\"&iArchDir&"\"
  165. strCMDMid = """ """&strDestFolder&"\"
  166. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  167. '-----SHELL OUT THE EXPANSION (or COPY) OF ALL NEEDED FILES.
  168. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  169. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  170. '-----SHELL OUT THE COPYING OF wsh.inf. SOMETIMES THIS IS COMPRESSED, SOMETIMES IT ISN'T
  171. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  172. IF FSO.FileExists(CDSource&"\"&iArchDir&"\wsh.inf") THEN
  173. FSO.CopyFile CDSource&"\"&iArchDir&"\wsh.inf", strDestFolder&"\"
  174. WshShell.Run "attrib -R """&strDestFolder&"\wsh.inf""", 0, FALSE
  175. ELSE
  176. WshShell.Run strCMDExpand&"wsh.in_"&strCMDMid&"wsh.inf""", 0, FALSE
  177. END IF
  178. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  179. '-----SHELL OUT THE EXPANSION OF core WSH Files. (EXE, DLL, OCX)
  180. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  181. WshShell.Run strCMDExpand&"wscript.ex_"&strCMDMid&"wscript.exe""", 0, FALSE
  182. WshShell.Run strCMDExpand&"cscript.ex_"&strCMDMid&"cscript.exe""", 0, FALSE
  183. WshShell.Run strCMDExpand&"jscript.dl_"&strCMDMid&"jscript.dll""", 0, FALSE
  184. WshShell.Run strCMDExpand&"scrobj.dl_"&strCMDMid&"scrobj.dll""", 0, FALSE
  185. WshShell.Run strCMDExpand&"scrrun.dl_"&strCMDMid&"scrrun.dll""", 0, FALSE
  186. WshShell.Run strCMDExpand&"vbscript.dl_"&strCMDMid&"vbscript.dll""", 0, FALSE
  187. WshShell.Run strCMDExpand&"wshext.dl_"&strCMDMid&"wshext.dll""", 0, FALSE
  188. WshShell.Run strCMDExpand&"wshom.oc_"&strCMDMid&"wshom.ocx""", 0, FALSE
  189. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  190. '-----CREATE THE SAMPLE WSH SCRIPT .
  191. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  192. SET FILE = fso.CreateTextFile(strDestFolder&"\test.vbs", True)
  193. FILE.WriteLine("MsgBox ""Welcome to Windows PE.""&vbCrLF&vbCrLF&""WSH support is functioning."", vbInformation, ""WSH support is functioning.""")
  194. FILE.Close
  195. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  196. '-----CREATE the BATS THAT WILL INSTALL THE WSH ENVIRONMENT.
  197. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  198. SET FILE = fso.CreateTextFile(strDestFolder&"\WSH.bat", True)
  199. FILE.WriteLine ("@ECHO OFF")
  200. FILE.WriteLine ("START ""Installing WSH"" /MIN WSH2.bat")
  201. FILE.Close
  202. SET FILE = fso.CreateTextFile(strDestFolder&"\WSH2.bat", True)
  203. FILE.WriteLine ("REM - This section initializes WSH")
  204. FILE.WriteLine ("")
  205. FILE.WriteLine ("REM - INSTALL WSH COMPONENTS")
  206. FILE.WriteLine ("Regsvr32 %SystemRoot%\System32\jscript.dll /s")
  207. FILE.WriteLine ("Regsvr32 %SystemRoot%\System32\scrobj.dll /s")
  208. FILE.WriteLine ("Regsvr32 %SystemRoot%\System32\scrrun.dll /s")
  209. FILE.WriteLine ("Regsvr32 %SystemRoot%\System32\vbscript.dll /s")
  210. FILE.WriteLine ("Regsvr32 %SystemRoot%\System32\wshext.dll /s")
  211. FILE.WriteLine ("Regsvr32 %SystemRoot%\System32\wshom.ocx /s")
  212. FILE.WriteLine ("")
  213. FILE.WriteLine ("REM - INSTALL FILE ASSOCIATIONS FOR WSH")
  214. FILE.WriteLine ("%SystemRoot%\System32\rundll32.exe setupapi,InstallHinfSection DefaultInstall 132 WSH.inf")
  215. FILE.WriteLine ("EXIT")
  216. FILE.Close
  217. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  218. '-----FILES READY - ASK IF THE USER WANTS TO EXPLORE TO THEM.
  219. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  220. IF iAmQuiet = 0 THEN
  221. strWantToView = MsgBox("This script has successfully retrieved all necessary files needed to "&_
  222. "install WSH on Windows PE. The files have been placed on your desktop in a directory named """&strFolderName&"""."&vbCrLF&vbCrLF&_
  223. "In order to install the WSH components within Windows PE, place the contents of this folder (not the folder itself) into the I386\System32 or IA64\System32 directory "&_
  224. "of your Windows PE CD, Hard drive install, or RIS Server installation, and modify your startnet.cmd to run the file ""WSH.bat"" (without quotes)."&vbCrLF&vbCrLF&_
  225. "A sample script named test.vbs that you can use to verify installation has been provided as well. You can remove this for your production version of Windows PE."&vbCrLF&vbCrLF&_
  226. "Would you like to open this folder now?", 36, strJobTitle)
  227. END IF
  228. IF strWantToView = 6 OR iWillBrowse = 1 THEN
  229. WshShell.Run("Explorer "&strDestFolder)
  230. END IF
  231. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  232. '-----WMI TEST OF CD LOADED AND CD READ INTEGRITY.
  233. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  234. SUB TestForMedia()
  235. IF CDDrive.MediaLoaded(0) = FALSE THEN
  236. MsgBox "Please place the Windows XP Professional CD in drive "&CDSource&" before continuing.", vbCritical, "No CD in drive "&CDSource&""
  237. WScript.Quit
  238. ELSE
  239. IF CDDrive.DriveIntegrity(0) = FALSE THEN
  240. MsgBox "Could not read files from the CD in drive "&CDSource&".", vbCritical, "CD in drive "&CDSource&" is unreadable."
  241. WScript.Quit
  242. END IF
  243. END IF
  244. END SUB
  245. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  246. '-----FSO TEST TO SEE IF THE CMDLINE PROVIDED FOLDER EXISTS.
  247. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  248. FUNCTION TestForFolder(a)
  249. IF NOT FSO.FolderExists(a) THEN
  250. FailOut()
  251. END IF
  252. END FUNCTION
  253. SUB Validate(a)
  254. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  255. '-----TEST FOR THE EXISTANCE OF A FOLDER OR FILE, OR THE NONEXISTANCE OF A FILE.
  256. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  257. TestForFolder(CDSource&"\"&a&"")
  258. TestForFolder(CDSource&"\DOCS")
  259. TestForFolder(CDSource&"\SUPPORT")
  260. TestForFolder(CDSource&"\VALUEADD")
  261. TestForFile(CDSource&"\"&a&"\System32\smss.exe")
  262. TestForFile(CDSource&"\"&a&"\System32\ntdll.dll")
  263. TestForFile(CDSource&"\"&a&"\winnt32.exe")
  264. TestForFile(CDSource&"\setup.exe")
  265. TestForANDFile CDSource&"\WIN51.B2", CDSource&"\WIN51.RC1", CDSource&"\WIN51.RC1"
  266. TestForANDFile CDSource&"\WIN51IP.B2", CDSource&"\WIN51IP.RC1", CDSource&"\WIN51MP.RC1"
  267. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  268. '-----TEST TO INSURE THAT THEY AREN'T TRYING TO INSTALL FROM Windows PE CD ITSELF
  269. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  270. Set Folder = FSO.GetFolder(CDSource&"\"&a&"\System32")
  271. IF Folder.Files.Count > 10 THEN
  272. FailOut()
  273. END IF
  274. END SUB
  275. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  276. '-----TEST FOR THE EXISTANCE OF A FOLDER OR FILE, OR THE NONEXISTANCE OF A FILE.
  277. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  278. FUNCTION TestForFolder(a)
  279. IF NOT FSO.FolderExists(a) THEN
  280. FailOut()
  281. END IF
  282. END FUNCTION
  283. FUNCTION TestForFile(a)
  284. IF NOT FSO.FileExists(a) THEN
  285. FailOut()
  286. END IF
  287. END FUNCTION
  288. FUNCTION TestForANDFile(a,b,c)
  289. IF NOT FSO.FileExists(a) AND NOT FSO.FileExists(b) AND NOT FSO.FileExists(c) THEN
  290. FailOut()
  291. END IF
  292. END FUNCTION
  293. FUNCTION TestNoFile(a)
  294. IF FSO.FileExists(a) THEN
  295. FailOut()
  296. END IF
  297. END FUNCTION
  298. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  299. '-----GENERIC ERROR IF WE FAIL MEDIA RECOGNITION.
  300. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  301. SUB FailOut()
  302. MsgBox"The CD in drive "&CDSource&" does not appear to be a valid Windows XP Professional CD.", vbCritical, "Invalid CD in Drive "&CDSource
  303. WScript.Quit
  304. END SUB
  305. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  306. '-----ADD DATE, AND ADD ZEROS SO WE DON'T HAVE A GIBBERISH TIMESTAMP ON UNIQUE FOLDERNAME.
  307. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  308. SUB GetUnique()
  309. strAppend=FixUp(Hour(Now()))&FixUp(Minute(Now()))&FixUp(Second(Now()))
  310. IF Len(strAppend) = 5 THEN
  311. strAppend = strAppend&"0"
  312. ELSEIF Len(strAppend) = 4 THEN
  313. strAppend = strAppend&"00"
  314. END IF
  315. END SUB
  316. FUNCTION FixUp(a)
  317. If Len(a) = 1 THEN
  318. FixUp = 0&a
  319. ELSE
  320. Fixup = a
  321. END IF
  322. END FUNCTION
  323. FUNCTION CleanLocation(a)
  324. CleanLocation = REPLACE(a, """", "")
  325. END FUNCTION