(view source code of cpuload.js as plain text)
// Define constants and variables
var wbemFlagReturnImmediately = 0x10;
var wbemFlagForwardOnly = 0x20;
var jsCrLf = String.fromCharCode( 13, 10 );
var jsQuote = String.fromCharCode( 34 );
var strMsg = "";
// Check command line parameters
objArgs = WScript.Arguments;
// Default is local computer
if ( objArgs.length == 0 ) {
// Check local computer name
try {
var objWMIService0 = GetObject( "winmgmts://./root/cimv2" );
}
catch( error ) {
strMsg = jsCrLf + "Error # " + error + jsCrLf;
Syntax( );
}
finally {
try {
var colItems0 = objWMIService0.ExecQuery( "SELECT * FROM Win32_ComputerSystem", null, 48 );
}
catch( error ) {
strMsg = jsCrLf + "Error # " + error + jsCrLf;
Syntax( );
}
finally {
var enumItems0 = new Enumerator( colItems0 );
for ( ; !enumItems0.atEnd(); enumItems0.moveNext() ) {
objItem = enumItems0.item( );
strComputer = objItem.Name
}
}
}
}
// Single argument, either computer name or /?
if ( objArgs.length == 1 ) {
strComputer = objArgs(0).valueOf( ).toUpperCase( );
if ( strComputer.indexOf( "?" ) > 0 ) Syntax( );
if ( strComputer.indexOf( "/" ) > 0 ) Syntax( );
}
// No more than 1 argument allowed
if ( objArgs.length > 1 ) Syntax( );
// Header line for screen output
strMsg = jsCrLf + "CPU load percentage for " + strComputer + ":" + jsCrLf + jsCrLf;
// Connect to specified computer
try {
var objWMIService = GetObject( "winmgmts://" + strComputer + "/root/CIMV2" );
}
catch( error ) {
strMsg = jsCrLf + "Error # " + error + jsCrLf;
Syntax( );
}
finally {
// Query processor properties
try {
var colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_Processor", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly );
}
catch( error ) {
strMsg = jsCrLf + "Error # " + error + jsCrLf;
Syntax( );
}
finally {
var enumItems = new Enumerator(colItems);
for ( ; !enumItems.atEnd(); enumItems.moveNext( ) ) {
var objItem = enumItems.item( );
strMsg += "Device ID : " + objItem.DeviceID + jsCrLf;
strMsg += "Load Percentage : " + objItem.LoadPercentage + jsCrLf + jsCrLf;
}
}
}
WScript.Echo( strMsg );
// Done
WScript.Quit(0);
function Syntax( ) {
strMsg += jsCrLf + "CPULoad.js, Version 1.02 for WSH 1.0" + jsCrLf;
strMsg += "Display CPU load percentage for each processor on any computer on the network";
strMsg += jsCrLf + jsCrLf;
strMsg += "Usage: CSCRIPT //NoLogo CPULOAD.JS [ computername ]";
strMsg += jsCrLf + jsCrLf;
strMsg += "Where: " + jsQuote + "computer_name" + jsQuote;
strMsg += " is the optional name of a remote" + jsCrLf;
strMsg += " computer (default is local computer ";
strMsg += "name)" + jsCrLf + jsCrLf;
strMsg += "Written by Rob van der Woude" + jsCrLf;
strMsg += "http://www.robvanderwoude.com" + jsCrLf + jsCrLf;
strMsg += "Created with Microsoft's Scriptomatic 2.0 tool" + jsCrLf;
strMsg += "http://www.microsoft.com/downloads/details.aspx?" + jsCrLf;
strMsg += " FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178+DisplayLang=en" + jsCrLf;
WScript.Echo( strMsg );
WScript.Quit(1);
}
page last modified: 2024-04-16; loaded in 0.0070 seconds