Yzöwl Posted May 20, 2005 Share Posted May 20, 2005 Here is a basic batch example using sc.exe, it checks if the services are running, if not starts them.@echo off &&setlocalset tocheck=servicename1 servicename2 servicename3for %%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 :eofJust 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 More sharing options...
cwuga Posted November 2, 2007 Share Posted November 2, 2007 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()NextYou 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 More sharing options...
nmX.Memnoch Posted November 2, 2007 Share Posted November 2, 2007 (edited) 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 colServiceListobjService.ChangeStartMode("Automatic")Wscript.Sleep 5000errReturnCode = objService.StartService()NextYou 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. Edited November 2, 2007 by nmX.Memnoch Link to comment Share on other sites More sharing options...
Yzöwl Posted November 2, 2007 Share Posted November 2, 2007 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&SETLOCALSET "_=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 :EOFI'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 More sharing options...
gunsmokingman Posted November 3, 2007 Share Posted November 3, 2007 (edited) Sorry for the length of the script, this is what it does1:\ Creates Folder Windir\Logs\Service2:\ It produces a text file named Windir\Logs\Service\ComputerName_UserName.txt3:\ List all the services and there status.Uncomment the areas needed to use4:\ Start any service that not running, 5:\ Set how you want it the service to startI 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 norun 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 ActiveOption 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,TrueDesided 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 November 3, 2007 by gunsmokingman Link to comment Share on other sites More sharing options...
nmX.Memnoch Posted November 4, 2007 Share Posted November 4, 2007 haven't slept well in the last 2½ yearsYeah...I didn't notice how old the original thread was until after I'd already replied. I could've edited the post but it was already too late anyway... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now