8 Ways to Manipulate a Service
I have been working on a client health script and one of the key issues was how to configure a service. There are two components of a service runtime that are most relevant at first, is it running? What the service set to do? The following are the possible values:
- Mode
- Boot
- System
- Auto
- Demand (Manual)
- Disabled
- Status
The major methods for controlling a service
- Control Panel
- The control panel provides you with a list of all services in the registry and control of each.
- Winkey + r >> services.msc
- Command Line: NET
- The command line of NET has a limitation of only modifying the status of services. It cannot change the Mode.
- Winkey + r >> cmd
- NET Start lists running services.
- When typed at the command prompt, service names of two words or more must be enclosed in quotation marks. For example, NET START "NET LOGON" starts the net logon service.
- SC.exe
- Service Control is a command line executable.
- There are multiple versions of SC.exe.
i. Windows 2000 server pack came with 4.0.1371.1
ii. Windows XP came with 5.1.2600.0
1. Note this is windows version not file version
2. %systemroot%\system32\sc.exe
iii. Visual Studio 2005 came with version 5.00.2134.1
1. This looks to be the same as the Windows XP version
2. C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin\winnt\
- SC can gather information, change status and mode, security (SDSET), create and delete
- http://msdn.microsoft.com/en-us/library/ms810435.aspx
- PsService.exe
- All the same functionality as SC.exe plus:
i. Allows you to control a service on a remote machine with a different account.
ii. Service Search which identifies active instances of a service on your network.
iii. Works on NT4 à Windows 7.
- Tip: to prevent the EULA from popping up use the switch: -accepteula
- http://technet.microsoft.com/en-us/sysinternals/bb897542.aspx
- Registry
- Another interesting thing I ran across was you can determine the status of a service from the registry. Through you could not change the status via the registry. If you do change the value only services.msc would displayed a difference.
i. HKLM\SYSTEM\CurrentControlSet\Services\
- This was extremely useful in determining if WMI was running on a box without attempting to create a WMI object
- ADSI
- See the code snippet at the article entitled Start a Service via ADSI
- http://myitforum.com/cs2/blogs/scassells/archive/2008/12/12/start-a-service-via-adsi.aspx
- PowerShell
- PowerShell Makes it Easy to grab and manipulate WMI objects
- Example useful commands
i. Get-Service
1. Display all the member functions possible by:
2. Get-service | gm ii. Stop-Service iii. Start-Service
- WMI Section
- You can modify the Win32_Services class in WMI.
- The namespace is root\cimv2.
i. There are no proceeding or trailing slash on the namespace
ii. If you are connecting to a remote computer you will have a double slash before the computer name and slash between the computer name and namespace
1. \\ComputerName\root\cimv2
- You can use CIM Studio to change the values directly
i. http://myitforum.com/cs2/blogs/scassells/archive/2008/06/06/how-to-find-the-sms-or-configmgr-namespace-in-wmi.aspx
- You can use VB script to connect to a WMI provider and change the service
- Or you can connect to WMI via PowerShell very easily. The following code snippet will list the Name, State, Mode, and start name. (careful of word wrap – all on one line)
i. Get-WmiObject -class Win32_Service | Select-Object -property name, state, startmode, startname | Sort-Object -property startmode, state, name | Format-Table *
- Another Option is to do a query executed
i. $Query = “select * from win32_Service” ii. $a = Get-WMIObject –namespace “root\cimv2” –query $Query iii. $a | Format-List *
The above information is about different methods to interact with a service. Think tools in a box. Use the one most appropriate.
Enjoy