Shaun Cassells at MyITForum.com

SMS 2003 and ConfigMgr 2007, PowerShell, Scripting, Finance, Fitness and Fun

News

Locations of visitors to this page

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
    • Running
    • Stopped
    • Pause
 

The major methods for controlling a service

  1. Control Panel
    1. The control panel provides you with a list of all services in the registry and control of each.
    2. Winkey + r >> services.msc
  2. Command Line: NET
    1. The command line of NET has a limitation of only modifying the status of services.  It cannot change the Mode.
    2. Winkey + r >> cmd
    3. NET Start lists running services.
    4. 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.
  3. SC.exe
    1. Service Control is a command line executable.
    2. 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\

    1. SC can gather information, change status and mode, security (SDSET), create and delete
    2. http://msdn.microsoft.com/en-us/library/ms810435.aspx
  1. PsService.exe
    1. 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.

    1. Tip: to prevent the EULA from popping up use the switch: -accepteula
    2. http://technet.microsoft.com/en-us/sysinternals/bb897542.aspx
  1. Registry
    1. 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\
    1. This was extremely useful in determining if WMI was running on a box without attempting to create a WMI object
  1. ADSI
    1. See the code snippet at the article entitled Start a Service via ADSI
    2. http://myitforum.com/cs2/blogs/scassells/archive/2008/12/12/start-a-service-via-adsi.aspx
  2. PowerShell
    1. PowerShell Makes it Easy to grab and manipulate WMI objects
    2. Example useful commands
                                                               i.      Get-Service

1.      Display all the member functions possible by:

2.      Get-service | gm                                                              ii.      Stop-Service                                                            iii.      Start-Service
  1. WMI Section
    1. You can modify the Win32_Services class in WMI.  
    2. 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

    1. 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

    1. You can use VB script to connect to a WMI provider and change the service
    2. 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 *
    1. 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

Comments

No Comments