#40791 - 2003-05-27 09:46 PM
check if a service is running
jhansenjr
Fresh Scripter
Registered: 2002-08-31
Posts: 37
Loc: New Jersey
How can I tell if a service is running? I tried several of the examples posted but none seem to work correctly. Need to check if "Norton AntiVirus Server" is running. Thanks
Top
#40793 - 2003-05-27 10:07 PM
Re: check if a service is running
jhansenjr
Fresh Scripter
Registered: 2002-08-31
Posts: 37
Loc: New Jersey
Those examples seem a little much for a starter.
Top
#40794 - 2003-05-27 10:10 PM
Re: check if a service is running
Allen
KiX Supporter
Registered: 2003-04-19
Posts: 4545
Loc: USA
Although not elegant and does little else, this will tell you if the service is running... code: function servicestate($_service,optional $remotepc) $objects=GetObject("winmgmts:{impersonationLevel=impersonate}!"+$remotepc+"\root\cimv2") if not @error=0 exit @error endif $services=$objects.ExecQuery('Select * from Win32_Service WHERE Name = "$_service"') for each $service in $services $servicestate=$service.state next endfunction For example: servicestate("Spooler") returns "Running" Just threw this together so please be understanding if it contains a bug or two, but it did work in my simple test above. [ 27. May 2003, 22:11: Message edited by: Al_Po ]
Top
#40797 - 2003-05-27 10:45 PM
Re: check if a service is running
jhansenjr
Fresh Scripter
Registered: 2002-08-31
Posts: 37
Loc: New Jersey
I see all the approaches but I'm having difficulty following them. I guess I will try to figure it out on my own.
Top
#40798 - 2003-05-27 10:48 PM
Re: check if a service is running
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
give me a chance to get home and I will walk you through an example. About 5:30 PM EST I will be back online.
Top
#40800 - 2003-05-27 11:49 PM
Re: check if a service is running
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
I will be on and off the forum. Let me know if you want to discuss an example.
Top
#40801 - 2003-05-27 11:52 PM
Re: check if a service is running
jhansenjr
Fresh Scripter
Registered: 2002-08-31
Posts: 37
Loc: New Jersey
Sure. I'm looking at one called active service.
Top
#40803 - 2003-05-28 12:09 AM
Re: check if a service is running
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
ActiveServive(Service) is nothing more than a wrapper for using XNET.exe which is a utility in your KiXtart distribution. Read the XNET help file. Then look at this code again. It just calls XNET, reports the return and returns a value for NT. The @WIN = 1 means NT. The else would be for Win9x computers and it checks the registry for Win9x services.
Top
#40804 - 2003-05-28 12:15 AM
Re: check if a service is running
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Check out fADSIServiceRun(). This code uses ADSI via COM to work with services. In it is simple code that will show how to get the status of a service. code: $objService = Getobject("WinNT://$Computer/$Service,Service") if @error = 0 $status = $objService.status ;$status = 4 ;Running ;$status = 1 ;Stopped ;$status = 7 ;Paused endif [ 28. May 2003, 00:16: Message edited by: Howard Bullock ]
Top
#40805 - 2003-05-28 12:16 AM
Re: check if a service is running
jhansenjr
Fresh Scripter
Registered: 2002-08-31
Posts: 37
Loc: New Jersey
Jochen, thanks. That works fine. Howard, I understand the points that you brought up but what I didn't underdstand was the $servicename. Where is that being declared? Above this script? Thanks
Top
#40806 - 2003-05-28 12:23 AM
Re: check if a service is running
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Usage: $active=ACTIVESERVICE("WINS") code: function activeservice($servicename) Dim $shellcommand, $xnetexe $activeservice=0 $servicename=trim($servicename) if $servicename='' exit 87 endif if not isdeclared($TOOLSDIR) global $TOOLSDIR endif ... $servicename in the above example is "WINS" which was passed into the function when used in the script. The function definition specified what parameters are to be accepted by a function. These parms can be mandatory or optional. See the FUNCTION command in the docs. [ 28. May 2003, 00:24: Message edited by: Howard Bullock ]
Top
#40807 - 2003-05-28 12:34 AM
Re: check if a service is running
jhansenjr
Fresh Scripter
Registered: 2002-08-31
Posts: 37
Loc: New Jersey
Thanks Howard. I appreciate your help.
Top
#40808 - 2003-05-28 12:45 AM
Re: check if a service is running
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Top
#40809 - 2003-05-28 12:47 AM
Re: check if a service is running
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11623
Loc: CA
Here is a version tailored to what you want to find. Reading and Experimenting will help increase your knowledge as well. When you have time please review the KiXtart manual as well as the FAQ section here on the board. You can also download the HELPFILE from my site (the link is in my signature) From a DOS prompt on either Windows 2000 or XP : Using KiXtart v4.21NOTICE: The remote system must have WMI installed. 2000/XP have it by default, NT 4 does not. Please review this FAQ for further information on this.http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=10;t=000048 C:\SCRIPTS\KIX32.EXE CHECKNAV.KIX$strComputer = "NameOfComputerToCheck"
$Proc = "Norton Antivirus Server"
BREAK ON
ListProc ($strComputer , $Proc )
FUNCTION ListProc ($strComputer ,$Proc )
For each $Process in GetObject ("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2" ).ExecQuery ("select * from Win32_Service where Name='$Proc'" )
? $PROC +' on ' +$strComputer +' is ' +$Process.state
Next
ENDFUNCTION [ 28. May 2003, 00:56: Message edited by: NTDOC ]
Top
#40810 - 2003-05-28 12:49 AM
Re: check if a service is running
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Long line police. You're busted DOC
Top
Moderator: Jochen , Allen , Radimus , Glenn Barnas , ShaneEP , Ruud van Velsen , Arend_ , Mart
0 registered
and 557 anonymous users online.