Jump to content

How To Start/stop/disable A Service


Recommended Posts

I need to write a script to stop a service running on a pc and set it to disabled. Is this possible?

if so what are the commands. I tried to look it up in the help file but couldn't find it.

Hi,

maybe _RunDos(....

command net start or net stop ...

could help

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

ProcessClose("cgasvc.exe.exe")

$PID = ProcessExists("cgasvc.exe.exe") ; Will return the PID or 0 if the process isn't found.

If $PID Then ProcessClose($PID)

this should work to end the process. but how do I set the startup type to disabled?

Link to comment
Share on other sites

  • Moderators

ProcessClose("cgasvc.exe.exe")

$PID = ProcessExists("cgasvc.exe.exe") ; Will return the PID or 0 if the process isn't found.

If $PID Then ProcessClose($PID)

this should work to end the process. but how do I set the startup type to disabled?

From the command prompt:

sc config <servicename> start= disabled

So for example to disable the Print Spooler service the command would be:

sc config spooler start= disabled

Edit: typo

Edited by big_daddy
Link to comment
Share on other sites

Another way in BETA

$serviceName = "Spooler"

RunWait(@ComSpec & " /c NET STOP " & $serviceName & " /Y",@SystemDir,@SW_HIDE)
$oInstance = ObjGet("winmgmts:{impersonationLevel=impersonate}//" & @ComputerName & "\root\cimv2:Win32_Service='" & $serviceName & "'")
$oMethod = $oInstance.Methods_("ChangeStartMode")
$oInParam = $oMethod.inParameters.SpawnInstance_()
$oInParam.StartMode = "Disabled"
$oInstance.ExecMethod_("ChangeStartMode",$oInParam)
Link to comment
Share on other sites

where there is a will, there are many ways

$service = "Symantec AntiVirus"
ConsoleWrite(_SetServiceState(@ComputerName, $service, "Stop") & @LF)
Sleep ( 5000 )
ConsoleWrite(_SetServiceState(@ComputerName, $service, "Start") & @LF)

Func _SetServiceState($s_pc, $service, $State)
    Local Const $wbemFlagReturnImmediately = 0x10
    Local Const $wbemFlagForwardOnly = 0x20

    Local $colItems = "", $objItem, $ret_status = -1
    Local $a_status[25] = ["Success", "Not supported", "Access denied", "Dependent services running", _
            "Invalid service control", "Service cannot accept control", "Service not active", "Service request timeout", _
            "Unknown failure", "Path not found", "Service already stopped", "Service database locked", "Service dependency deleted", _
            "Service dependency failure", "Service disabled", "Service logon failed", "Service marked for deletion", "Service no thread", _
            "Status circular dependency", "Status duplicate name", "Status - invalid name", "Status - invalid parameter", _
            "Status - invalid service account", "Status - service exists", "Service already paused"]
    If Ping($s_pc) Then
        Local $objWMIService = ObjGet("winmgmts:\\" & $s_pc & "\root\CIMV2")
        If @error Then
            MsgBox(16, "_SetServiceState", "ObjGet Error: winmgmts")
            Return
        EndIf
        $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Service", "WQL", _
                $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
        If @error Then
            MsgBox(16, "_SetServiceState", "ExecQuery Error: SELECT * FROM Win32_Service")
            Return
        EndIf
        If IsObj($colItems) Then
            For $objItem In $colItems
                If $objItem.Name = $service Then
                    Select
                        Case $State = "Boot" Or $State = "System" Or $State = "Automatic" Or $State = "Manual" Or $State = "Disabled"
                            $ret_status = $objItem.ChangeStartMode ($State)
                        Case $State = "Stop"
                            $ret_status = $objItem.StopService ()
                        Case $State = "Start"
                            $ret_status = $objItem.StartService ()
                        Case $State = "Pause"
                            $ret_status = $objItem.PauseService ()
                        Case $State = "Resume"
                            $ret_status = $objItem.ResumeService ()
                        Case $State = "Delete"
                            $ret_status = $objItem.Delete ()
                    EndSelect
                    ExitLoop
                EndIf
            Next
        EndIf
    EndIf
    If $ret_status <> -1 Then
        Return $a_status[$ret_status]
    Else
        SetError(1)
        Return $ret_status
    EndIf
EndFunc ;==>_SetServiceState
Edited by gafrost

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

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