<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matt Wilson &#187; Scripting</title>
	<atom:link href="http://www.mattwilsoninc.com/category/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mattwilsoninc.com</link>
	<description>A glimpse into Web Operations</description>
	<lastBuildDate>Wed, 04 May 2011 00:40:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>The many ways to control Windows Services</title>
		<link>http://www.mattwilsoninc.com/microsoft/the-many-ways-to-control-windows-services/</link>
		<comments>http://www.mattwilsoninc.com/microsoft/the-many-ways-to-control-windows-services/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 04:29:05 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[Windows Services]]></category>

		<guid isPermaLink="false">http://www.mattwilsoninc.com/?p=31</guid>
		<description><![CDATA[<a href="http://www.mattwilsoninc.com/microsoft/the-many-ways-to-control-windows-services/" title="The many ways to control Windows Services"></a>I&#8217;m often asked how windows services are controlled in the enterprise. There is a variety of convenient ways to control services, each with strengths and weeknesses. We&#8217;ll go through some of them today. 1. The services.msc MMC is the nice &#8230;<p class="read-more"><a href="http://www.mattwilsoninc.com/microsoft/the-many-ways-to-control-windows-services/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.mattwilsoninc.com/microsoft/the-many-ways-to-control-windows-services/" title="The many ways to control Windows Services"></a><p>I&#8217;m often asked how windows services are controlled in the enterprise. There is a variety of convenient ways to control services, each with strengths and weeknesses. We&#8217;ll go through some of them today.</p>
<p>1. The <strong>services.msc</strong> MMC is the nice GUI provided by windows. This features a very intuitive interface, but can only connect to one system at a time. This is not very scalable if you need to shut down many services on multiple machines.</p>
<p>2. net start, net stop etc.. This is the original command line version to control services. Good when you are on the system. This is scriptable via a batch file. This command came with Windows NT.</p>
<p>3. <strong>sc.exe</strong> comes with the Windows Resource Kit. This is a great tool and allows to control services on remote machines. Setting up services can also be done with this tool.</p>
<div id="attachment_29" class="wp-caption alignnone" style="width: 310px"><a href="http://www.mattwilsoninc.com/wp-content/uploads/2008/08/sc_query.png"><img class="size-medium wp-image-29" title="sc_query" src="http://www.mattwilsoninc.com/wp-content/uploads/2008/08/sc_query-300x138.png" alt="scquery" width="300" height="138" /></a><p class="wp-caption-text">scquery</p></div>
<p> 4. PS Service (psservice.exe) from the <a href="http://technet.microsoft.com/en-us/sysinternals/bb896649.aspx" target="_blank">PS Tools Suite</a>, authored by <a href="http://blogs.technet.com/markrussinovich/" target="_blank">Mark Russinovch</a>. psservice runs on the command line like sc.exe and shares some of the same features like controlling services on remote machines. One advantage is displaying the security descriptor in a readable format.</p>
<div id="attachment_34" class="wp-caption alignnone" style="width: 310px"><a href="http://www.mattwilsoninc.com/wp-content/uploads/2008/08/psservice_security.png"><img class="size-medium wp-image-34" title="psservice_security" src="http://www.mattwilsoninc.com/wp-content/uploads/2008/08/psservice_security-300x252.png" alt="PSService Security" width="300" height="252" /></a><p class="wp-caption-text">PSService Security</p></div>
<p>5. WMI or Windows Management Instrumentation can control services. WMI can be programmed from VBScript, c# (.NET) and PowerShell. Flow control can be quickly executed with any of the mentioned programming languages giving administrators precise control over which services and systems need to have services controlled. Below is an example of some VBScript functions starting and stopping services via WMI. These functions also start/stop the dependate services which is important.</p>
<pre>Function StopServiceWMI(strServiceName)

  strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    &amp; "{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   &amp; "{Win32_Service.Name='" &amp; strServiceName &amp; "'} Where " _
        &amp; "AssocClass=Win32_DependentService " &amp; "Role=Antecedent" )
For each objService in colServiceList
    objService.StopService()
Next
Wscript.Sleep 5000
Set colServiceList = objWMIService.ExecQuery _
        ("Select * from Win32_Service where Name='" &amp; strServiceName &amp; "'")
For each objService in colServiceList
    errReturn = objService.StopService()
Next

End Function

Function StartServiceWMI(strServiceName)

  strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    &amp; "{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where Name='" &amp; strServiceName &amp; "'")
For each objService in colServiceList
    errReturn = objService.StartService()
Next
Wscript.Sleep 5000
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   &amp; "{Win32_Service.Name='" &amp; strServiceName &amp; "'} Where " _
        &amp; "AssocClass=Win32_DependentService " &amp; "Role=Dependent" )
For each objService in colServiceList
    objService.StartService()
Next

End Function</pre>
<p>5. One of the last methods is to use the native Get-Service PowerShell CmdLet. To understand what Get-Service can do. Type in Get-Help Get-Service in the PowerShell command prompt. You can resume, start, stop, restart, suspend, set service properties and create a new service. PowerShell does not currently have remoting capabilities, so all CmdLets are executed on the local server. PowerShell V2 will have remoting capability using WS-MAN, but that will be a topic for another time.</p>
<p>I hope you&#8217;ve enjoyed the quick tour with controlling Windows Services. Automation is the key to success in the enterprise.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.mattwilsoninc.com%2Fmicrosoft%2Fthe-many-ways-to-control-windows-services%2F&amp;title=The%20many%20ways%20to%20control%20Windows%20Services" id="wpa2a_2"><img src="http://www.mattwilsoninc.com/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mattwilsoninc.com/microsoft/the-many-ways-to-control-windows-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

