Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for cpuload.js

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

page last modified: 2024-04-16; loaded in 0.0070 seconds