List service with a batch file

Hi,

I want to know if there is a way to list all service that have startup type set to automatic with a batch file.

thanks
christgAsked:
Who is Participating?
 
exx1976Connect With a Mentor Commented:
strComputer = "."
Const ForAppending = 8
data = "Paused"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\test.txt", ForAppending, True)
objTextFile.Writeline data
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where State = 'Paused' and StartMode = 'Auto'")
For Each objItem In colItems
   objTextFile.WriteLine objItem.Name
Next
data = "Stopped"
objTextFile.Writeline data
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where State = 'stopped' and StartMode = 'Auto'")
For Each objItem In colItems
   objTextFile.WriteLine objItem.Name
Next
objTextFile.Close


There ya go.  That will even put in little headers so you can see which are paused and which are stopped.

HTH,
exx

BTW - mdiglio --  I've done my fair share of WMI scripting, but I've never seen the ,,48 at the end of a service inquiry.  What exactly is that for?  Couldn't find any reference to it..  ??
0
 
Yan_westCommented:
Hmm you can list all running services with the net start command.

Just type net start at the command prompt, and it will give you all running services.
0
 
christgAuthor Commented:
Yes, I already tried that but I only want to have a list that contain only automatic services. I also tried with sc.exe with no good result.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Yan_westCommented:
Just open notepad and type

net start

and save it as services.bat

If you want to redirect the input in a text file you can write

Net start > services.txt

Or Net start >> services.txt to append to the file each time you run it.
0
 
paul_knappCommented:
If you are willing to spend a little bit of money, purchase Dameware NT Utilities. This is a very valuable piece of software that has numerous features. You will be able with this software to export alot of information about PC's on your network. One of which is the ability to export a list of services running on any PC on the network to a text file. You can downlod a 'trial' version of this software from www.dameware.com. Check it out. Good stuff
0
 
christgAuthor Commented:
What I ant to do it to schedule a batch file that will create a file that contain only services that are supposed to start automatically.

I know that there is a lot of nice utility on the market but I don't want to do the right - export to a file everynight !!

0
 
mdiglioCommented:
Hello,
here is a vb script that should accmoplish the task.
you can schedule the script to run or schedule a batch file to run that in turn will run the script.

open notepad >> copy and paste the following code >> save it with a .vbs extension.
It will create a file on the C drive called test.txt. Of course you can change this part to your liking (line 6)

'!!Begin Copy
strComputer = "."
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\test.txt", ForAppending, True)

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select name from Win32_Service where StartMode = 'Auto'", , 48)
For Each objItem In colItems
 
   objTextFile.WriteLine objItem.Name
 
Next

objTextFile.Close

'!!End Copy

Let me know what changes we need to make
0
 
christgAuthor Commented:
Thanks mdiglio, now, is it possible to have an option that will sort automatic service that are not running into a file ?
0
 
mdiglioCommented:
christg,
did exx1976 response answer your question?
I didn't understand what you meant by 'sort automatic service that are not running into a file'
(maybe I need more coffee)
I hope that worked for you

exx1976,
I always use those constants out of habit of learning wmi from the scriptomatic tool :)
this is an excerpt from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/scripting_api_constants.asp

to create a semisynchronous call, the iFlags parameter in an SWbemServices.ExecQuery_ call must contain two flags. The value of WbemFlagReturnImmediately is 16 and the value of WbemFlagForwardOnly is 32. Because the constants cannot be accessed by name, the values of these flags are combined, producing an iFlags value of 48
0
 
christgAuthor Commented:
Yes, it works with exx1976 response.

Thanks
0
 
exx1976Commented:
Ahhh..  The scriptomatic tool..   I have looked at it a few times, but never used it.  I learned from the MS 2433 class and various books and code I found posted on the net...  I'll look into that MSDN article, thanks!!

Glad I could help christg, but I really think you should have split the points..   mdiglio did help you out..  I just happened to find the thread and added 4 or 5 lines to what he already had there...


-exx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.