CatchFish Posted February 24, 2006 Share Posted February 24, 2006 (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.au3It 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 March 5, 2006 by CatchFish mLipok 1 Link to comment Share on other sites More sharing options...
GaryFrost Posted February 24, 2006 Share Posted February 24, 2006 Might want to look at Admin Tool from my sig SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference. Link to comment Share on other sites More sharing options...
Josbe Posted February 24, 2006 Share Posted February 24, 2006 I found useful this info. Thanks CatchFish. AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta Link to comment Share on other sites More sharing options...
Bertman Posted March 1, 2006 Share Posted March 1, 2006 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 More sharing options...
CatchFish Posted March 5, 2006 Author Share Posted March 5, 2006 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 More sharing options...
CatchFish Posted March 5, 2006 Author Share Posted March 5, 2006 Might want to look at Admin Tool from my sigMagnificent! 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... Link to comment Share on other sites More sharing options...
frodo Posted October 13, 2006 Share Posted October 13, 2006 I found this services UDF was far more refined than the one i had been working on for ages, so first of all thanks @CatchFishIn 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 herePlease 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 Link to comment Share on other sites More sharing options...
this-is-me Posted October 13, 2006 Share Posted October 13, 2006 *Cough* Who else would I be? Link to comment Share on other sites More sharing options...
frodo Posted October 15, 2006 Share Posted October 15, 2006 *Cough*Hehe why are thou coughing this-is-me? Link to comment Share on other sites More sharing options...
this-is-me Posted October 15, 2006 Share Posted October 15, 2006 (edited) Some responses are better made with gutteral noises instead of words... EDIT: Have you ever seen Chicken Little? Edited October 15, 2006 by this-is-me Who else would I be? Link to comment Share on other sites More sharing options...
frodo Posted October 16, 2006 Share Posted October 16, 2006 (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 areservice functions at the post you linked to, but mine have things likeauto conversion from displayname to internalname and autoretry, andwere written for my own use for my new Ghost 9 tray app that replacesNortons 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, whichmeans 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) Edited October 16, 2006 by frodo Link to comment Share on other sites More sharing options...
this-is-me Posted October 16, 2006 Share Posted October 16, 2006 (edited) EDIT EDIT EDIT: Nevermind what I was saying. It turned out I was completely off my rocker Edited October 16, 2006 by this-is-me Who else would I be? Link to comment Share on other sites More sharing options...
PsaltyDS Posted September 14, 2007 Share Posted September 14, 2007 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 ;==>_ServiceStatusWaitSo 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 More sharing options...
MePHiTiC Posted September 21, 2007 Share Posted September 21, 2007 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 More sharing options...
iameaten Posted February 15, 2010 Share Posted February 15, 2010 Might want to look at Admin Tool from my sigHI 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. Link to comment Share on other sites More sharing options...
kasty Posted May 31, 2012 Share Posted May 31, 2012 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 More sharing options...
IsMaelhIT Posted June 27, 2012 Share Posted June 27, 2012 (edited) Hi, I use _NTServices.au3 succesfully, but when I reboot it fail !!! Edited June 27, 2012 by IsMaelhIT Link to comment Share on other sites More sharing options...
Tankbuster Posted March 20, 2013 Share Posted March 20, 2013 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 More sharing options...
ur Posted November 26, 2018 Share Posted November 26, 2018 A suggestion, $STANDARD_RIGHTS_REQUIRED is conflicting with pre-defined variables. Link to comment Share on other sites More sharing options...
BrewManNH Posted November 26, 2018 Share Posted November 26, 2018 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 GudeHow 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now