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.

1493 lines
53 KiB

  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // System wide constants.
  3. // These constants are not expected to change.
  4. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5. var CLASSID_MTSCRIPT = "{854c316d-c854-4a77-b189-606859e4391b}";
  6. Error.prototype.toString = Error_ToString;
  7. Object.prototype.toString = Object_ToString;
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. // Global constants
  10. // These are subject to change and tuning.
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12. var g_aProcsToKill = ["mtscript.exe", "mshta.exe", "build.exe", "nmake.exe", "mtodaemon.exe"]; // "sleep.exe" is also killed seperately.
  13. // The lists of files to copy.
  14. // g_aCopyBCDistDir and g_aCopyBCDistDirD are the set of files which are in the same directory as this
  15. // script is, and are necessary for the operation of bcdist itself.
  16. // The g_aCopyBCDistDirD files are optional and are only necessary for running a debug MTScript.exe
  17. var g_aCopyBCDistDirScript = ['registermtscript.cmd'];
  18. var g_aCopyBCDistDir = ['bcrunas.exe', 'sleep.exe'];
  19. var g_aCopyBCDistDirD = ['mshtmdbg.dll', 'msvcrtd.dll'];
  20. // The g_aCopyFromScripts files are the Build Console Scripts.
  21. // These files are copied from either the current directory or .\mtscript
  22. var g_aCopyFromScripts = [ 'buildreport.js', 'harness.js', 'master.js', 'msgqueue.js',
  23. 'mtscript.js', 'publicdataupdate.js', 'robocopy.js',
  24. 'sendmail.js', 'slave.js', 'slaveproxy.js', 'slavetask.js',
  25. 'staticstrings.js', 'task.js', 'types.js', 'updatestatusvalue.js',
  26. 'utils.js', 'utilthrd.js',
  27. 'config_schema.xml', 'enviro_schema.xml'
  28. ];
  29. // The g_aCopyFromBin files are the Build Console executable files.
  30. // These files are copied from either the current directory or .\%ARCH% (x86, axp64,...)
  31. var g_aCopyFromBin = [ 'mtscript.exe', 'mtlocal.dll', 'mtscrprx.dll', 'mtrcopy.dll', "mtodaemon.exe", "mtodproxy.dll"];
  32. var g_strDropServer = "\\\\ptt\\cftools\\test\\bcrel";
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. // Global Variables
  36. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. var g_strMyLocation;
  38. var g_objNet;
  39. var g_strLocalHost;
  40. var g_objFS;
  41. var g_strShareDir;
  42. var g_Opts;
  43. if (DoLocalSetup())
  44. {
  45. g_Opts = ParseArguments(new DefaultArguments());
  46. if (g_Opts)
  47. {
  48. if (main(g_Opts))
  49. WScript.Quit(0);
  50. }
  51. }
  52. WScript.Quit(1);
  53. // GetMachineList(objArgs) //
  54. // //
  55. // Reads the XML BC environment template to extract the //
  56. // list of machines in the build. //
  57. // //
  58. function GetMachineList(objArgs)
  59. {
  60. var xml = new ActiveXObject('Microsoft.XMLDOM');
  61. var node;
  62. var nodelist;
  63. var objMach;
  64. var nIndex;
  65. xml.async = false;
  66. // It's unlikely they have the schema file available for this template,
  67. // so we turn off schema validation right now. The script engine will
  68. // validate it when we start the build.
  69. xml.validateOnParse = false;
  70. xml.resolveExternals = false;
  71. if (!xml.load(objArgs.strXMLFile) || !xml.documentElement)
  72. {
  73. Usage("Not a valid xml file: " + objArgs.strXMLFile);
  74. }
  75. node = xml.documentElement.selectSingleNode('BuildManager');
  76. if (!node)
  77. {
  78. Usage("Not a valid xml file: " + objArgs.strXMLFile);
  79. }
  80. objArgs.aMachines[0] = new MachineInfo(node.getAttribute("Name"));
  81. objArgs.aMachines[0].WriteStatusInfo("Initializing");
  82. nodelist = xml.documentElement.selectNodes('Machine');
  83. for (node = nodelist.nextNode(); node; node = nodelist.nextNode())
  84. {
  85. // Don't initialize the build manager machine twice
  86. // just because it is also used in the building process
  87. if ( objArgs.aMachines[0].strName.toLowerCase() == node.getAttribute("Name").toLowerCase() )
  88. continue;
  89. nIndex = objArgs.aMachines.length;
  90. objArgs.aMachines[nIndex] = new MachineInfo(node.getAttribute("Name"));
  91. objArgs.aMachines[nIndex].WriteStatusInfo("Initializing");
  92. }
  93. }
  94. // GetStringArgument(objArgs, strMember) //
  95. // //
  96. // Helper function for ParseArguments. Stores the //
  97. // next argument in the "strMember" member of "objArgs". //
  98. // Exits script with Usage() if there is no available argument. //
  99. function GetStringArgument(objArgs, strMember)
  100. {
  101. if (objArgs[strMember] == null)
  102. {
  103. LogMsg("ParseArguments internal error: Unknown member: " + strMember);
  104. WScript.Quit(1);
  105. }
  106. objArgs.argn++;
  107. if (objArgs.argn < objArgs.objArguments.length)
  108. objArgs[strMember] = objArgs.objArguments(objArgs.argn);
  109. else
  110. Usage("missing paramter");
  111. }
  112. function ParseArguments(objArgs)
  113. {
  114. var strArg;
  115. var chArg0;
  116. var chArg1;
  117. var nIndex;
  118. var fMachineList = false;
  119. for(objArgs.argn = 0; objArgs.argn < objArgs.objArguments.length; objArgs.argn++)
  120. {
  121. strArg = objArgs.objArguments(objArgs.argn);
  122. chArg0 = strArg.charAt(0);
  123. chArg1 = strArg.toLowerCase().slice(1);
  124. if (chArg0 != '-' && chArg0 != '/')
  125. {
  126. if (objArgs.strXMLFile != '')
  127. {
  128. Usage("Enter an XML file or a list of the machines but not both");
  129. }
  130. else
  131. {
  132. fMachineList = true;
  133. nIndex = objArgs.aMachines.length;
  134. objArgs.aMachines[nIndex] = new MachineInfo(strArg);
  135. objArgs.aMachines[nIndex].WriteStatusInfo("Initializing");
  136. }
  137. }
  138. else
  139. {
  140. // Currently, no options
  141. switch(chArg1)
  142. {
  143. case 'v':
  144. objArgs.fVerbose = true;
  145. break;
  146. case 'd':
  147. objArgs.argn++;
  148. if (objArgs.argn < objArgs.objArguments.length)
  149. {
  150. if (objArgs.strSrcDir == '')
  151. objArgs.nDropNum = Number(objArgs.objArguments(objArgs.argn));
  152. else
  153. Usage("Only one of -src and -d may be specified");
  154. }
  155. else
  156. Usage("missing parameter for -d arugment");
  157. if (objArgs.nDropNum < 1000 || objArgs.nDropNum >= 10000)
  158. Usage("dropnum range is 1000...9999");
  159. break;
  160. case 'dropsrc':
  161. GetStringArgument(objArgs, "strDropSrc");
  162. break;
  163. case 'src':
  164. GetStringArgument(objArgs, "strSrcDir");
  165. break;
  166. case 'dst':
  167. GetStringArgument(objArgs, "strDstDir");
  168. break;
  169. case 'u':
  170. GetStringArgument(objArgs, "strUserName");
  171. if (objArgs.strUserName == '*')
  172. {
  173. objArgs.strUserName = g_objNet.UserDomain + '\\' + g_objNet.UserName;
  174. }
  175. break;
  176. //case 'p':
  177. // GetStringArgument(objArgs, "strPassword");
  178. // break;
  179. case 'arch':
  180. GetStringArgument(objArgs, "strArch");
  181. break;
  182. case 'f': // Entered an XML file on the command line. Get the list of machines from here.
  183. if (fMachineList)
  184. Usage("Enter an XML file or a list of the machines but not both");
  185. GetStringArgument(objArgs, "strXMLFile");
  186. GetMachineList(objArgs);
  187. nIndex = objArgs.aMachines.length;
  188. break;
  189. case 'debug':
  190. objArgs.strExeType = 'debug';
  191. break;
  192. default:
  193. Usage("Unknown argument: " + strArg);
  194. break;
  195. }
  196. }
  197. }
  198. if (objArgs.aMachines.length == 0)
  199. Usage("you must specify one or more machine names or an XML file");
  200. if (objArgs.nDropNum)
  201. objArgs.strSrcDir = objArgs.strDropSrc + "\\" + objArgs.nDropNum;
  202. else if (objArgs.strSrcDir == '')
  203. Usage("you must specify a drop number or source location");
  204. //if (objArgs.strUserName == '' || objArgs.strPassword == '')
  205. // Usage("username and password required");
  206. WScript.StdOut.Write('Enter Password:');
  207. objArgs.strPassword = WScript.StdIn.ReadLine();
  208. delete objArgs.objArguments;
  209. delete objArgs.argn;
  210. return objArgs;
  211. }
  212. function Usage(strError)
  213. {
  214. WScript.Echo('');
  215. WScript.Echo('Usage: BCDist -u domain\\username ');
  216. WScript.Echo(' [-v] [-d nnnn [-dropsrc path] | -src path] ');
  217. WScript.Echo(' [-dst path] [-arch x86] [-debug] machines...');
  218. WScript.Echo(' -u d\\u = domain\\username');
  219. WScript.Echo(' -f xmlFile = environment xml template');
  220. WScript.Echo(' -v = verbose');
  221. WScript.Echo(' -d = drop number');
  222. WScript.Echo(' -dropsrc = drop source location ("' + g_strDropServer + '")');
  223. WScript.Echo(' -src = source directory');
  224. WScript.Echo(' -dst = destination directory');
  225. WScript.Echo(' -arch = Specifiy x86 or alpha');
  226. WScript.Echo(' -debug = copy debug exes instead of retail');
  227. WScript.Echo('Do NOT enter the password on the command line');
  228. if (strError && strError != '')
  229. {
  230. WScript.Echo('');
  231. WScript.Echo('Error: ' + strError);
  232. }
  233. WScript.Quit(1);
  234. }
  235. // DefaultArguments() //
  236. // //
  237. // Create an options object with the default //
  238. // options filled in. //
  239. function DefaultArguments()
  240. {
  241. var obj =
  242. {
  243. objArguments:WScript.Arguments,
  244. argn:0,
  245. aMachines:new Array(),
  246. fVerbose:false,
  247. nDropNum:0,
  248. strDropSrc:g_strDropServer,
  249. strSrcDir:'',
  250. strDstDir:'',
  251. strArch:"x86",
  252. strExeType:"retail",
  253. strUserName:'',
  254. strPassword:'',
  255. strXMLFile:''
  256. };
  257. return obj;
  258. }
  259. // DoLocalSetup() //
  260. // //
  261. // Setup some globals. Necessary before arugment parsing //
  262. // can succeed. //
  263. // //
  264. function DoLocalSetup()
  265. {
  266. var i = -1;
  267. var objFile;
  268. var strPath;
  269. var objLogin;
  270. var objRemote;
  271. try
  272. {
  273. g_objNet = WScript.CreateObject("WScript.Network");
  274. g_strLocalHost = g_objNet.ComputerName;
  275. g_objFS = new FileSystemObject();
  276. g_strMyLocation = SplitFileName(WScript.ScriptFullName)[0];
  277. LogMsg("Creating local share...");
  278. objLogin =
  279. {
  280. strMachine:g_strLocalHost
  281. }
  282. objRemote = new WMIInterface(objLogin, '');
  283. g_strShareDir = g_strMyLocation + "bcdiststatus";
  284. g_objFS.CreateFolder(g_strShareDir);
  285. strPath = objRemote.RemoteShareExists("BC_DISTSTATUS");
  286. if (strPath != "")
  287. g_strShareDir = strPath;
  288. else
  289. objRemote.RemoteCreateShare("BC_DISTSTATUS", g_strShareDir, "Build Console Distribution Status");
  290. }
  291. catch(ex)
  292. {
  293. LogMsg("BCDist failed to do local setup: " + ex);
  294. return false;
  295. }
  296. return true;
  297. }
  298. function main(objOpts)
  299. {
  300. var strPath;
  301. var strPathUNC;
  302. var fSuccess = true;
  303. var strDomain = objOpts.strUserName.split("\\")[0];
  304. var strUser = objOpts.strUserName.split("\\")[1];
  305. var fLocal;
  306. var strCmd;
  307. LogMsg("Upgrading the following machines:");
  308. for(i = 0; i < objOpts.aMachines.length;++i)
  309. {
  310. LogMsg(" " + objOpts.aMachines[i].strName);
  311. }
  312. for(i = 0; i < objOpts.aMachines.length;++i)
  313. {
  314. fLocal = false;
  315. strCmd = '';
  316. try
  317. {
  318. LogMsg("Distributing to " + objOpts.aMachines[i].strName);
  319. var objLogin =
  320. {
  321. strMachine:objOpts.aMachines[i].strName,
  322. strUser:objOpts.strUserName, // Unneccessary if the same as the current user.
  323. strPassword:objOpts.strPassword
  324. }
  325. if (objLogin.strMachine.toUpperCase() == g_objNet.ComputerName.toUpperCase())
  326. { // On the local host you may not specify a username
  327. if (objLogin.strUser.toUpperCase() != (g_objNet.UserDomain + '\\' + g_objNet.UserName).toUpperCase())
  328. throw new Error(-1, "Specified username does not match current login for local machine");
  329. delete objLogin.strUser;
  330. delete objLogin.strPassword;
  331. fLocal = true;
  332. }
  333. objOpts.aMachines[i].WriteStatusInfo("Connecting");
  334. var objRemote = new WMIInterface(objLogin, '');
  335. var objRegistry = new WMIRegistry(objLogin);
  336. objOpts.aMachines[i].WriteStatusInfo("Terminating MTScript.exe");
  337. LogMsg(" Terminating remote processes");
  338. TerminateMTScript(objRemote, objRegistry);
  339. strPath = DetermineInstallPoint(objOpts, objRemote, objRegistry);
  340. strPathUNC = PathToUNC(objOpts.aMachines[i].strName, strPath);
  341. objOpts.aMachines[i].WriteStatusInfo("Copying files");
  342. CopyFiles(objRemote, strPathUNC, objOpts);
  343. objOpts.aMachines[i].WriteStatusInfo("Creating share");
  344. ResetRegistry(objRegistry, strPath);
  345. objOpts.aMachines[i].WriteStatusInfo("Registering");
  346. objOpts.aMachines[i].DisableWriteStatusInfo();
  347. LogMsg(" Registering");
  348. // We don't use BCRunAs when running on the local machine.
  349. if (!fLocal)
  350. {
  351. // Usage: BCRunAs UserName Domain Password CWD Cmdline
  352. //
  353. strCmd = strPath + '\\BCRunAs.exe ' + strUser + ' ' +
  354. strDomain + ' "' + objOpts.strPassword + '" "';
  355. }
  356. strCmd += 'cmd.exe /K ' + strPath + "\\RegisterMTScript.cmd " +
  357. g_strLocalHost;
  358. if (!fLocal)
  359. {
  360. strCmd += '"';
  361. }
  362. objRemote.RemoteExecuteCmd(strCmd, strPath);
  363. // Do not write any more status after this point -- the remote machine should do it now.
  364. }
  365. catch(ex)
  366. {
  367. fSuccess = false;
  368. objOpts.aMachines[i].WriteStatusInfo("FAILED " + ex);
  369. }
  370. }
  371. if (! CheckStatus(objOpts) )
  372. fSuccess = false;
  373. return fSuccess;
  374. }
  375. function CheckStatus(objOpts)
  376. {
  377. var i;
  378. var fTerminalState;
  379. var strStatus;
  380. var aStatus;
  381. var aState = new Array()
  382. var fChanged;
  383. LogMsg("");
  384. LogMsg("Current status on the remote machines...(Press Ctrl-C to quit)");
  385. do
  386. {
  387. fTerminalState = true;
  388. fChanged = false;
  389. for(i = 0; i < objOpts.aMachines.length;++i)
  390. {
  391. strStatus = objOpts.aMachines[i].ReadStatusInfo();
  392. aStatus = strStatus.split(" ");
  393. if (aStatus.length == 0 || aStatus[0] != 'OK' && aStatus[0] != 'FAILED')
  394. fTerminalState = false;
  395. if (aState[i] == null || aState[i] != strStatus)
  396. {
  397. aState[i] = strStatus;
  398. fChanged = true;
  399. }
  400. }
  401. if (fChanged)
  402. {
  403. for(i = 0; i < objOpts.aMachines.length;++i)
  404. LogMsg(objOpts.aMachines[i].strName + ": " + aState[i]);
  405. if (!fTerminalState)
  406. LogMsg("Waiting...");
  407. LogMsg("");
  408. }
  409. if (!fTerminalState)
  410. WScript.Sleep(1000);
  411. } while(!fTerminalState);
  412. }
  413. // TerminateMTScript(objRemote, objRegistry) //
  414. // //
  415. // Using WMI, terminate processes which may be involved //
  416. // in a build. This is neccessary before a BC upgrade can happen. //
  417. // Also, remote "mtscript.exe" to prevent it getting restarted //
  418. // prematurely. //
  419. // //
  420. function TerminateMTScript(objRemote, objRegistry)
  421. {
  422. var i;
  423. var fRenamed = false;
  424. var strMTScriptPath = ''
  425. try
  426. {
  427. strMTScriptPath = objRegistry.GetExpandedStringValue(WMIRegistry.prototype.HKCR, "CLSID\\" + CLASSID_MTSCRIPT + "\\LocalServer32", '');
  428. }
  429. catch(ex)
  430. {
  431. }
  432. if (strMTScriptPath != '')
  433. {
  434. if (objRemote.RemoteFileExists(strMTScriptPath + ".UpdateInProgress"))
  435. objRemote.RemoteDeleteFile(strMTScriptPath + ".UpdateInProgress");
  436. if (objRemote.RemoteFileExists(strMTScriptPath) &&
  437. objRemote.RemoteRenameFile(strMTScriptPath, strMTScriptPath + ".UpdateInProgress"))
  438. {
  439. fRenamed = true;
  440. }
  441. }
  442. for(i = 0; i < g_aProcsToKill.length; ++i)
  443. objRemote.RemoteTerminateExe(g_aProcsToKill[i], 1);
  444. objRemote.RemoteTerminateExe("sleep.exe", 0); // If sleep.exe sets ERRORLEVEL != 0, then the remote cmd.exe windows will not close.
  445. if (fRenamed)
  446. {
  447. for( i = 3; i >= 0; --i)
  448. {
  449. try
  450. {
  451. objRemote.RemoteDeleteFile(strMTScriptPath + ".UpdateInProgress");
  452. }
  453. catch(ex)
  454. {
  455. if (i == 0)
  456. throw ex;
  457. WScript.Sleep(500); // It sometimes takes a little while for the remote mtscript.exe to quit.
  458. continue;
  459. }
  460. break;
  461. }
  462. }
  463. return true;
  464. }
  465. // DetermineInstallPoint(objOpts, objRemote, objRegistry) //
  466. // //
  467. // If the user has supplied a destination path, use that. //
  468. // Otherwise if mtscript.exe has previously been registered on the remote machine //
  469. // then install to the same location. //
  470. // Else, report an error. //
  471. function DetermineInstallPoint(objOpts, objRemote, objRegistry)
  472. {
  473. var strMTScriptPath = ''
  474. if (objOpts.strDstDir != '')
  475. return objOpts.strDstDir;
  476. try
  477. {
  478. strMTScriptPath = objRegistry.GetExpandedStringValue(WMIRegistry.prototype.HKCR, "CLSID\\" + CLASSID_MTSCRIPT + "\\LocalServer32", '');
  479. }
  480. catch(ex)
  481. {
  482. }
  483. if (strMTScriptPath != '')
  484. strMTScriptPath = SplitFileName(strMTScriptPath)[0];
  485. else
  486. throw new Error(-1, "-dst must be specified -- mtscript was not previously registered");
  487. return strMTScriptPath;
  488. }
  489. // CopyFiles(objRemote, strDstPath, objOpts) //
  490. // //
  491. // Copy the necessary files to the remote machine. //
  492. // The files are always copied to a "flat" install -- the executables and the scripts //
  493. // and as the same directory level. //
  494. // The files in a daily drop are not flat - the executables and the //
  495. // scripts are in seperate directories. //
  496. function CopyFiles(objRemote, strDstPath, objOpts)
  497. {
  498. var i;
  499. var strSourcePath;
  500. var strDstPathUNC;
  501. var strAltLocation;
  502. strSourcePath = RemoveEndChar(objOpts.strSrcDir, "\\");
  503. strDstPathUNC = RemoveEndChar(strDstPath, "\\");
  504. Trace("Copy files from " + strSourcePath + " to " + strDstPathUNC);
  505. g_objFS.CreateFolder(strDstPathUNC);
  506. strAltLocation = strSourcePath + "\\" + objOpts.strArch;
  507. CopyListOfFiles(g_aCopyBCDistDirScript, g_strMyLocation, null, strDstPathUNC, true);
  508. if (objOpts.nDropNum)
  509. {
  510. LogMsg(" Copying files from drop " + strSourcePath);
  511. CopyListOfFiles(g_aCopyFromScripts, strSourcePath + "\\scripts", null, strDstPathUNC, true);
  512. CopyListOfFiles(g_aCopyBCDistDir, strSourcePath + "\\" + objOpts.strArch + "\\" + objOpts.strExeType, null, strDstPathUNC, true);
  513. CopyListOfFiles(g_aCopyBCDistDirD, strSourcePath + "\\" + objOpts.strArch + "\\" + objOpts.strExeType, null, strDstPathUNC, false);
  514. CopyListOfFiles(g_aCopyFromBin, strSourcePath + "\\" + objOpts.strArch + "\\" + objOpts.strExeType, null, strDstPathUNC, true);
  515. }
  516. else
  517. {
  518. LogMsg(" Copying files from " + strSourcePath);
  519. CopyListOfFiles(g_aCopyFromScripts, strSourcePath, strSourcePath + "\\mtscript", strDstPathUNC, true);
  520. CopyListOfFiles(g_aCopyBCDistDir, strSourcePath, strSourcePath + "\\" + objOpts.strArch, strDstPathUNC, true);
  521. CopyListOfFiles(g_aCopyBCDistDirD, strSourcePath, strSourcePath + "\\" + objOpts.strArch, strDstPathUNC, false);
  522. CopyListOfFiles(g_aCopyFromBin, strSourcePath, strSourcePath + "\\" + objOpts.strArch, strDstPathUNC, true);
  523. }
  524. }
  525. // CopyListOfFiles(aFiles, strSrc, strAltSrc, strDst, fRequired) //
  526. // //
  527. // Copy a list of files. //
  528. // Check for the existance of each file in either the strSrc or strAltSrc path. //
  529. // Copy to the strDst path. //
  530. // If a file does not exist, and fRequired is set, then throw an exception. //
  531. function CopyListOfFiles(aFiles, strSrc, strAltSrc, strDst, fRequired)
  532. {
  533. var i;
  534. for(i = 0; i < aFiles.length; ++i)
  535. {
  536. if (g_objFS.FileExists(strSrc + "\\" + aFiles[i]))
  537. g_objFS.CopyFile(strSrc + "\\" + aFiles[i], strDst + "\\" + aFiles[i]);
  538. else if (strAltSrc && g_objFS.FileExists(strAltSrc + "\\" + aFiles[i]))
  539. g_objFS.CopyFile(strAltSrc + "\\" + aFiles[i], strDst + "\\" + aFiles[i]);
  540. else if (fRequired)
  541. throw new Error(-1, "File not found: " + strSrc + "\\" + aFiles[i]);
  542. }
  543. }
  544. // ResetRegistry(objRegistry, strPath) //
  545. // //
  546. // Reset the registry entries for the script path. //
  547. function ResetRegistry(objRegistry, strPath)
  548. {
  549. objRegistry.CreateKey(WMIRegistry.prototype.HKCU, "Software\\Microsoft\\MTScript\\File Paths");
  550. objRegistry.SetStringValue(WMIRegistry.prototype.HKCU, "Software\\Microsoft\\MTScript\\File Paths", "Script Path", strPath);
  551. objRegistry.SetStringValue(WMIRegistry.prototype.HKCU, "Software\\Microsoft\\MTScript\\File Paths", "Initial Script", "mtscript.js");
  552. }
  553. //*********************************************************************
  554. //*********************************************************************
  555. //*********************************************************************
  556. //*********************************************************************
  557. // Library funtions
  558. // SplitFileName(strPath)
  559. // Return an array of 3 elements, path,filename,extension
  560. // [0] == "C:\path\"
  561. // [1] == "filename"
  562. // [2] == ".ext"
  563. function SplitFileName(strPath)
  564. {
  565. var nDot = strPath.lastIndexOf('.');
  566. var nSlash = strPath.lastIndexOf('\\');
  567. var nColon = strPath.lastIndexOf(':');
  568. if (nDot >= 0 && nDot > nSlash && nDot > nColon)
  569. {
  570. return [strPath.slice(0, nSlash + 1), strPath.slice(nSlash + 1, nDot), strPath.slice(nDot)];
  571. }
  572. // We get here if the file had no extension
  573. if (nSlash >= 2) // do not slice the UNC double \ at the start of a filename.
  574. {
  575. return [strPath.slice(0, nSlash + 1), strPath.slice(nSlash + 1, nDot), ''];
  576. }
  577. return ['', strPath, ''];
  578. }
  579. // RemoveEndChar(str, strChar) //
  580. // //
  581. // If 'strChar' appears as the last character //
  582. // in a string, remove it. //
  583. function RemoveEndChar(str, strChar)
  584. {
  585. var length = str.length;
  586. if (str.charAt(length - 1) == strChar)
  587. str = str.slice(0, length - 1);
  588. return str;
  589. }
  590. function PathToUNC(strMachineName, strPath)
  591. {
  592. return "\\\\" +
  593. strMachineName +
  594. "\\" +
  595. strPath.charAt(0) +
  596. "$" +
  597. strPath.slice(2)
  598. }
  599. function Assert(fOK, msg)
  600. {
  601. if (!fOK)
  602. {
  603. var caller = GetCallerName(null);
  604. LogMsg("ASSERTION FAILED :(" + caller + ") " + msg);
  605. WScript.Quit(0);
  606. }
  607. }
  608. function unevalString(str)
  609. {
  610. var i;
  611. var newstr = '"';
  612. var c;
  613. for(i = 0; i < str.length; ++i)
  614. {
  615. c = str.charAt(i);
  616. switch(c)
  617. {
  618. case'\\':
  619. newstr += "\\\\";
  620. break;
  621. case '"':
  622. newstr += '\\"';
  623. break;
  624. case "'":
  625. newstr += "\\'";
  626. break;
  627. case "\n":
  628. newstr += "\\n";
  629. break;
  630. case "\r":
  631. newstr += "\\r";
  632. break;
  633. case "\t":
  634. newstr += "\\t";
  635. break;
  636. default:
  637. newstr += c;
  638. break;
  639. }
  640. }
  641. return newstr + '"';
  642. }
  643. // Object_ToString() //
  644. // //
  645. // Provide a useful version of conversion //
  646. // from "object" to string - great for dumping //
  647. // objects to the debug log. //
  648. function Object_ToString()
  649. {
  650. var i;
  651. var str = "{";
  652. var strComma = '';
  653. for(i in this)
  654. {
  655. str += strComma + i + ":" + this[i];
  656. strComma = ', ';
  657. }
  658. return str + "}";
  659. }
  660. function Error_ToString()
  661. {
  662. var i;
  663. var str = 'Exception(';
  664. /*
  665. Only some error messages get filled in for "ex".
  666. Specifically the text for disk full never seems
  667. to get set by functions such as CreateTextFile().
  668. */
  669. if (this.number != null && this.description == '')
  670. {
  671. switch(this.number)
  672. {
  673. case -2147024784:
  674. this.description = "There is not enough space on the disk.";
  675. break;
  676. case -2147024894:
  677. this.description = "The system cannot find the file specified.";
  678. break;
  679. case -2147023585:
  680. this.description = "There are currently no logon servers available to service the logon request.";
  681. break;
  682. case -2147023170:
  683. this.description = "The remote procedure call failed.";
  684. break;
  685. case -2147024837:
  686. this.description = "An unexpected network error occurred";
  687. break;
  688. case -2147024890:
  689. this.description = "The handle is invalid.";
  690. break;
  691. default:
  692. this.description = "Error text not set for (" + this.number + ")";
  693. break;
  694. }
  695. }
  696. if (this.description)
  697. {
  698. var end = this.description.length - 1;
  699. while (this.description.charAt(end) == '\n' || this.description.charAt(end) == '\r')
  700. {
  701. end--;
  702. }
  703. this.description = this.description.slice(0, end+1);
  704. }
  705. for(i in this)
  706. {
  707. str += i + ": " + this[i] + " ";
  708. }
  709. return str + ")";
  710. }
  711. function LogMsg(msg)
  712. {
  713. WScript.Echo(msg);
  714. }
  715. function Trace(msg)
  716. {
  717. if (g_Opts && g_Opts.fVerbose)
  718. WScript.Echo(" TRACE: " + msg);
  719. }
  720. function GetCallerName(cIgnoreCaller)
  721. {
  722. var tokens;
  723. if (cIgnoreCaller == null)
  724. cIgnoreCaller = 0;
  725. ++cIgnoreCaller;
  726. var caller = GetCallerName.caller;
  727. while (caller != null && cIgnoreCaller)
  728. {
  729. caller = caller.caller;
  730. --cIgnoreCaller;
  731. }
  732. if (caller != null)
  733. {
  734. tokens = caller.toString().split(/ |\t|\)|,/);
  735. if (tokens.length > 1 && tokens[0] == "function")
  736. {
  737. return tokens[1] + ")";
  738. }
  739. }
  740. return "<undefined>";
  741. }
  742. // class WMIInterface(objLogin, strNameSpace) //
  743. // //
  744. // This class provides an easier to use interface to the WMI //
  745. // functionality. //
  746. // //
  747. // You must provide login information and the WMI namespace //
  748. // you wish to use. //
  749. // //
  750. // RemoteFileExists(strPath) NOTHROW //
  751. // returns true if the given file exists on the remote machine //
  752. // //
  753. // RemoteRenameFile(strFrom, strTo) //
  754. // Renames the given file. //
  755. // Throws on any error. //
  756. // //
  757. // RemoteDeleteFile(strPath) //
  758. // Delete the file. //
  759. // Throws on any error. //
  760. // //
  761. // RemoteTerminateExe(strExeName) //
  762. // Terminates all processes with the given name. //
  763. // Does not throw if the process does not exist. //
  764. // It will throw if the RPC to terminate a process fails. //
  765. // Does not return error status. //
  766. // //
  767. // RemoteExecuteCmd(strCmd, strDirectory) //
  768. // Runs the given command in the specified directory. //
  769. // It will throw if the RPC to start the process fails. //
  770. // Its not possible to retrieve status from the command //
  771. // which is run. //
  772. // //
  773. // RemoteDeleteShare(strShareName) //
  774. // If the named share exists, remove it. //
  775. // Throw on error (except if the error is "not found") //
  776. // //
  777. // RemoteCreateShare(strShareName, strSharePath, strShareComment) //
  778. // Create a share name "strShareName" with the given path and comment. //
  779. // Throw on error (it is an error if strShareName is already shared). //
  780. // //
  781. // RemoteShareExists(strShareName) //
  782. // Returns the shared path, or "" //
  783. // does not throw any errors. //
  784. // //
  785. function WMIInterface(objLogin, strNameSpace)
  786. {
  787. try
  788. {
  789. WMIInterface.prototype.wbemErrNotFound = -2147217406;
  790. WMIInterface.prototype.RemoteFileExists = _WMIInterface_FileExists;
  791. WMIInterface.prototype.RemoteRenameFile = _WMIInterface_RenameFile;
  792. WMIInterface.prototype.RemoteDeleteFile = _WMIInterface_DeleteFile;
  793. WMIInterface.prototype.RemoteTerminateExe = _WMIInterface_TerminateExe;
  794. WMIInterface.prototype.RemoteExecuteCmd = _WMIInterface__ExecuteCMD;
  795. WMIInterface.prototype.RemoteDeleteShare = _WMIInterface__DeleteShare;
  796. WMIInterface.prototype.RemoteCreateShare = _WMIInterface__CreateShare;
  797. WMIInterface.prototype.RemoteShareExists = _WMIInterface__ShareExists;
  798. // Private methods
  799. WMIInterface.prototype._FindFiles = _WMIInterface__FindFiles;
  800. WMIInterface.prototype._FileOperation = _WMIInterface__FileOperation;
  801. if (!strNameSpace || strNameSpace == '')
  802. strNameSpace = "root\\cimv2";
  803. this._strNameSpace = strNameSpace;
  804. this._objLogin = objLogin;
  805. this._objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
  806. this._objService = this._objLocator.ConnectServer(this._objLogin.strMachine, this._strNameSpace, this._objLogin.strUser, this._objLogin.strPassword);
  807. this._objService.Security_.impersonationlevel = 3
  808. }
  809. catch(ex)
  810. {
  811. LogMsg("WMIInterface logon failed " + ex);
  812. throw ex;
  813. }
  814. }
  815. function _WMIInterface__FindFiles(strPattern)
  816. {
  817. var objFileSet = this._objService.ExecQuery('Select * from CIM_DataFile Where name=' + unevalString(strPattern));
  818. if (!objFileSet.Count)
  819. throw new Error(-1, "File not found: " + strPattern);
  820. return objFileSet;
  821. }
  822. function _WMIInterface__FileOperation(strPattern, strOperation, objInArgs)
  823. {
  824. try
  825. {
  826. var objFileSet = this._FindFiles(strPattern);
  827. var enumSet = new Enumerator(objFileSet);
  828. for (; !enumSet.atEnd(); enumSet.moveNext())
  829. {
  830. Trace("remote " + strOperation + " " + (objInArgs ? objInArgs : ''));
  831. var objOut = CallMethod(enumSet.item(), strOperation, objInArgs);
  832. break;
  833. }
  834. }
  835. catch(ex)
  836. {
  837. ex.description = strOperation + '(' + strPattern + ')' + " Failed, " + ex.description;
  838. throw ex;
  839. }
  840. return true;
  841. }
  842. function _WMIInterface_FileExists(strName)
  843. {
  844. try
  845. {
  846. var objFileSet = this._objService.ExecQuery('Select * from CIM_DataFile Where name=' + unevalString(strName));
  847. if (objFileSet.Count)
  848. return true;
  849. }
  850. catch(ex)
  851. {
  852. }
  853. Trace("RemoteFileExists: not found " + strName);
  854. return false;
  855. }
  856. function _WMIInterface_RenameFile(strFrom, strTo)
  857. {
  858. return this._FileOperation(strFrom, "Rename", {FileName:strTo});
  859. }
  860. function _WMIInterface_DeleteFile(strName)
  861. {
  862. return this._FileOperation(strName, "Delete");
  863. }
  864. function _WMIInterface_TerminateExe(strName, nExitCode)
  865. {
  866. var setQueryResults = this._objService.ExecQuery("Select Name,ProcessId From Win32_Process");
  867. var enumSet = new Enumerator(setQueryResults);
  868. var nCount = 0;
  869. for( ; !enumSet.atEnd(); enumSet.moveNext())
  870. {
  871. var item = enumSet.item();
  872. if (item.Name == strName)
  873. {
  874. var outParam = CallMethod(item, "Terminate", {Reason:nExitCode}); // Reason will be the return code.
  875. Trace("Killed " + item.Name + " pid = " + item.ProcessId);
  876. nCount++;
  877. }
  878. }
  879. if (!nCount)
  880. Trace("Cannot terminate process " + strName + ": not found");
  881. }
  882. function _WMIInterface__ExecuteCMD(strCmd, strDir)
  883. {
  884. try
  885. {
  886. Trace("Executing :" + strCmd);
  887. var objInstance = this._objService.Get("Win32_Process");
  888. var outParam = CallMethod(objInstance, "Create",
  889. {
  890. CommandLine:strCmd,
  891. CurrentDirectory:strDir
  892. });
  893. //EnumerateSetOfProperties("outParam properties", outParam.Properties_);
  894. Trace("ExecuteCMD " + strCmd + ", pid = " + outParam.ProcessId);
  895. }
  896. catch(ex)
  897. {
  898. ex.description = "ExecuteCMD " + strCmd + " failed " + ex.description;
  899. throw ex;
  900. }
  901. }
  902. function _WMIInterface__DeleteShare(strShareName)
  903. {
  904. try
  905. {
  906. var objInstance = this._objService.Get("Win32_Share='" + strShareName + "'");
  907. Trace("DeleteShare " + objInstance.Name + "," + objInstance.Path);
  908. CallMethod(objInstance, "Delete");
  909. }
  910. catch(ex)
  911. {
  912. if (ex.number != this.wbemErrNotFound)
  913. {
  914. ex.description = "DeleteShare " + strShareName + " failed " + ex.description;
  915. throw ex;
  916. }
  917. else
  918. Trace("DeleteShare " + strShareName + " not found");
  919. }
  920. }
  921. function _WMIInterface__ShareExists(strShareName)
  922. {
  923. try
  924. {
  925. var objInstance = this._objService.Get("Win32_Share='" + strShareName + "'");
  926. Trace("ShareExists " + objInstance.Name + "," + objInstance.Path);
  927. return objInstance.Path;
  928. }
  929. catch(ex)
  930. {
  931. if (ex.number != this.wbemErrNotFound)
  932. {
  933. ex.description = "ShareExists " + strShareName + " failed " + ex.description;
  934. throw ex;
  935. }
  936. else
  937. Trace("ShareExists " + strShareName + " not found");
  938. }
  939. return "";
  940. }
  941. function _WMIInterface__CreateShare(strShareName, strSharePath, strShareComment)
  942. {
  943. try
  944. {
  945. var objInstance = this._objService.Get("Win32_Share");
  946. var outParam = CallMethod(
  947. objInstance,
  948. "Create",
  949. {
  950. Description:strShareComment,
  951. Name:strShareName,
  952. Path:strSharePath,
  953. Type:0
  954. });
  955. }
  956. catch(ex)
  957. {
  958. ex.description = "CreateShare " + strShareName + " failed " + ex.description;
  959. throw ex;
  960. }
  961. }
  962. // class WMIRegistry(objLogin) //
  963. // Class to enable remote registry access via WMI //
  964. // //
  965. // This class provides an easier to use interface to the WMI //
  966. // functionality. //
  967. // //
  968. // You must provide login information and the WMI namespace //
  969. // you wish to use. //
  970. // //
  971. // GetExpandedStringValue(hkey, strSubKeyName, strValueName) //
  972. // Retieves the string value for the given registry key. //
  973. // If strValueName == '', then retrieve the default value. //
  974. // //
  975. // If the value is of type REG_EXPAND_SZ, then the returned //
  976. // string will be the expanded value. //
  977. // //
  978. // Throw any errors. //
  979. // //
  980. // SetStringValue(hkey, strSubKeyName, strValueName, strValue) //
  981. // Sets a string value for the given registry key. //
  982. // If strValueName == '', then set the default value. //
  983. // Throw any errors. //
  984. // //
  985. // CreateKey(hkey, strSubKeyName) //
  986. // Create the specified registry key. //
  987. // Multiple levels of keys can be created at once. //
  988. // It is not an error to create a key which already exists. //
  989. // //
  990. // Throw any errors. //
  991. function WMIRegistry(objLogin)
  992. {
  993. try
  994. {
  995. WMIRegistry.prototype.HKCR = 0x80000000; // Required by StdRegProv
  996. WMIRegistry.prototype.HKCU = 0x80000001; // Required by StdRegProv
  997. WMIRegistry.prototype.GetExpandedStringValue = _WMIRegistry_GetExpandedStringValue;
  998. WMIRegistry.prototype.SetStringValue = _WMIRegistry_SetStringValue;
  999. WMIRegistry.prototype.CreateKey = _WMIRegistry_CreateKey;
  1000. this._objRemote = new WMIInterface(objLogin, "root\\default");
  1001. this._objInstance = this._objRemote._objService.Get("StdRegProv");
  1002. }
  1003. catch(ex)
  1004. {
  1005. LogMsg("WMIRegistry failed " + ex);
  1006. throw ex;
  1007. }
  1008. }
  1009. function _WMIRegistry_GetExpandedStringValue(hkey, strSubKeyName, strValueName)
  1010. {
  1011. try
  1012. {
  1013. var outParam = CallMethod(this._objInstance, "GetExpandedStringValue", {hDefKey:hkey, sSubKeyName:strSubKeyName, sValueName:strValueName});
  1014. return outParam.sValue;
  1015. }
  1016. catch(ex)
  1017. {
  1018. ex.description = "GetExpandedStringValue failed ('" + strSubKeyName + "', '" + strValueName + "'): " + ex.description;
  1019. throw ex;
  1020. }
  1021. }
  1022. function _WMIRegistry_SetStringValue(hkey, strSubKeyName, strValueName, strValue)
  1023. {
  1024. try
  1025. {
  1026. var outParam = CallMethod(this._objInstance, "SetStringValue", {hDefKey:hkey, sSubKeyName:strSubKeyName, sValueName:strValueName, sValue:strValue});
  1027. // outParam.ReturnValue == 0;
  1028. }
  1029. catch(ex)
  1030. {
  1031. ex.description = "SetStringValue failed ('" + strSubKeyName + "', '" + strValueName + "'): " + ex.description;
  1032. throw ex;
  1033. }
  1034. }
  1035. function _WMIRegistry_CreateKey(hkey, strSubKeyName)
  1036. {
  1037. try
  1038. {
  1039. var outParam = CallMethod(this._objInstance, "CreateKey", {hDefKey:hkey, sSubKeyName:strSubKeyName});
  1040. }
  1041. catch(ex)
  1042. {
  1043. ex.description = "CreateKey failed ('" + strSubKeyName + "', '" + strValueName + "'): " + ex.description;
  1044. throw ex;
  1045. }
  1046. }
  1047. // FileSystemObject() //
  1048. // //
  1049. // Provide enhanced file system access. //
  1050. // The primary functionaly here is to provide better error //
  1051. // reporting. //
  1052. function FileSystemObject()
  1053. {
  1054. if (!FileSystemObject.prototype.objFS)
  1055. {
  1056. FileSystemObject.prototype.objFS = new ActiveXObject("Scripting.FileSystemObject");
  1057. FileSystemObject.prototype.FileExists = _FileSystemObject_FileExists;
  1058. FileSystemObject.prototype.FolderExists = _FileSystemObject_FolderExists;
  1059. FileSystemObject.prototype.CreateFolder = _FileSystemObject_CreateFolder;
  1060. FileSystemObject.prototype.DeleteFile = _FileSystemObject_DeleteFile;
  1061. FileSystemObject.prototype.DeleteFolder = _FileSystemObject_DeleteFolder;
  1062. FileSystemObject.prototype.MoveFolder = _FileSystemObject_MoveFolder;
  1063. FileSystemObject.prototype.CopyFolder = _FileSystemObject_CopyFolder;
  1064. FileSystemObject.prototype.CopyFile = _FileSystemObject_CopyFile;
  1065. FileSystemObject.prototype.CreateTextFile = _FileSystemObject_CreateTextFile;
  1066. FileSystemObject.prototype.OpenTextFile = _FileSystemObject_OpenTextFile;
  1067. }
  1068. }
  1069. function _FileSystemObject_FileExists(str)
  1070. {
  1071. try
  1072. {
  1073. var fRet = this.objFS.FileExists(str);
  1074. Trace("FileExists('" + str + "') = " + fRet);
  1075. return fRet;
  1076. }
  1077. catch(ex)
  1078. {
  1079. ex.description = "FileExists('" + str + "') failed: " + ex.description;
  1080. throw ex;
  1081. }
  1082. }
  1083. function _FileSystemObject_FolderExists(str)
  1084. {
  1085. try
  1086. {
  1087. var fRet = this.objFS.FolderExists(str);
  1088. Trace("FolderExists('" + str + "') = " + fRet);
  1089. return fRet;
  1090. }
  1091. catch(ex)
  1092. {
  1093. Trace("FolderExists('" + str + "') failed: " + ex);
  1094. return false;
  1095. }
  1096. }
  1097. function _FileSystemObject_CreateFolder(str)
  1098. {
  1099. try
  1100. {
  1101. if (!this.FolderExists(str))
  1102. this.objFS.CreateFolder(str);
  1103. }
  1104. catch(ex)
  1105. {
  1106. ex.description = "CreateFolder('" + str + "') failed: " + ex.description;
  1107. throw ex;
  1108. }
  1109. }
  1110. function _FileSystemObject_DeleteFile(str)
  1111. {
  1112. try
  1113. {
  1114. Trace("DeleteFile '" + str + "'");
  1115. this.objFS.DeleteFile(str, true);
  1116. }
  1117. catch(ex)
  1118. {
  1119. ex.description = "DeleteFile(" + str + ") failed " + ex.description;
  1120. throw ex;
  1121. }
  1122. }
  1123. function _FileSystemObject_DeleteFolder(str)
  1124. {
  1125. try
  1126. {
  1127. Trace("DeleteFolder '" + str + "'");
  1128. this.objFS.DeleteFolder(str, true);
  1129. }
  1130. catch(ex)
  1131. {
  1132. ex.description = "DeleteFolder('" + str + "' failed: " + ex.description;
  1133. throw ex;
  1134. }
  1135. }
  1136. function _FileSystemObject_MoveFolder(str, strDst)
  1137. {
  1138. try
  1139. {
  1140. Trace("MoveFolder '" + str + "', '" + strDst + "'");
  1141. this.objFS.MoveFolder(str, strDst);
  1142. }
  1143. catch(ex)
  1144. {
  1145. ex.description = "MoveFolder('" + str + "', '" + strDst + "' failed: " + ex.description;
  1146. throw ex;
  1147. }
  1148. }
  1149. function _FileSystemObject_CreateTextFile(strFileName, fOverwrite)
  1150. {
  1151. do
  1152. {
  1153. Trace("CreateTextFile '" + strFileName + "'");
  1154. try
  1155. {
  1156. return this.objFS.CreateTextFile(strFileName, fOverwrite);
  1157. }
  1158. catch(ex)
  1159. {
  1160. if (fOverwrite && this.FileExists(strFileName))
  1161. {
  1162. Trace(" CreateTextFile: Attempt to delete " + strFileName);
  1163. this.DeleteFile(strFileName);
  1164. continue;
  1165. }
  1166. ex.description = "CreateTextFile('" + strFileName + "' failed: " + ex.description;
  1167. throw ex;
  1168. }
  1169. } while(true);
  1170. }
  1171. function _FileSystemObject_OpenTextFile(strFileName)
  1172. {
  1173. var objFile;
  1174. do
  1175. {
  1176. try
  1177. {
  1178. objFile = this.objFS.OpenTextFile(strFileName, 1);
  1179. Trace("OpenTextFile '" + strFileName + "' success");
  1180. return objFile;
  1181. }
  1182. catch(ex)
  1183. {
  1184. ex.description = "OpenTextFile('" + strFileName + "' failed: " + ex.description;
  1185. throw ex;
  1186. }
  1187. } while(true);
  1188. }
  1189. function _FileSystemObject_CopyFile(str, strDst)
  1190. {
  1191. do
  1192. {
  1193. Trace("CopyFile '" + str + "', '" + strDst + "'");
  1194. try
  1195. {
  1196. this.objFS.CopyFile(str, strDst, true);
  1197. break;
  1198. }
  1199. catch(ex)
  1200. {
  1201. if (this.FileExists(strDst))
  1202. {
  1203. Trace(" CopyFile: Attempt to delete " + strDst);
  1204. this.DeleteFile(strDst);
  1205. continue;
  1206. }
  1207. ex.description = "CopyFile('" + str + "', '" + strDst + "' failed: " + ex.description;
  1208. throw ex;
  1209. }
  1210. } while(true);
  1211. }
  1212. function _FileSystemObject_CopyFolder(str, strDst)
  1213. {
  1214. var strName;
  1215. var folder;
  1216. var fc;
  1217. try
  1218. {
  1219. Trace("CopyFolder '" + str + "', '" + strDst + "'");
  1220. this.CreateFolder(strDst);
  1221. folder = this.objFS.GetFolder(str);
  1222. fc = new Enumerator(folder.Files);
  1223. for (; !fc.atEnd(); fc.moveNext())
  1224. {
  1225. strName = String(fc.item());
  1226. this.CopyFile(strName, strDst + "\\" + fc.item().Name);
  1227. }
  1228. fc = new Enumerator(folder.SubFolders);
  1229. for (; !fc.atEnd(); fc.moveNext())
  1230. {
  1231. strName = String(fc.item());
  1232. this.CopyFolder(strName, strDst + "\\" + fc.item().Name);
  1233. }
  1234. }
  1235. catch(ex)
  1236. {
  1237. Trace("CopyFolder('" + str + "', '" + strDst + "' failed: " + ex);
  1238. throw ex;
  1239. }
  1240. }
  1241. function MachineInfo(strName)
  1242. {
  1243. this.strName = strName;
  1244. this.strStatusFile = g_strShareDir + "\\" + strName + ".txt";
  1245. this.fDisabledWriteStatus = false;
  1246. MachineInfo.prototype.ReadStatusInfo = _MachineInfo_ReadStatusInfo;
  1247. MachineInfo.prototype.WriteStatusInfo = _MachineInfo_WriteStatusInfo;
  1248. MachineInfo.prototype.DisableWriteStatusInfo = _MachineInfo_DisableWriteStatusInfo;
  1249. }
  1250. function _MachineInfo_DisableWriteStatusInfo()
  1251. {
  1252. this.fDisabledWriteStatus = true;
  1253. }
  1254. function _MachineInfo_ReadStatusInfo(strText)
  1255. {
  1256. var strText;
  1257. var objFile;
  1258. try
  1259. {
  1260. objFile = g_objFS.OpenTextFile(this.strStatusFile);
  1261. strText = objFile.ReadLine();
  1262. objFile.Close();
  1263. }
  1264. catch(ex)
  1265. {
  1266. strText = "cannot read status file";
  1267. }
  1268. return strText;
  1269. }
  1270. function _MachineInfo_WriteStatusInfo(strText)
  1271. {
  1272. var objFile;
  1273. try
  1274. {
  1275. Trace("WriteStatusInfo(" + strText + ") for " + this.strName);
  1276. if (this.fDisabledWriteStatus)
  1277. {
  1278. LogMsg("Error: Attempting to write status to " + this.strName + " after disabling");
  1279. }
  1280. else
  1281. {
  1282. objFile = g_objFS.CreateTextFile(this.strStatusFile, true);
  1283. objFile.WriteLine(strText);
  1284. objFile.Close();
  1285. }
  1286. }
  1287. catch(ex)
  1288. {
  1289. LogMsg("WriteStatusInfo(" + strText + ") for " + this.strName + " failed: " + ex);
  1290. }
  1291. }
  1292. //*********************************************************************
  1293. //*********************************************************************
  1294. // CallMethod(objInstance, strMethodName, hParameters) //
  1295. // //
  1296. // Call a method on the given object, with the supplied //
  1297. // named parameters. //
  1298. // //
  1299. // Throw if the method returns a non-zero ReturnValue. //
  1300. // Else return the outParams. //
  1301. function CallMethod(objInstance, strMethodName, hParameters)
  1302. {
  1303. try
  1304. {
  1305. Trace("CallMethod " + strMethodName + " " + (hParameters ? hParameters : ''));
  1306. var objMethod = objInstance.Methods_(strMethodName);
  1307. var inParams;
  1308. var outParam;
  1309. if (hParameters)
  1310. {
  1311. if (objMethod.inParameters)
  1312. inParams = objMethod.inParameters.SpawnInstance_();
  1313. }
  1314. var strParamName;
  1315. if (hParameters)
  1316. {
  1317. for(strParamName in hParameters)
  1318. inParams[strParamName] = hParameters[strParamName];
  1319. outParam = objInstance.ExecMethod_(strMethodName, inParams);
  1320. }
  1321. else
  1322. outParam = objInstance.ExecMethod_(strMethodName);
  1323. }
  1324. catch(ex)
  1325. {
  1326. ex.description = "CallMethod " + strMethodName + (hParameters ? hParameters : '()') + " failed " + ex.description;
  1327. throw ex;
  1328. }
  1329. if (outParam.ReturnValue != 0)
  1330. throw new Error(outParam.ReturnValue, "Method " + strMethodName + " failed");
  1331. return outParam;
  1332. }
  1333. function EnumerateSetOfProperties(strSetName, SWBemSet, strIndent)
  1334. {
  1335. var fPrint = false;
  1336. if (!strIndent)
  1337. {
  1338. strIndent = '';
  1339. fPrint = true;
  1340. }
  1341. var strMsg = strIndent + strSetName + "+\n";
  1342. strIndent += " ";
  1343. try
  1344. {
  1345. var enumSet = new Enumerator(SWBemSet);
  1346. for( ; !enumSet.atEnd(); enumSet.moveNext())
  1347. {
  1348. var item = enumSet.item();
  1349. if (item.Properties_ == null)
  1350. {
  1351. if (item.Name != "Name")
  1352. strMsg += strIndent + item.Name + " = " + item.Value + "\n";
  1353. }
  1354. else
  1355. {
  1356. strMsg += EnumerateSetOfProperties(item.Name, item.Properties_, strIndent);
  1357. }
  1358. }
  1359. if (fPrint)
  1360. LogMsg(strMsg);
  1361. }
  1362. catch(ex)
  1363. {
  1364. if (strIndent == " " && SWBemSet.Properties_ != null)
  1365. return EnumerateSetOfProperties(strSetName, SWBemSet.Properties_);
  1366. LogMsg("ERROR: " + strSetName + " " + ex);
  1367. }
  1368. return strMsg;
  1369. }