Winscripter
  WSH
  Forums
  Downloads
  Books
  Links
  Amazon





© winscripter.com
1998-2004







Script: Check State of Service

Posted by on Sunday, March 21, 2004 (PST)

Script: Checks the state of a local or remote service (running, stopped, etc...) Uses WMI Win32_Service.

 

/** 
*    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 );
}

Comments:

the remote server machine does not exist or is unavailable
By maxcronjob on Wednesday, December 22, 2004 (PST)

I keep trying to get this script to work, but I keep getting either

 

Error:

 

or,

 

Error:  The remote server machine does not exist or is unavailable.

 

 

I'm running this on my local XP box, just to see how it works.  I'm a scripting newbie, so I'm flailing.

 

thanks,

 

max

 

 

Reply to this Comment

Wrong code
By default on Wednesday, June 29, 2005 (PST)
You must write: CheckService( "localhost", "Messenger" ); for proper work... I realy don't know why author wrote there something like "XP" and "Win2K". There must be computer name or just localhost for current machine.

Reply to this Comment

Add Your Comment



WSH and ADSI Administrative Scripting

New Articles
  • List installed COM objects and associated ProgIDs
    Script: Lists all COM Objects and their associated ProgIDs (If available). Win32_ClassicCOMClassSetting

  • Script: File Rotator
    Script: Rotate files where the most current file has the lowest number in the archive. When files exceed the retention period, they are deleted. Typically used for log files, backups, etc..

  • Script: Create IIS Website and DNS record
    Script: Dan Casier sent me this script that will create a website and appropriate DNS record. The script is intended for Windows 2000 Server with local DNS and necessary DNS mof installed.


  • Winscripter   |  WSH   |  Forums   |  Downloads   |  Books   |  Links   |  Amazon