Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


November 2006

The All-Purpose Service Controller

Use SC as a more fully functioning Net command alternative
RSS
Subscribe to Windows IT Pro | See More Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Command-line junkies love the ability to manipulate Windows services from the command prompt, but until a few years ago all we had was Net Start, Net Pause, Net Continue, and Net Stop, which—as their names suggest—start, pause, continue (i.e., "un-pause"), and stop a service.

If you'd like to be able to do more with services from the command line, you should check out the SC tool (sc.exe), which you'll find in Windows XP and later.

What Does It Look Like?
SC commands share the structure

sc [\\<servername>] <command> <servicename> [<options>] 

where the optional servername simply points to the system at which to direct the command, command is the particular SC subcommand (e.g., pause, start, create, delete), servicename is the service's internal name, and options refers to any options that the subcommand requires. The simple example

sc \\server1 stop lanmanserver 

contacts the Server1 system and stops the Server service, which goes by the internal service name lanmanserver. If I wanted to stop the Server service locally, I'd simply drop the servername option.

The Internal Name
Knowing the service's internal name is essential. If I assume that the displayed name for the file-server service is Server, and I try the command

sc stop server 

I get an SC error. Only lanmanserver works. The internal name is also called the key name because lanmanserver is the name of the registry subkey within HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services in which Windows stores information about that particular service. If you know the friendlier display name— such as Server for the file-server service or DNS for the DNS Server service—just use the getkeyname command to convert a display name to a key name. For example, to find the DNS Server service's key name, I could use the command

sc getkeyname "dns server" 

Doing so tells me that the DNS Server service's key name is DNS. (Case seems not to matter to SC, by the way.) You can reverse the process, and—given the key name—get the more human-friendly display name, by using the getdisplayname command.

How Does It Work?
SC has four commands that correspond to the aforementioned Net commands: start, stop, pause, and continue. For example, to pause the server service, I could type

sc pause server 

SC's "stop" functionality behaves a little differently than it does with the Microsoft Management Console (MMC) Services snap-in or the Net Stop command, which understand that the Computer Browser service depends on the Server service. The Services snap-in and Net Stop ask if it's OK to stop the Computer Browser service as well, whereas SC just fails with the statement A stop control has been sent to a service that other running services are dependent on. In other words, in this instance, SC isn't quite as smart as the other commands.

With the enumdepend command, however, you can determine what other services depend on a given service:

sc enumdepend lanmanserver 

In response, SC reports that the DFS and Browser services depend on the Server service. But SC returns this information in a sort of terse, techie way. For example, you can use the query command to query a service about its current state. Querying the Server service with

sc query lanmanserver 

returns the information that Figure 1 shows. This is the basic way that SC lists information about a service.

SC is a pretty useful tool, but I've only just begun. Join me next month for more SC!

End of Article



Reader Comments
SC is a great tool, but I've noticed something that has an effect on shell scripting. SC sends start, stop, pause, and continue signals and doesn't wait for completion of the task before exiting. If type "SC Stop service" and then "sc query service" you will see a "STOP_PENDING" state. This means the following command in a shell script could run before the service is completely stopped.

How do I handle that situation? I have a service I want to stop then backup the files to another location using NTBACKUP from the command line. I want to script this and I want wait till the service is completely stopped before running the backup command, wait till the backup is complete before starting the service again.

kincaid@valleysupply.net January 26, 2007 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

Escape From Yesterworld

Kevin points you to the funniest SQL Server website ever! ...


Related Articles Castaway on Command-Prompt Island

Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Are You Satisfied?

A Preliminary Look at Deployment Plans for Microsoft Windows Vista

Related Events Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

IT Connections
Dive into the new Microsoft platforms and products you implement and support with the experts from Microsoft, TechNet Magazine, Windows ITPro and industry gurus. There are 70+ sessions and interactive panels with networking opportunities.

Attention User Group Leaders...
Announcing the eNews Generator—a FREE HTML e-newsletter builder for user group leaders. Build your HTML and text e-newsletters in minutes and add Windows IT Pro & SQL Server Mag articles alongside your own message!.

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Get SQL Server 2008 at WinConnections
Don’t miss Microsoft Exchange and Windows Connections conferences, the premier events for Microsoft IT Professionals in Las Vegas, November 10-13. Every attendee will receive a copy of SQL Server 2008 Standard Edition with one CAL.



Interested in Email Encryption?
Read about the advantages of identity-based encryption in this free report.

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing