Jump to content

Start, Stop, Pause and get status of NT Services


CatchFish
 Share

Recommended Posts

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
Link to comment
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")

Link to comment
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.

Link to comment
Share on other sites

  • 7 months later...

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:

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 10 months later...

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
Link to comment
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

Link to comment
Share on other sites

  • 2 years later...
  • 2 years later...

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

Link to comment
Share on other sites

  • 4 weeks later...
  • 8 months later...

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.

Link to comment
Share on other sites

  • 5 years later...

I'm not sure if you realize but, this post is from 12 years ago. I'm sure things have changed greatly in the past dozen years since this was posted. 

Using such old code, you have to know what it's doing and if it doesn't work, how to fix it yourself. The OP hasn't been here in over a decade, so he's not going to help you with it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...