Sign in to follow this  
Followers 0
zandztrom

While string check?

8 posts in this topic

Hi,

I'm totally new to loops and iterations and stuff so I need some help.

I use _NTservices to stop and start SQLServer and want to check that it's properly stopped before it is started again and if it takes too long it should time out.

How can I do this in an easy way? Should I use a while loop? In that case how is it used to check against a string such as "stopped".

Share this post


Link to post
Share on other sites



Start with something like this:

While 1

$Returned_String=Service_State()

    Select
        
    Case $Returned_String="Stopped"
;Do Somehting
    
    Case $Returned_String="Started"
;Do Somehting
    
    Case Else
    
    EndSelect


Wend


Func Service_State()

;Here you would put in the code that you would use to check the state of a service and make it equal to $String

;And then return the string using "return $String".

EndFunc

Good Luck.

Share this post


Link to post
Share on other sites

Hi,

I'm totally new to loops and iterations and stuff so I need some help.

I use _NTservices to stop and start SQLServer and want to check that it's properly stopped before it is started again and if it takes too long it should time out.

How can I do this in an easy way? Should I use a while loop? In that case how is it used to check against a string such as "stopped".

How are you doing the start/stop of services? NET.exe, WMI, ...?

:)


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

#4 ·  Posted (edited)

How are you doing the start/stop of services? NET.exe, WMI, ...?

:)

@DjDeep00! Thanks for your quick help I'll try that...

@ PsaltyDS. I manage services by using the includes:

NTServices.au3

http://www.autoitscript.com/forum/index.php?showtopic=22165

or

ServiceControl.au3

http://www.autoitscript.com/forum/index.php?showtopic=6487

Would be great to have a equivalence to RunWait() for services ex _StartSvcWait() etc.

Edited by zandztrom

Share this post


Link to post
Share on other sites

@ PsaltyDS. I manage services by using the includes:

NTServices.au3

http://www.autoitscript.com/forum/index.php?showtopic=22165

or

ServiceControl.au3

http://www.autoitscript.com/forum/index.php?showtopic=6487

Would be great to have a equivalence to RunWait() for services ex _StartSvcWait() etc.

You can code your own easily with TimerInit/TimerDiff plus the _ServiceStatus() function.

:)


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

#6 ·  Posted (edited)

You can code your own easily with TimerInit/TimerDiff plus the _ServiceStatus() function.

:)

Hehe, well as you can see from my former question I'm not really a code wizard.

:)

Edited by zandztrom

Share this post


Link to post
Share on other sites

Me either, but this is a shot at how I would do it (not tested):

#include <_NTServices.au3>

; ---------------------------------------------
; Function:     _ServiceStartWait()
; Purpose:  Start a service and wait for service status to be "Running"
; Call with:    _ServiceWait($v_ServiceName, $v_ComputerName = "", $i_Timeout)
;   Where:  $v_ServiceName = string service name
;           $v_ComputerName (optional) = computer to operate on, default is local
;           $i_Timeout (optional) = timeout in seconds, default 0 = never timeout
; On success returns non-zero
; On failure of the service start 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 _ServiceStartWait($v_ServiceName, $v_ComputerName = "", $i_Timeout = 0)
    Local $sStatus = _ServiceStatus ($v_ServiceName, $v_ComputerName)
    If Not @error Then
        If $sStatus = "Running"  Then
            Return 1
        Else
            $sStatus = _ServiceStart ($v_ServiceName, $v_ComputerName)
            If Not @error Then
                Local $iTimer = TimerInit()
                Do
                    If _ServiceStatus ($v_ServiceName, $v_ComputerName) = "Running"  Then Return 1
                    Sleep(250)
                Until $i_Timeout And (TimerDiff($iTimer) > ($i_Timeout * 1000))
                ; Timeout
                Return SetError(-1, 0, 0)
            Else
                ; Error starting service
                Return SetError(@error, @extended, $sStatus)
            EndIf
        EndIf
    Else
        ; Error seeing service
        Return SetError(1, @error, 0)
    EndIf
EndFunc   ;==>_ServiceStartWait

; ---------------------------------------------
; Function:     _ServiceStopWait()
; Purpose:  Stop a service and wait for service status to be "Stopped"
; Call with:    _ServiceWait($v_ServiceName, $v_ComputerName = "", $i_Timeout)
;   Where:  $v_ServiceName = string service name
;           $v_ComputerName = computer to operate on, default is local
;           $i_Timeout = timeout in seconds
; On success returns non-zero
; On failure of the service stop 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 _ServiceStopWait($v_ServiceName, $v_ComputerName = "", $i_Timeout = 0)
    Local $sStatus = _ServiceStatus ($v_ServiceName, $v_ComputerName)
    If Not @error Then
        If $sStatus = "Stopped"  Then
            Return 1
        Else
            $sStatus = _ServiceStop ($v_ServiceName, $v_ComputerName)
            If Not @error Then
                Local $iTimer = TimerInit()
                Do
                    If _ServiceStatus ($v_ServiceName, $v_ComputerName) = "Stopped"  Then Return 1
                    Sleep(250)
                Until $i_Timeout And (TimerDiff($iTimer) > ($i_Timeout * 1000))
                ; Timeout
                Return SetError(-1, 0, 0)
            Else
                ; Error starting service
                Return SetError(@error, @extended, $sStatus)
            EndIf
        EndIf
    Else
        ; Error seeing service
        Return SetError(1, @error, 0)
    EndIf
EndFunc   ;==>_ServiceStopWait

:)


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 had another idea, and combined the two into one function: _ServiceStatusWait(), which will wait for any given status, i.e. "Running" or "Stopped".

:)


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

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