Jump to content

Start/Stop a service


Recommended Posts

Hello all,

I'm trying to write a program that can disable one service and enable another service. However if I try to run the two functions as shown in the code only the first service is disabled and the second service is not enabled. But if I run them seperately the code works. Not sure why I can't do them together.

Thanks!

_GetService("Windows Firewall/Internet Connection Sharing (ICS)", "Disabled")
_GetService("Windows Firewall/Internet Connection Sharing (ICS)", "Automatic")
Func _GetService($service, $State)
    Dim $objWMIService, $colServiceList, $objService, $errReturnCode
    Dim $strComputer
    $strComputer = "."
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

    $colServiceList = $objWMIService.ExecQuery _
            ("Select * from Win32_Service where Name = '" & $service & "'")
    
If $State = "Disabled" then
    For $objService In $colServiceList
        If $objService.State = "Running" Then
            $objService.StopService()
            Sleep(5000)
        EndIf
        $objService.ChangeStartMode($State)
    Next

Else
    
For $objService In $colServiceList
        $objService.ChangeStartMode($State)
        $objService.StartService()
        Sleep(5000)
    Next
    
    EndIf
EndFunc
Link to comment
Share on other sites

There are a lot of funtions dealing with services. Have you tried them.

What happens when you do it manually via cmd net start/stop

Edited by Xenobiologist

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

  • 1 month later...

Try changing these lines:

_GetService("Windows Firewall/Internet Connection Sharing (ICS)", "Disabled")

_GetService("Windows Firewall/Internet Connection Sharing (ICS)", "Automatic")

To these lines:

_GetService("SharedAccess", "Disabled")

_GetService("SharedAccess", "Automatic")

Reason:

The code is referencing the Display name when it should be referencing the Service name.

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