Script for disabling services

Home Forums Scripting General Scripting Script for disabling services

This topic contains 2 replies, has 2 voices, and was last updated by Avatar crobertson 3 years, 5 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • Avatar
    crobertson
    Member
    #145502

    I have a script for disabling services already that I modified from one I found online. (A batch file really)

    Does anyone have a script that they would share for turning off services? This script works ok, but If a service doesn’t stop quickly enough, it messes up the batch and starts showning dos command format help.

    Avatar
    wullieb1
    Moderator
    #242081

    Re: Script for disabling services

    I use this to check if it is disabled. If it is not i call the stop service and disable it.

    Thre is probably a more elegant way of doing it but this works for me.

    ‘Disable the Windows Firewall on users machines
    If Checkstate = “” Then
    Call UpdateComponentsDiv(“Firewall not installed on this machine. Please update to at least Windows XP Service Pack 2.“)
    ElseIf Checkstate = “true” Then
    StopService
    Call UpdateComponentsDiv(“The Windows firewall is now disabled“)
    ElseIf Checkstate = “false” Then
    Call UpdateComponentsDiv(“The Windows firewall Service is already disabled“)
    End If[/CODE]

    [CODE] Function Checkstate

    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:\” & strComputer & “rootcimv2”)
    Set colItems = objWMIService.ExecQuery(“Select * from Win32_Service”,,48)
    For Each objItem in colItems
    If objitem.displayname = “Windows Firewall/Internet Connection Sharing (ICS)” Then
    If objitem.started = “False” Then
    Checkstate = “false”
    Else
    Checkstate = “true”
    end If
    Exit For
    End If
    Next
    End Function[/CODE]

    [CODE] Function StopService

    StrComputer = “.”
    Set objWMIservice = GetObject (“winmgmts:\” & strComputer & “rootcimv2”)
    Set Colitems = ObjWMIservice.execquery (“Select * from win32_service”,,48)
    For Each objitem In colItems
    If objitem.displayname = “Windows Firewall/Internet Connection Sharing (ICS)” Then
    If objitem.started = “True” Then
    objitem.StopService (“Windows Firewall/Internet Connection Sharing (ICS)”)
    objitem.ChangeStartMode (“Disabled”)
    End If
    Exit for
    End If
    Next
    End Function[/CODE][CODE] ‘Disable the Windows Firewall on users machines
    If Checkstate = “” Then
    Call UpdateComponentsDiv(“Firewall not installed on this machine. Please update to at least Windows XP Service Pack 2.“)
    ElseIf Checkstate = “true” Then
    StopService
    Call UpdateComponentsDiv(“The Windows firewall is now disabled“)
    ElseIf Checkstate = “false” Then
    Call UpdateComponentsDiv(“The Windows firewall Service is already disabled“)
    End If[/CODE]

    Function Checkstate

    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:\” & strComputer & “rootcimv2”)
    Set colItems = objWMIService.ExecQuery(“Select * from Win32_Service”,,48)
    For Each objItem in colItems
    If objitem.displayname = “Windows Firewall/Internet Connection Sharing (ICS)” Then
    If objitem.started = “False” Then
    Checkstate = “false”
    Else
    Checkstate = “true”
    end If
    Exit For
    End If
    Next
    End Function[/CODE]

    [CODE] Function StopService

    StrComputer = “.”
    Set objWMIservice = GetObject (“winmgmts:\” & strComputer & “rootcimv2”)
    Set Colitems = ObjWMIservice.execquery (“Select * from win32_service”,,48)
    For Each objitem In colItems
    If objitem.displayname = “Windows Firewall/Internet Connection Sharing (ICS)” Then
    If objitem.started = “True” Then
    objitem.StopService (“Windows Firewall/Internet Connection Sharing (ICS)”)
    objitem.ChangeStartMode (“Disabled”)
    End If
    Exit for
    End If
    Next
    End Function[/CODE][CODE] Function Checkstate

    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:\” & strComputer & “rootcimv2”)
    Set colItems = objWMIService.ExecQuery(“Select * from Win32_Service”,,48)
    For Each objItem in colItems
    If objitem.displayname = “Windows Firewall/Internet Connection Sharing (ICS)” Then
    If objitem.started = “False” Then
    Checkstate = “false”
    Else
    Checkstate = “true”
    end If
    Exit For
    End If
    Next
    End Function[/CODE]

    Function StopService

    StrComputer = “.”
    Set objWMIservice = GetObject (“winmgmts:\” & strComputer & “rootcimv2”)
    Set Colitems = ObjWMIservice.execquery (“Select * from win32_service”,,48)
    For Each objitem In colItems
    If objitem.displayname = “Windows Firewall/Internet Connection Sharing (ICS)” Then
    If objitem.started = “True” Then
    objitem.StopService (“Windows Firewall/Internet Connection Sharing (ICS)”)
    objitem.ChangeStartMode (“Disabled”)
    End If
    Exit for
    End If
    Next
    End Function[/CODE][CODE] Function StopService

    StrComputer = “.”
    Set objWMIservice = GetObject (“winmgmts:\” & strComputer & “rootcimv2”)
    Set Colitems = ObjWMIservice.execquery (“Select * from win32_service”,,48)
    For Each objitem In colItems
    If objitem.displayname = “Windows Firewall/Internet Connection Sharing (ICS)” Then
    If objitem.started = “True” Then
    objitem.StopService (“Windows Firewall/Internet Connection Sharing (ICS)”)
    objitem.ChangeStartMode (“Disabled”)
    End If
    Exit for
    End If
    Next
    End Function[/CODE]

    Avatar
    crobertson
    Member
    #298529

    Just following up here is the basic script that can be tailored to your need. This batch checks if the service is even available then if it is running, echos the service name, stops the service if running and disables the service.

    =====code start
    echo.&echo.&echo =====Disable services=====
    echo.
    for %%x in (
    “Mcx2Svc”
    “NetTcpPortSharing”
    “RemoteRegistry”
    “SharedAccess”
    “fax”
    “TabletInputService”
    “RemoteAccess”
    “UxSms”
    “Bdesvc”
    “EFS”
    “wuauserv”
    “wsearch”
    ) do (sc query %%x| find “STATE” >nul 2>&1 && sc query %%x| find “RUNNING” >nul 2>&1 && echo Stop and Disable service: %%x && net stop %%x && sc config %%x start= disabled)
    ======end code

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.