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.

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