/**
* Script: CheckService.js
* Purpose: Check the current state of a service local or remote.
* Author: Daren Thiel
* Date: 21 Mar 2004
* Web: http://www.winscripter.com
* Note: Must be run under an account with sufficient
* permissions.
**/
CheckService( "XP", "Messenger" );
CheckService( "Win2K", "Messenger" );
// Check state of a service
function CheckService( computer, service )
{
try
{
// Connect to machine with WMI
var wmistr = "winmgmts:{impersonationLevel=impersonate}!\\\\";
var wmi = GetObject( wmistr + computer + "\\root\\cimv2" );
// Execute query looking for the DisplayName of the service.
// Look in the services mmc if you don't know it.
var query = "SELECT * FROM Win32_Service WHERE DisplayName='" + service + "'";
var svc = wmi.ExecQuery( query );
// enumerate and loop through the results - we should have only one.
var esvc = new Enumerator( svc );
for( ; !esvc.atEnd(); esvc.moveNext() )
{
var isvc = esvc.item();
// Format and print the display name and state
print( padx( isvc.DisplayName, 15 ) + "\t [" + isvc.State + "]" );
}
}
catch( e )
{
print( "Error: " + e.description );
}
}
// Print
function print( msg )
{
WScript.Echo( msg );
}
// Pad with trailing spaces
function padx( msg, size )
{
var smsg = String( msg );
while( smsg.length < size )
smsg += " ";
return( smsg );
}