Jump to content

Enable a disabled service


D8TA

Recommended Posts

Here is a basic batch example using sc.exe, it checks if the services are running, if not starts them.

@echo off &&setlocal
set tocheck=servicename1 servicename2 servicename3
for %%a in (%tocheck%) do (
 sc.exe query %%a | find /i "state" | find /i "4  running" >nul
 if not errorlevel 0 sc start %%a
)
endlocal &&goto :eof

Just replace the required 'Service Names', servicename1 servicename2 servicename3 with the ones you wish to check.

It isn't ideal for your requirements but may help you out!

Link to comment
Share on other sites

  • 2 years later...

I just copied/pasted/modified some .vbs stuff to do what I think you needed to get accomplished (I'm posting so others searching may find this answer since I don't think what he actually wanted to do was successfully posted).

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name = 'YourServiceNameHere'")

For Each objService in colServiceList
objService.ChangeStartMode("Automatic")
Wscript.Sleep 5000
errReturnCode = objService.StartService()
Next

You can set objService.ChangeStartMode mode to Automatic, Manual, or Disabled. YMMV with this, but it worked for me.

Link to comment
Share on other sites

I would start by restricting users from controlling services. IMHO it sounds like you're approaching this backwards.

How would I go about restricting the services within group policy? Your right I am, this would prevent them from doing it again after I have the service started again.

The best way to stop them from messing around with services? Don't make them admins! :)

You can't restrict services with Group Policy until they're in an AD domain. Once you do that then you just create a Group Policy Object (GPO) that has the settings you want for the services. Then you apply that GPO to the OU that the computer accounts are in.

Here is a basic batch example using sc.exe, it checks if the services are running, if not starts them.
Unfortunately, you can't Start a service if it's set to Disabled so the state has to be changed first. :)
I just copied/pasted/modified some .vbs stuff to do what I think you needed to get accomplished (I'm posting so others searching may find this answer since I don't think what he actually wanted to do was successfully posted).

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name = 'YourServiceNameHere'")

For Each objService in colServiceList
objService.ChangeStartMode("Automatic")
Wscript.Sleep 5000
errReturnCode = objService.StartService()
Next

You can set objService.ChangeStartMode mode to Automatic, Manual, or Disabled. YMMV with this, but it worked for me.

That script will do exactly what he wants as far as changing the state of the service. A mixture of that script and the one that Yzöwl posted could be used to both change the state to Automatic and then Start the service. :D Edited by nmX.Memnoch
Link to comment
Share on other sites

Here is a basic batch example using sc.exe, it checks if the services are running, if not starts them.
Unfortunately, you can't Start a service if it's set to Disabled so the state has to be changed first. :)
I was aware of that at the time of posting, as you will note by my full response. However since you have brought it up and I as a result of not fully answering the question haven't slept well in the last 2½ years, the following will do as requested.
@ECHO OFF&SETLOCAL
SET "_=servicename1 servicename2 servicename3"
FOR %%? IN (%_%) DO (CALL :CS_ %%?
IF ERRORLEVEL 4 SC CONFIG %%? START= AUTO)
GOTO :EOF
:CS_
FOR /F "TOKENS=2 DELIMS=: " %%? IN (
'SC QC %1^|FIND "START_TYPE"') DO EXIT/B %%?
GOTO :EOF

I'm not sure that the service would need starting as well, especially at login. If that is necessary then the appropriate line could be added to the batch file code now provided.

Link to comment
Share on other sites

Sorry for the length of the script, this is what it does

1:\ Creates Folder Windir\Logs\Service

2:\ It produces a text file named Windir\Logs\Service\ComputerName_UserName.txt

3:\ List all the services and there status.

Uncomment the areas needed to use

4:\ Start any service that not running,

5:\ Set how you want it the service to start

I thought this might be a better way to do what you want because you are going to run it as a log on script so you will be able to tell who is changing what. You will have to make some changes to the script, as I only tested this so there was no

run time errors.

Save As ServiceStartLog.vbs

'/-> This Is My Text Comment Line
' This Is A Standard Comment Line Remove This To Make That Part Of The Script Active
Option Explicit
'/-> Like Varibles But Never Change
Const NewText=2, AddText=8
'/-> Varibles
Dim ColItems, Computer, ObjItem, ObjS, Service, Ts
Computer = "."
'/-> Objects
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Wmi :Set Wmi = GetObject("winmgmts:\\" & Computer & "\root\cimv2")
Dim Sys :Set Sys = Act.Environment("Process")
Dim Loc :Loc = Sys("windir") & "\Logs\Service\"
Dim ArL :ArL = Chr(160) & Chr(171) & Chr(160)
Dim ArR :ArR = Chr(160) & Chr(187) & Chr(160)
Dim Lne :Lne = ArL & "----------------------------------------" & ArR
Dim Rpt :Rpt = Loc & Sys("ComputerName") & "_" & Sys("UserName") & ".txt"
'/-> Create Folder Windir\Logs If Missing
If Not Fso.FolderExists(Sys("windir") & "\Logs") Then
Fso.CreateFolder(Sys("windir") & "\Logs")
End If
'/-> Create Folder Windir\Logs\Service For Text File
If Not Fso.FolderExists(Sys("windir") & "\Logs\Service") Then
Fso.CreateFolder(Sys("windir") & "\Logs\Service")
End If
'/-> Checks If The Text File There
If Fso.FileExists(Rpt) Then
'/-> Add Text To The Text File, File Was There
Set Ts = Fso.OpenTextFile(Rpt,AddText,True)
StartReport()
Else
'/-> Creates New Text File, File Was Not There
Set Ts = Fso.OpenTextFile(Rpt,NewText,True)
StartReport()
End If
'/-> Start Collection
Set ColItems = Wmi.ExecQuery("SELECT * FROM Win32_Service",,48)
For Each ObjItem in ColItems
If ObjItem.Started = False Then
Ts.WriteLine Chr(160) & ArL & "Process Name " & ArR & ObjItem.Name
Ts.WriteLine Chr(160) & ArL & "Process State " & ArR & ObjItem.State
Ts.WriteLine Chr(160) & ArL & "Process Start Mode" & ArR & ObjItem.StartMode
Ts.WriteLine Chr(160) & ArL & "Process Full Name " & ArR & ObjItem.DisplayName
Ts.WriteLine Chr(160) & ArL & "Process Cmd Path " & ArR & ObjItem.PathName
Ts.WriteLine Lne
' Return = ObjS.StartService()
' WScript.Sleep 5000
'/-> Other Start Modes
'/-> ObjItem.ChangeStartMode("Disabled")
'/-> ObjItem.ChangeStartMode("Manual")
' Return = ObjItem.ChangeStartMode("Automatic")
End If
Next
'/-> Add Text Functions
Function StartReport()
Ts.WriteLine Lne
Ts.WriteLine Chr(160) & ArL & "Start Of The Script" & ArR & Now()
Ts.WriteLine Chr(160) & ArL & "Computer Name " & ArR & Sys("ComputerName")
Ts.WriteLine Chr(160) & ArL & "User Login Name " & ArR & Sys("UserName")
Ts.WriteLine Lne
End Function
Ts.WriteLine Chr(160) & ArL & "End Of The Script " & ArR & Time
Ts.WriteLine Lne & vbCrLf & Lne & vbCrLf
'/-> Close The Text File Object
Ts.Close()
'/-> Run Report
' Act.Run("notepad.exe " & Chr(34) & Rpt & Chr(34)),1,True

Desided to add a script that will just turn on any service that is turned off.

I have tested this and got no run time errors.

Save As ServiceStart.vbs

'/-> Varibles
Dim ColItems, Computer, ObjItem
Computer = "."
'/-> Objects
Dim Wmi :Set Wmi = GetObject("winmgmts:\\" & Computer & "\root\cimv2")
'/-> Start Collection
Set ColItems = Wmi.ExecQuery("SELECT * FROM Win32_Service",,48)
For Each ObjItem in ColItems
If ObjItem.Started = False Then
' Return = ObjItem.StartService()
' WScript.Sleep 5000
'/-> Other Start Modes
'/-> ObjItem.ChangeStartMode("Disabled")
'/-> ObjItem.ChangeStartMode("Manual")
' Return = ObjItem.ChangeStartMode("Automatic")
End If
Next

Edited by gunsmokingman
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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...