<?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>youdidwhatwithtsql.com &#187; Windows Services</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/windows-services/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Monitoring &amp; starting Services with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113</link>
		<comments>http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113#comments</comments>
		<pubDate>Thu, 21 May 2009 21:32:25 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[Windows Services]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113</guid>
		<description><![CDATA[Are you part of the DBA crowd that hasn&#8217;t yet checked out Powershell? I&#8217;m really enthusiastic about its potential for server administration. Script out all those mundane jobs you have to do and make life easy. Here&#8217;s a Powershell script that makes checking if services are running, and optionally starting them, on multiple servers really [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113">Monitoring &amp; starting Services with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Are you part of the <a href="http://en.wikipedia.org/wiki/Database_administrator" target="_blank">DBA</a> crowd that hasn&#8217;t yet checked out <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>? I&#8217;m really enthusiastic about its potential for server administration. Script out all those mundane jobs you have to do and make life easy. Here&#8217;s a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script that makes checking if services are running, and optionally starting them, on multiple servers really easy. First, be sure to check <a href="http://en.wikipedia.org/wiki/Windows_service#Managing_services" target="_blank">services.msc</a> for your SQL Server service names. They may be different from the default, i.e. if you’re running <a href="http://msdn.microsoft.com/en-us/library/ms165614.aspx" target="_blank">Named Instances</a>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SQL Server Service Names" border="0" alt="image thumb4 Monitoring &amp; starting Services with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image-thumb4.png" width="217" height="244" /></a>&#160;</p>
<ul>
<li>Create <strong>C:\Computers.txt</strong> with a list of servers to check. </li>
<li>The $servicesArray specifies <strong>SQLSERVERAGENT </strong>and<strong> MSSQLSERVER</strong>. Change these to different services if required. Be sure to use single quotes if a $ symbol is in the service name or they will be treated as variables. </li>
<li>Set $start = $true will make the script start the listed services if they are not running. </li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p113code2'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1132"><td class="code" id="p113code2"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Setup trap to catch exceptions</span>
<span style="color: #0000FF;">trap</span> <span style="color: #000000;">&#91;</span>Exception<span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">write-error</span> $<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;TRAPPED: &quot;</span> <span style="color: pink;">+</span> <span style="color: #000080;">$_</span>.Exception.Message<span style="color: #000000;">&#41;</span>; 
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># read computers from text file</span>
<span style="color: #800080;">$computers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> C:\Computers.txt;
<span style="color: #800080;">$start</span> <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
&nbsp;
<span style="color: #008000;"># Setup the Service array with the service names we want to check are running</span>
<span style="color: #800080;">$serviceArray</span> <span style="color: pink;">=</span> <span style="color: #800000;">'SQLAgent$SQL2005'</span><span style="color: pink;">,</span> <span style="color: #800000;">'MSSQL$SQL2005'</span>;
&nbsp;
<span style="color: #008000;"># Powershell knows it's an array so working with it is simple</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$computer</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$computers</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Checking $computer&quot;</span>;
	<span style="color: #800080;">$objWMIService</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-Class</span> win32_service <span style="color: pink;">-</span>computer <span style="color: #800080;">$computer</span>
&nbsp;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$service</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$objWMIService</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>	
		<span style="color: #008000;"># Check each service specicfied in the $serviceArray</span>
		<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$srv</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$serviceArray</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$service</span>.name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$srv</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;$srv is present on $computer.&quot;</span>;
				<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$service</span>.state <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;running&quot;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;$srv is running on $computer&quot;</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0000FF;">else</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;$srv is not running on $computer&quot;</span>;
					<span style="color: #008000;"># If $start is true the script will attempt to start the service if it is stopped</span>
					<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$start</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						<span style="color: #008000;"># Attempt to start the current service on the current computer</span>
						<span style="color: #800080;">$serviceInstance</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: pink;">-</span>computer <span style="color: #800080;">$computer</span> Win32_Service <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Name='$srv'&quot;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #800080;">$name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$serviceInstance</span>.Name;
						<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Attempting to start $name  on $computer.&quot;</span>
						<span style="color: #800080;">$serviceInstance</span>.StartService<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
						<span style="color: #008000;"># Refresh the object instance so we get new data</span>
						<span style="color: #800080;">$serviceInstance</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: pink;">-</span>computer <span style="color: #800080;">$computer</span> Win32_Service <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Name='$srv'&quot;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #800080;">$state</span> <span style="color: pink;">=</span> <span style="color: #800080;">$serviceInstance</span>.State;
						<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;$name is &quot;</span><span style="color: #800000;">&quot;$state&quot;</span><span style="color: #800000;">&quot; on  $computer.&quot;</span>;		
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image5.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Monitoring &amp; checking services in Powershell" border="0" alt="image thumb5 Monitoring &amp; starting Services with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image-thumb5.png" width="644" height="379" /></a></p>
<p><map name='google_ad_map_113_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/113?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_113_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=113&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmonitoring-starting-services-with-powershell%2F113' title="Monitoring &amp; starting Services with Powershell" alt=" Monitoring &amp; starting Services with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113">Monitoring &amp; starting Services with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

