<?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>David&#039;s technobabble &#187; Oracle</title>
	<atom:link href="http://bable.cybermarshall.com/tag/oracle/feed/" rel="self" type="application/rss+xml" />
	<link>http://bable.cybermarshall.com</link>
	<description>David&#039;s thoughts about this and that</description>
	<lastBuildDate>Fri, 22 Jan 2010 18:29:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Powershell to stop and start dependent services with error notification</title>
		<link>http://bable.cybermarshall.com/2009/05/22/using-powershell-to-stop-and-start-dependent-services-with-error-notification/</link>
		<comments>http://bable.cybermarshall.com/2009/05/22/using-powershell-to-stop-and-start-dependent-services-with-error-notification/#comments</comments>
		<pubDate>Fri, 22 May 2009 06:40:10 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Hyperion]]></category>
		<category><![CDATA[Oracle]]></category>

		<guid isPermaLink="false">http://bable.cybermarshall.com/?p=529</guid>
		<description><![CDATA[A few days ago I received a request to restart a set of dependent Hyperion services every Sunday. This had to be done because; &#8220;That&#8217;s what Oracle support said is the fix to one of our TAR&#8217;s&#8221;. Although this server has 3.75 GB of RAM and had never used more than 1.2 GB of RAM. [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I received a request to restart a set of dependent Hyperion services every Sunday. This had to be done because;<em> &#8220;That&#8217;s what Oracle support said is the fix to one of our TAR&#8217;s&#8221;.</em> Although this server has 3.75 GB of RAM and had never used more than 1.2 GB of RAM. Oracle claims that memory issues are causing a service to fail. This failure causes logons to fail. There are no events in the eventlogs or error information in any Hyperion logs that we can find. However, If you restart the services, then the problem goes away. So that is Oracle support&#8217;s answer, restart the services; <em>&#8220;case closed&#8221;</em>.</p>
<p>I do not call this a solution, but a kludge.  Our Hyperion activity is relatively light. I guess Oracle&#8217;s answer to their very large and active customers must be to restart these services every 15 minutes. Hmmm, no let me think a second, the Hyperion product is financial planning software, with support answers like this, should I be concerned that this software might be calculating the wrong results? I wonder if I need to restart the other services frequently, just to be sure they calculate correctly?  </p>
<p>Ok, my sarcasm and diatribe aside, I decided to create a PowerShell script and run it as a scheduled task.<br />
<span id="more-529"></span><br />
I decided to take this opportunity to create a flexible script that I might have use for in the future. I decided:</p>
<ul>
<li>If one of the stop or start operations fails, then stop processing and notify someone to take action via email.</li>
<li>I wanted a simple array that I would list services in the dependent stop order</li>
<li>The code would use the same array and start in the reverse order</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?" onclick="pageTracker._trackPageview('/outgoing/www.ericbess.com/ericblog/2008/03/03/wp-codebox/_examples?referer=');"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p529code2'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5292"><td class="code" id="p529code2"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">## Reference to System.Net</span>
<span style="color: #000000;">&#91;</span>void<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;System.Net.Mail&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #008000;">############################################</span>
<span style="color: #008000;"># sendEmail [-smtphost -from -to -subject -body ]</span>
<span style="color: #008000;">############################################</span>
<span style="color: #0000FF;">function</span> sendEmail <span style="color: #000000;">&#40;</span><span style="color: #800080;">$smtphost</span><span style="color: pink;">,</span> <span style="color: #800080;">$from</span><span style="color: pink;">,</span> <span style="color: #800080;">$to</span><span style="color: pink;">,</span> <span style="color: #800080;">$subject</span><span style="color: pink;">,</span> <span style="color: #800080;">$body</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #008000;">#* Create new .NET object and assign to variable</span>
<span style="color: #800080;">$mail</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Net.Mail.MailMessage
<span style="color: #800080;">$mail</span>.From <span style="color: pink;">=</span> <span style="color: #800080;">$from</span>;
<span style="color: #800080;">$mail</span>.To.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$to</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$mail</span>.Subject <span style="color: pink;">=</span> <span style="color: #800080;">$subject</span>;
<span style="color: #800080;">$mail</span>.Body <span style="color: pink;">=</span> <span style="color: #800080;">$body</span>;
<span style="color: #800080;">$smtp</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Net.Mail.SmtpClient<span style="color: #000000;">&#40;</span><span style="color: #800080;">$smtphost</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$smtp</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$mail</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># set this to your smtp server</span>
<span style="color: #800080;">$smpthost</span><span style="color: pink;">=</span><span style="color: #800000;">&quot;your_smtp_server.yourdomain.com&quot;</span>
<span style="color: #800080;">$thishost</span> <span style="color: pink;">=</span> <span style="color: #800080;">$env</span>:computername
<span style="color: #008000;"># set this your from email address</span>
<span style="color: #800080;">$from</span> <span style="color: pink;">=</span> <span style="color: #800080;">$thishost</span><span style="color: pink;">+</span><span style="color: #800000;">&quot;-alert-notifier@yourdomain.com&quot;</span>
<span style="color: #800080;">$to</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;alertme@yourdomain.com&quot;</span>
&nbsp;
<span style="color: #008000;"># stop in forward order, start in reverse order. These are the service names, not display names.</span>
<span style="color: #800080;">$svcs</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;service0&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;service1&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;service2&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;"># stop in forward order</span>
<span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span><span style="color: pink;">=</span><span style="color: #804000;">0</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-le</span> <span style="color: #800080;">$svcs</span>.Length <span style="color: pink;">-</span> <span style="color: #804000;">1</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008080; font-weight: bold;">stop-Service</span> <span style="color: #800080;">$svcs</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#93;</span>
   <span style="color: #800080;">$service</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">get-service</span> <span style="color: #800080;">$svcs</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#93;</span>
   <span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$service</span>.status <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;Stopped&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #800080;">$dt</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span>;
      <span style="color: #800080;">$subject</span><span style="color: pink;">=</span><span style="color: #800000;">&quot;Error! unable to stop &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$svcs</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; on host $thishost on $dt.&quot;</span>
      <span style="color: #800080;">$msgtext</span><span style="color: pink;">=</span><span style="color: #800080;">$subject</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`n</span>The status is $service.status&quot;</span>
      sendEmail <span style="color: #800080;">$smpthost</span> <span style="color: #800080;">$from</span> <span style="color: #800080;">$to</span> <span style="color: #800080;">$subject</span> <span style="color: #800080;">$msgtext</span>
      exit
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># start in reverse order</span>
<span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span><span style="color: pink;">=</span><span style="color: #800080;">$svcs</span>.Length <span style="color: pink;">-</span> <span style="color: #804000;">1</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-ge</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$i</span><span style="color: pink;">--</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008080; font-weight: bold;">start-Service</span> <span style="color: #800080;">$svcs</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#93;</span> 
   <span style="color: #800080;">$service</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">get-service</span> <span style="color: #800080;">$svcs</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#93;</span> 
   <span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$service</span>.status <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;Running&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #800080;">$dt</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span>;
      <span style="color: #800080;">$subject</span><span style="color: pink;">=</span><span style="color: #800000;">&quot;Error! unable to start &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$svcs</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; on host $thishost on $dt.&quot;</span>
      <span style="color: #800080;">$msgtext</span><span style="color: pink;">=</span><span style="color: #800080;">$subject</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`n</span>The status is $service.status&quot;</span>
      sendEmail <span style="color: #800080;">$smpthost</span> <span style="color: #800080;">$from</span> <span style="color: #800080;">$to</span> <span style="color: #800080;">$subject</span> <span style="color: #800080;">$msgtext</span>
      exit
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>A few other tidbits of information:</p>
<ul>
<li>To make this run as a windows scheduled task you need to set the Task&#8217;s &#8220;Run:&#8221; field to&#8221;</li>
<pre>
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -command "&#038;{C:\fully-qualified-path\your_script.ps1}"
</pre>
<li>To run unsigned code in PowerShell you need to review PowerShell and code-signing.<br /> In PowerShell V2 you will be able to set a temporary bypass</li>
<pre>
powershell.exe –ExecutionPolicy Bypass -command "&#038;{C:\fully-qualified-path\your_script.ps1}"
</pre>
</ul>
<p>This script seems to be working pretty well. </p>
<p>BY the way, did you notice the `n above? Believe it or not, that is the PowerShell newline character. <em>What was Microsoft thinking!?</em> What was wrong with &#8220;\n&#8221; like most other languages? In case you are wondering, on the US keyboard, the ` is under the ~.</p>
]]></content:encoded>
			<wfw:commentRss>http://bable.cybermarshall.com/2009/05/22/using-powershell-to-stop-and-start-dependent-services-with-error-notification/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
