Solved

List service with a batch file

Posted on 2004-09-13
11
393 Views
Last Modified: 2010-04-13
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
0
Comment
Question by:christg
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 15

Expert Comment

by:Yan_west
ID: 12047793
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
 

Author Comment

by:christg
ID: 12047807
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
 
LVL 15

Expert Comment

by:Yan_west
ID: 12047815
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
 
LVL 1

Expert Comment

by:paul_knapp
ID: 12047817
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
 

Author Comment

by:christg
ID: 12047884
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 16

Expert Comment

by:mdiglio
ID: 12049737
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
 

Author Comment

by:christg
ID: 12053631
Thanks mdiglio, now, is it possible to have an option that will sort automatic service that are not running into a file ?
0
 
LVL 18

Accepted Solution

by:
exx1976 earned 125 total points
ID: 12053729
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
 
LVL 16

Expert Comment

by:mdiglio
ID: 12054403
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
 

Author Comment

by:christg
ID: 12054506
Yes, it works with exx1976 response.

Thanks
0
 
LVL 18

Expert Comment

by:exx1976
ID: 12054675
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now