JSI Tip 9668. How can a script rapidly determine the services that are set to start automatically, but are NOT running?


I have scripted SvcNR.bat to return the Registry sub-key, state, and display name of those services that are configured to start automatically, but are NOT running.

The syntax for using SvcNR.bat is:

@echo off
setlocal
For /F "Tokens=1,2* Delims=," %%a in ('SvcNR') do (
 set subkey=%%a
 set state=%%b
 set dsply=%%c
 call :DoSomeThing
)
endlocal
goto :EOF
:DoSomeThing
@echo %subkey% %state% %dsply%
SvcNR.bat contains:
@echo off
setlocal
if exist "%TEMP%\SvcNR.vbs" goto SvcNR
@echo Option Explicit>"%TEMP%\SvcNR.vbs"
@echo Dim strComputer, wbemServices, wbemObjectSet, wbemObject>>"%TEMP%\SvcNR.vbs"
@echo strComputer = "%ComputerName%">>"%TEMP%\SvcNR.vbs"
@echo Set wbemServices = GetObject("winmgmts:\\" ^& strComputer)>>"%TEMP%\SvcNR.vbs"
@echo Set wbemObjectSet = wbemServices.InstancesOf("Win32_Service")>>"%TEMP%\SvcNR.vbs"
@echo For Each wbemObject In wbemObjectSet>>"%TEMP%\SvcNR.vbs"
@echo.  On Error Resume Next>>"%TEMP%\SvcNR.vbs"
@echo.  if wbemObject.StartMode = "Auto" Then>>"%TEMP%\SvcNR.vbs"
@echo.      if wbemObject.State ^ "Running" Then>>"%TEMP%\SvcNR.vbs"
@echo.       Wscript.Echo 
" ^& wbemObject.Name ^&
,
^& wbemObject.State ^&
,
^& wbemObject.DisplayName ^&
">>"%TEMP%\SvcNR.vbs" @echo. End If>>"%TEMP%\SvcNR.vbs" @echo. End If>>"%TEMP%\SvcNR.vbs" @echo Next>>"%TEMP%\SvcNR.vbs" :SvcNR cscript //nologo "%TEMP%\SvcNR.vbs" endlocal



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish