Sign in to follow this  
Followers 0
CatchFish

Start, Stop, Pause and get status of NT Services

18 posts in this topic

#1 ·  Posted (edited)

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)

_NTServices.au3

It provides four major functions: _ServiceStatus(), _ServicePause(), _ServiceStart(), _ServiceStop().

For example, status of IIS Administration Service on local machine can be retrieved like this:

$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:

$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:

$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.

Edited by CatchFish

Share this post


Link to post
Share on other sites



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")

Share this post


Link to post
Share on other sites

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:

$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.

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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 :lmao:

Share this post


Link to post
Share on other sites

Who else would I be?

Share this post


Link to post
Share on other sites

Hehe why are thou coughing this-is-me?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

EDIT: Have you ever seen Chicken Little?

Edited by this-is-me

Who else would I be?

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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) :lmao:

Edited by frodo

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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

Edited by this-is-me

Who else would I be?

Share this post


Link to post
Share on other sites

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...

; ---------------------------------------------
; 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".

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Might want to look at Admin Tool from my sig

HI There,

Thanks for this code ,I have same requirement but that is for XP.

can I use the same code on professional XP to start and pause running processeds.

Thanks

Chetan J.

Share this post


Link to post
Share on other sites

Hi,

I fixed some bugs in _ServiceStatus:

1) When it didn't find a service, it returned a number, rather than a string. This makes the comparison logic too complex. Now it would return an empty string.

2) When it was used from within a restricted privileges account (quite normal in 64-bits operating systems), it tried to query services with too high privileges, making the function fail. Now it uses the minimum privileges needed to carry out its task.

You can find the updated file in the attachment.

_NTServices.au3

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Hi, I use _NTServices.au3 succesfully, but when I reboot it fail !!!

Edited by IsMaelhIT

Share this post


Link to post
Share on other sites

Just as an info:

I used this UDF in a loop to query the status of services and my code created with this UDF a memory leak adding something like 4k on each iteration.

Maybe this is related to my code or OS (Win7/Win2008R2) but I just want to note it here.

I got no example code to demonstrate this.

Switching to solved my problem. Now working fine without a leak.

I do not blame this UDF directly but just in case someone finds a similar problem, I already raised my hand.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0