AutoIt Forums: Start, Stop, Pause and get status of NT Services - AutoIt Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Start, Stop, Pause and get status of NT Services Using Win32 API in AutoIt script Rate Topic: -----

#1 User is offline   CatchFish 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 42
  • Joined: 06-August 05

Posted 24 February 2006 - 05:46 PM

You can now start, stop, pause and get status of an NT service by including this .au3: (needs AutoIt beta v3.1.1.110 or above)
Attached File  _NTServices.au3 (9.8K)
Number of downloads: 2060
It provides four major functions: _ServiceStatus(), _ServicePause(), _ServiceStart(), _ServiceStop().

For example, status of IIS Administration Service on local machine can be retrieved like this:
[ code='text' ]    ( Popup )
$status = _ServiceStatus("IISAdmin")

According to the service itself, $status could be one of these:
  • "Stopped"
  • "Start Pending"
  • "Stop Pending"
  • "Running"
  • "Coninue Pending"
  • "Pause Pending"
  • "Paused"
If it fails to get information, the result is 0 and @error is non-zero.


You can also specify a computer name (or an IP address) in any of these functions, like this:
[ code='text' ]    ( Popup )
$result = _ServicePause("IISAdmin", "CatchFishComputer")

If succeeds, it returns 1. Otherwise it returns 0 and sets @error to a non-zero number. The same to _ServiceStart() & _ServiceStop(). Note that controlling remote NT services needs certain privilege through an IPC connection. (See man page of console command "net use" for more information.)


Because these four functions only recognize "internal" service names, so you may need the helper function, _ToInternalServiceName(), to translate the display names that you see in Services Manager. You may do the job in this way:
[ code='text' ]    ( Popup )
$result = _ServiceStop( _ToInternalServiceName("Task Scheduler") )

The function above scans the registry to retrieve information. Thus it takes unnecessary time for the convenience. I suggest that you get the internal service name through _ToInternalServiceName() in advance, and then hard-coded that name in the script, if possible.

That's it! Have fun.

This post has been edited by CatchFish: 05 March 2006 - 08:15 AM


#2 User is offline   GaryFrost 

  • I don't need your attitude. I have one of my own
  • Icon
  • Group: Developers(Dev)
  • Posts: 7,852
  • Joined: 01-June 04
  • Gender:Male
  • Location:USA

Posted 24 February 2006 - 05:52 PM

Might want to look at Admin Tool from my sig

#3 User is offline   Josbe 

  • Infrequent ghost ☺
  • PipPipPipPipPipPip
  • Group: Full Members
  • Posts: 1,585
  • Joined: 28-January 04
  • Gender:Male

Posted 24 February 2006 - 06:32 PM

I found useful this info. Thanks CatchFish.

#4 User is offline   Bertman 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 24
  • Joined: 08-February 06

Posted 01 March 2006 - 01:03 PM

Hello,

I realy would like to use this script, but I get an error:

NTServices.au3 (133) : ==> "Case" statement with no matching "Select" statement.:
Case $SERVICE_STOPPED

I use this line: $status = _ServiceStatus("Ati HotKey Poller")

#5 User is offline   CatchFish 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 42
  • Joined: 06-August 05

Posted 05 March 2006 - 07:01 AM

View PostBertman, on Mar 1 2006, 04:03 AM, said:

Hello,

I realy would like to use this script, but I get an error:

NTServices.au3 (133) : ==> "Case" statement with no matching "Select" statement.:
Case $SERVICE_STOPPED

I use this line: $status = _ServiceStatus("Ati HotKey Poller")

You'll need the latest beta version of AutoIt 3. I'm using v3.1.1.110 (beta) and it works like a charm.
Get beta version

Also you might want to try this:
[ code='text' ]    ( Popup )
$status = _ServiceStatus(_ToInternalServiceName("Ati HotKey Poller"))

Because "Ati HotKey Poller" doesn't seem like an internal name of a service, it needs to be translated. But if you find $status = 0 (meaning there's an error), just use the orginal statement instead.

#6 User is offline   CatchFish 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 42
  • Joined: 06-August 05

Posted 05 March 2006 - 08:26 AM

View Postgafrost, on Feb 24 2006, 08:52 AM, said:

Might want to look at Admin Tool from my sig

Magnificent! WMI works perfectly. Retrieves more information than APIs in an easier way. And the code is much shorter. However error handling of COM is always a pain...

#7 User is offline   frodo 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 37
  • Joined: 24-October 05

Posted 13 October 2006 - 10:16 AM

I found this services UDF was far more refined than the one i had been working on for ages, so first of all thanks @CatchFish

In the interest of sharing, i whacked together a reusable function to do starting/stopping with feedback making use of the UDF, so here it is, feel free to (as the CatchFish did in his post) customise/mangle it to your needs, it is after all a basic template.


Download here

Please rename the .txt extension to .au3, i would have posted it in the forum, but the formatting i used under AutoIT at home looks crap in this window :)

#8 User is offline   this-is-me 

  • Pursuer of obscure functionality
  • PipPipPipPipPipPipPip
  • Group: Full Members
  • Posts: 2,370
  • Joined: 02-April 04

Posted 13 October 2006 - 02:22 PM

*Cough*

#9 User is offline   frodo 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 37
  • Joined: 24-October 05

Posted 15 October 2006 - 04:08 AM

View Postthis-is-me, on Oct 13 2006, 11:22 PM, said:



Hehe why are thou coughing this-is-me?

#10 User is offline   this-is-me 

  • Pursuer of obscure functionality
  • PipPipPipPipPipPipPip
  • Group: Full Members
  • Posts: 2,370
  • Joined: 02-April 04

Posted 15 October 2006 - 07:06 AM

Some responses are better made with gutteral noises instead of words...





EDIT: Have you ever seen Chicken Little?

This post has been edited by this-is-me: 15 October 2006 - 07:07 AM


#11 User is offline   frodo 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 37
  • Joined: 24-October 05

Posted 16 October 2006 - 05:26 AM

View Postthis-is-me, on Oct 15 2006, 04:06 PM, said:

Some responses are better made with gutteral noises instead of words...
EDIT: Have you ever seen Chicken Little?


Umm, no.....

p.s. i only just noticed your *cough* was a link. Took a look...yeah they are
service functions at the post you linked to, but mine have things like
auto conversion from displayname to internalname and autoretry, and
were written for my own use for my new Ghost 9 tray app that replaces
Nortons own (1/5 the size & half the memory) for my PE plugin (and beyond),
although im still peeved at Norton for not using a standard statusbar control, which
means i can only grab the 1st segment for use in the tray.....would have been nice to get the % done.....
.
Im just sharing the outcome of my tinkering.....my stuff was original
(well except for the _NTServices dependency) :)

This post has been edited by frodo: 16 October 2006 - 05:32 AM


#12 User is offline   this-is-me 

  • Pursuer of obscure functionality
  • PipPipPipPipPipPipPip
  • Group: Full Members
  • Posts: 2,370
  • Joined: 02-April 04

Posted 16 October 2006 - 06:48 AM

EDIT EDIT EDIT: Nevermind what I was saying. It turned out I was completely off my rocker

This post has been edited by this-is-me: 16 October 2006 - 06:54 AM


#13 User is offline   PsaltyDS 

  • Most Venerable Penguin
  • Icon
  • Group: AutoIt MVPs(MVP)
  • Posts: 10,227
  • Joined: 12-December 05
  • Gender:Male
  • Location:Broke down in Troy crater; AAA won't return my calls...

Posted 14 September 2007 - 04:44 PM

If there is anybody still maintaining this, I made two functions _ServiceStartWait() and _ServiceStopWait() for a forum post demo. They are not tested yet, but at least the idea might be useful.

They could be improved by integrating the dll calls so they didn't have to be created from scratch for each loop of _ServiceStatus().

It might even be better to integrate the two into one function _ServiceStatusWait() that waits for a parameter status... hmmm...

[ autoIt ]    ( ExpandCollapse - Popup )
; --------------------------------------------- ; Function:     _ServiceStatusWait() ; Purpose:  Waits for service status to match the given parameter. ; Call with:    _ServiceStatusWait($v_ServiceName, $v_ComputerName = "", $s_Status = "Running", $i_Timeout = 0) ;   Where:  $v_ServiceName = string service name ;           $v_ComputerName = computer to operate on, default is local ;           $s_Status (optional) = status string to wait for, default is "Running" ;           $i_Timeout = timeout in seconds, default 0 = never timeout ; On success returns 1 ; On failure of the service status request returns 0 and sets @error and @extended ; On timeout returns 0 and @error = -1 ; Notes: The service name is the 'internal' name, not necessarily the 'display' ;           name seen in the GUI manager.  Use _ToInternalServiceName() to translate ;           display name to internal. ; Requires: _NTServices.au3 ; --------------------------------------------- Func _ServiceStatusWait($v_ServiceName, $v_ComputerName = "", $s_Status = "Running", $i_Timeout = 0)     Local $sStatus = _ServiceStatus ($v_ServiceName, $v_ComputerName)     If Not @error Then         If $sStatus = $s_Status Then             Return 1         Else             Local $iTimer = TimerInit()             Do                 Sleep(250)                 If _ServiceStatus ($v_ServiceName, $v_ComputerName) = $s_Status Then Return 1             Until $i_Timeout And (TimerDiff($iTimer) > ($i_Timeout * 1000))             ; Timeout             Return SetError(-1, 0, 0)         EndIf     Else         ; Error seeing service         Return SetError(@error, @extended, 0)     EndIf EndFunc   ;==>_ServiceStatusWait


So the call could be _ServiceStatusWait("Spooler", "", "Running", 5) to wait a maximum of 5 seconds for the Spooler service status to be "Running".

:)

#14 User is offline   MePHiTiC 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 51
  • Joined: 20-December 06
  • Location:Sin City

Posted 21 September 2007 - 03:41 PM

I've got this working on default Windows services such as Task Scheduler, Spooler, etc ... However if I try it on 3rd Party services I'm returned with "0". I checked the reg to hard code the service name, and nothing appears to work for third party services. I've tried several.

Can anyone else verify this or possibly shade some light on this? I'm trying to check a 3rd party service to see if it's running.

Thanks again!
MePH

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users