Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for cpuloadtd.js

(view source code of cpuloadtd.js as plain text)

  1. // Define constants and variables
  2. var wbemFlagReturnImmediately = 0x10;
  3. var wbemFlagForwardOnly       = 0x20;
  4. var jsCrLf                    = String.fromCharCode( 13, 10 );
  5. var jsTab                     = String.fromCharCode(      9 );
  6. var jsQuote                   = String.fromCharCode(     34 );
  7. var strMsg                    = "";
  8.  
  9. // Check command line parameters
  10. objArgs = WScript.Arguments;
  11. // Default is local computer
  12. if ( objArgs.length == 0 ) {
  13. 	// Check local computer name
  14. 	try {
  15. 		var objWMIService0 = GetObject( "winmgmts://./root/cimv2" );
  16. 	}
  17. 	catch( error ) {
  18. 		strMsg = jsCrLf + "Error # " + error + jsCrLf;
  19. 		Syntax( );
  20. 	}
  21. 	finally {
  22. 		try {
  23. 			var colItems0 = objWMIService0.ExecQuery( "SELECT * FROM Win32_ComputerSystem", null, 48 );
  24. 		}
  25. 		catch( error ) {
  26. 			strMsg = jsCrLf + "Error # " + error + jsCrLf;
  27. 			Syntax( );
  28. 		}
  29. 		finally {
  30. 			var enumItems0 = new Enumerator( colItems0 );
  31. 			for ( ; !enumItems0.atEnd(); enumItems0.moveNext() ) {
  32. 				objItem = enumItems0.item( );
  33. 				strComputer = objItem.Name
  34. 			}
  35. 		}
  36. 	}
  37. }
  38. // Single argument, either computer name or /?
  39. if ( objArgs.length == 1 ) {
  40. 	strComputer = objArgs(0).valueOf( ).toUpperCase( );
  41. 	if ( strComputer.indexOf( "?" ) > 0 ) Syntax( );
  42. 	if ( strComputer.indexOf( "/" ) > 0 ) Syntax( );
  43. }
  44. // No more than 1 argument allowed
  45. if ( objArgs.length >  1 ) Syntax( );
  46.  
  47.  
  48. // Header line for screen output
  49. strMsg = jsCrLf + "Computer Name" + jsTab + "CPU ID" + jsTab + "Load Percentage" + jsCrLf
  50.  
  51. // Connect to specified computer
  52. try {
  53. 	var objWMIService = GetObject( "winmgmts://" + strComputer + "/root/CIMV2" );
  54. }
  55. catch( error ) {
  56. 	strMsg = jsCrLf + "Error # " + error + jsCrLf;
  57. 	Syntax( );
  58. }
  59. finally {
  60. 	// Query processor properties
  61. 	try {
  62. 		var colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_Processor", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly );
  63. 	}
  64. 	catch( error ) {
  65. 		strMsg = jsCrLf + "Error # " + error + jsCrLf;
  66. 		Syntax( );
  67. 	}
  68. 	finally {
  69. 		var enumItems = new Enumerator(colItems);
  70. 		for ( ; !enumItems.atEnd(); enumItems.moveNext( ) ) {
  71. 			var objItem = enumItems.item( );
  72. 			strMsg += strComputer + jsTab + objItem.DeviceID + jsTab + objItem.LoadPercentage + jsCrLf + jsCrLf;
  73. 		}
  74. 	}
  75. }
  76.  
  77. WScript.Echo( strMsg );
  78.  
  79. // Done
  80. WScript.Quit(0);
  81.  
  82.  
  83. function Syntax( ) {
  84. 	strMsg += jsCrLf + "CPULoadTD.js,  Version 1.00 for WSH 1.0" + jsCrLf;
  85. 	strMsg += "Display CPU load percentage for each processor on any computer on the network";
  86. 	strMsg += jsCrLf + "in tab delimited format";
  87. 	strMsg += jsCrLf + jsCrLf;
  88. 	strMsg += "Usage:  CSCRIPT  //NoLogo  CPULOADTD.JS  [ computername ]";
  89. 	strMsg += jsCrLf + jsCrLf;
  90. 	strMsg += "Where:  " + jsQuote + "computer_name" + jsQuote;
  91. 	strMsg += " is the optional name of a remote" + jsCrLf;
  92. 	strMsg += "        computer (default is local computer ";
  93. 	strMsg += "name)" + jsCrLf + jsCrLf;
  94. 	strMsg += "Written by Rob van der Woude" + jsCrLf;
  95. 	strMsg += "http://www.robvanderwoude.com" + jsCrLf + jsCrLf;
  96. 	strMsg += "Created with Microsoft's Scriptomatic 2.0 tool" + jsCrLf;
  97. 	strMsg += "http://www.microsoft.com/downloads/details.aspx?" + jsCrLf;
  98. 	strMsg += "    FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178+DisplayLang=en" + jsCrLf;
  99. 	WScript.Echo( strMsg );
  100. 	WScript.Quit(1);
  101. }
  102.  

page last uploaded: 2017-04-06, 13:33