Nunos Posted June 21, 2017 Posted June 21, 2017 (edited) I am trying to learn how to take advantage of the return values in functions but I don't understand how they work or how I could use them for say error catching in a script. Below is a function that was made by engine and I want to learn how to use the return values to see if the operation was a success or not. ; Name...........: _Service_Start ; Description ...: Starts a service. ; Syntax.........: _Service_Start($sServiceName [, $sComputerName]) ; Parameters ....: $sServiceName - Name of the service. ; $sComputerName - [Optional] The name of the target computer. The local computer is default. ; Requirement(s).: Administrative rights on the target computer. ; Return values .: Success - 1 ; Failure - 0 ; Sets @error ; Author ........: engine ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: So in my script I test to see if a service is running or not and if it is not then I use the service start to get it to run. Local $sServiceName = "Audiosrv" Func ServiceStatus() MsgBox(0, "Checking", "Checking the status of the service.") Select Case $aiStatus[1] = $SERVICE_STOPPED _Service_Start($sServiceName) Case $aiStatus[1] = $SERVICE_RUNNING _Service_Resume($sServiceName) EndSelect EndFunc The function says it returns a value of 1 for Success and 0 for Failure so I am wondering how or if I could use that to make sure that whatever action I performed on the service actually worked and if not maybe display a msgbox that states that. Thank you in advance for your time and patience. Edited June 21, 2017 by Nunos Highlighted the part I am trying to learn to use.
Floops Posted June 21, 2017 Posted June 21, 2017 (edited) You have to assign the return value to a variable. Then you can use the variable for whatever you want. $iReturn = _Service_Start($sServiceName) If $iReturn = 1 Then ; Success, do something now ElseIf $iReturn = 0 Then ; Failure, do something else EndIf In this case the function passes its return value to the variable $iReturn. Since 1 means success and 0 failure you can easily check if the function failed or not by checking the variable. Hope this helps! Edited June 21, 2017 by Floops
Moderators JLogan3o13 Posted June 21, 2017 Moderators Posted June 21, 2017 Just to clarify, you do not have to assign the return to a variable, you can work with it directly: MsgBox(0, "Result", "Function result was " & ((_Service_Start("AxInstSV") = 0) ? "failure" : "successful")) "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
Nunos Posted June 21, 2017 Author Posted June 21, 2017 (edited) Thank you for the clarification. I will test tonight after work. One question what is AxInstSV ? I have never seen that before. Would I need to change that to the name of the service I am testing it on or perhaps the variable I stored the service name in? Edited June 21, 2017 by Nunos
anthonyjr2 Posted June 21, 2017 Posted June 21, 2017 2 minutes ago, Nunos said: Would I need to change that to the name of the service I am testing it on or perhaps the variable I stored the service name in? Yes that's what you need to do. UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=
Nunos Posted June 21, 2017 Author Posted June 21, 2017 MsgBox(0, "Result", "Function result was " & ((_Service_Start($sServiceName) = 0) ? "failure" : "successful")) Does that look correct?
Moderators JLogan3o13 Posted June 21, 2017 Moderators Posted June 21, 2017 AxnstSV was just a service I grabbed as an example, it is the ActiveX installer service. And yes, you would simply change the service name to the one you want to start/stop. "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
Moderators JLogan3o13 Posted June 21, 2017 Moderators Posted June 21, 2017 (edited) @Nunos Try it Does it work as you expect? Edited June 21, 2017 by JLogan3o13 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
Nunos Posted June 21, 2017 Author Posted June 21, 2017 Sorry you guys are going faster than me. I tested and it seems to work. I will test at home tonight after work on the computer with the actual service I am trying to check it is an sql instance on a server that after reboots doesn't start fast enough. Thank you all for your help.
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