<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>CBTr.Net Tech Dump</title>
	
	<link>http://www.cbtr.net</link>
	<description />
	<lastBuildDate>Sat, 04 Sep 2010 06:57:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/cbtr" /><feedburner:info uri="cbtr" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Regex; Getting Dates</title>
		<link>http://feedproxy.google.com/~r/cbtr/~3/Xm__EJIphBM/</link>
		<comments>http://www.cbtr.net/2010/08/11/regex-getting-dates/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 21:57:30 +0000</pubDate>
		<dc:creator>Mr.M</dc:creator>
				<category><![CDATA[powershell]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expression]]></category>

		<guid isPermaLink="false">http://www.cbtr.net/?p=553</guid>
		<description><![CDATA[I&#8217;m recently reading up on Regular Expression and was having some issue getting the data I need. I have a html text file that has the following as its content: The only part of this html file I need is the YYYYMMDD section between between &#60;a href=&#8221;&#8230;&#8221;&#62;&#60;/a&#62;. I tried a few ways of stripping out [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m recently reading up on Regular Expression and was having some issue getting the data I need.  I have a html text file that has the following as its content:</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;"><ol><li class="li1"><div class="de1">&lt;tbody&gt;</div></li><li class="li1"><div class="de1">&lt;tr&gt;&lt;td class=&quot;n&quot;&gt;&lt;a href=&quot;../&quot;&gt;Parent Directory&lt;/a&gt;/&lt;/td&gt;&lt;td class=&quot;m&quot;&gt;&lt;/td&gt;&lt;td class=&quot;s&quot;&gt;- &lt;/td&gt;&lt;td class=&quot;t&quot;&gt;Directory&lt;/td&gt;&lt;/tr&gt;</div></li><li class="li1"><div class="de1">&lt;tr&gt;&lt;td class=&quot;n&quot;&gt;&lt;a href=&quot;20091103/&quot;&gt;20091103&lt;/a&gt;/&lt;/td&gt;&lt;td class=&quot;m&quot;&gt;2009-Dec-29 19:31:07&lt;/td&gt;&lt;td class=&quot;s&quot;&gt;- &lt;/td&gt;&lt;td class=&quot;t&quot;&gt;Directory&lt;/td&gt;&lt;/tr&gt;</div></li><li class="li1"><div class="de1">&lt;tr&gt;&lt;td class=&quot;n&quot;&gt;&lt;a href=&quot;20100116/&quot;&gt;20100116&lt;/a&gt;/&lt;/td&gt;&lt;td class=&quot;m&quot;&gt;2010-Jan-30 01:54:24&lt;/td&gt;&lt;td class=&quot;s&quot;&gt;- &lt;/td&gt;&lt;td class=&quot;t&quot;&gt;Directory&lt;/td&gt;&lt;/tr&gt;</div></li><li class="li1"><div class="de1">&lt;tr&gt;&lt;td class=&quot;n&quot;&gt;&lt;a href=&quot;20100130/&quot;&gt;20100130&lt;/a&gt;/&lt;/td&gt;&lt;td class=&quot;m&quot;&gt;2010-Mar-26 05:31:56&lt;/td&gt;&lt;td class=&quot;s&quot;&gt;- &lt;/td&gt;&lt;td class=&quot;t&quot;&gt;Directory&lt;/td&gt;&lt;/tr&gt;</div></li><li class="li1"><div class="de1">&lt;tr&gt;&lt;td class=&quot;n&quot;&gt;&lt;a href=&quot;20100730/&quot;&gt;20100730&lt;/a&gt;/&lt;/td&gt;&lt;td class=&quot;m&quot;&gt;2010-Aug-08 15:59:47&lt;/td&gt;&lt;td class=&quot;s&quot;&gt;- &lt;/td&gt;&lt;td class=&quot;t&quot;&gt;Directory&lt;/td&gt;&lt;/tr&gt;</div></li><li class="li1"><div class="de1">&lt;tr&gt;&lt;td class=&quot;n&quot;&gt;&lt;a href=&quot;latest/&quot;&gt;latest&lt;/a&gt;/&lt;/td&gt;&lt;td class=&quot;m&quot;&gt;2010-Aug-05 03:46:25&lt;/td&gt;&lt;td class=&quot;s&quot;&gt;- &lt;/td&gt;&lt;td class=&quot;t&quot;&gt;Directory&lt;/td&gt;&lt;/tr&gt;</div></li><li class="li1"><div class="de1">&lt;/tbody&gt;</div></li></ol></pre></div></div>
<p>The only part of this html file I need is the YYYYMMDD section between between &lt;a href=&#8221;&#8230;&#8221;&gt;&lt;/a&gt;.  I tried a few ways of stripping out the desire date as using Powershell&#8217;s -match but I found that -match has its own shortcoming and was frustrating the hell out of me.  I went to a co-worker of mine who excel in regular expression and he threw my regex syntax into <a href="http://sourceforge.net/projects/regulator/" target="_blank">Regulator </a>and it would give me the result I want, so I know my criteria is correct but Powershell doesn&#8217;t like it or not showing me what I want.</p>
<p>Thus, I ended up using the .Net regex class and was able to get my desire result:</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="re0">$c</span> <span class="sy0">=</span> <span class="kw2">gc</span> <span class="st0">&quot;.\Desktop\powershell\local_site.txt&quot;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="re3">regex</span><span class="br0">&#93;</span><span class="st0">'&lt;a\s+href=\&quot;\d+/?\&quot;&gt;(\d+)&lt;/a&gt;'</span><span class="br0">&#41;</span>.matches<span class="br0">&#40;</span><span class="re0">$c</span><span class="br0">&#41;</span> <span class="sy0">|</span> <span class="kw3">foreach</span> <span class="br0">&#123;</span><a href="about:blank"><span class="kw6">$_</span></a>.Groups<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>.Value<span class="br0">&#125;</span></div></li></ol></pre></div></div>
<p>Without the foreach loop, you will get a listing that look like this:</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="pascal" style="font-family:monospace;"><ol><li class="li1"><div class="de1">Groups   <span class="sy0">:</span> <span class="coMULTI">{20091103, 20091103}</span></div></li><li class="li1"><div class="de1">Success  <span class="sy0">:</span> <span class="kw2">True</span></div></li><li class="li1"><div class="de1">Captures <span class="sy0">:</span> <span class="coMULTI">{20091103}</span></div></li><li class="li1"><div class="de1"><span class="kw1">Index</span>    <span class="sy0">:</span> <span class="nu0">1301</span></div></li><li class="li1"><div class="de1">Length   <span class="sy0">:</span> <span class="nu0">32</span></div></li><li class="li1"><div class="de1">Value    <span class="sy0">:</span> <span class="nu0">20091103</span></div></li><li class="li1"><div class="de1">...</div></li><li class="li1"><div class="de1">...</div></li></ol></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.cbtr.net/2010/08/11/regex-getting-dates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.cbtr.net/2010/08/11/regex-getting-dates/</feedburner:origLink></item>
		<item>
		<title>Change Windows Service Password</title>
		<link>http://feedproxy.google.com/~r/cbtr/~3/W259yVYKY-s/</link>
		<comments>http://www.cbtr.net/2010/08/10/change-windows-service-password/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 00:08:18 +0000</pubDate>
		<dc:creator>Mr.M</dc:creator>
				<category><![CDATA[powershell]]></category>
		<category><![CDATA[change service password powershell]]></category>
		<category><![CDATA[changing service password]]></category>
		<category><![CDATA[changing windows service password]]></category>

		<guid isPermaLink="false">http://www.cbtr.net/?p=543</guid>
		<description><![CDATA[In my environment, I have over 100+ servers and every so many days, the account that runs SQL services are changed and it is a bitch to change it via point and click on 100+ servers. The script first look at the local machine and return services that has the [log on as] that matches [...]]]></description>
			<content:encoded><![CDATA[<p>In my environment, I have over 100+ servers and every so many days, the account that runs SQL services are changed and it is a bitch to change it via point and click on 100+ servers.  The script first look at the local machine and return services that has the [log on as] that matches PHX.  It will then go into a foreach loop and change the password for only those services.  The win32_service wmi has a [-computer] option, in which, you can use to access remote server.</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="re0">$services</span> <span class="sy0">=</span> <span class="kw1">Get-WmiObject</span> win32_service <span class="sy0">|</span> <span class="kw3">where</span> <span class="br0">&#123;</span><a href="about:blank"><span class="kw6">$_</span></a>.StartName <span class="kw4">-match</span> <span class="st0">'^PHX\\*'</span><span class="br0">&#125;</span></div></li><li class="li1"><div class="de1"><span class="re0">$pwd</span> <span class="sy0">=</span> <span class="st0">&quot;pwd_here&quot;</span></div></li><li class="li1"><div class="de1"><span class="kw3">if</span> <span class="br0">&#40;</span><span class="re0">$matches</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span> <span class="kw4">-ne</span> <span class="re0">$null</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">    <span class="kw3">foreach</span> <span class="br0">&#40;</span><span class="re0">$service</span>.Name <span class="kw3">in</span> <span class="re0">$services</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1">    <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">        <span class="kw1">write-host</span> <span class="re0">$service</span>.Name</div></li><li class="li1"><div class="de1">        <span class="re0">$service</span>.Change<span class="br0">&#40;</span><span class="re0">$null</span><span class="sy0">,</span><span class="re0">$null</span><span class="sy0">,</span><span class="re0">$null</span><span class="sy0">,</span><span class="re0">$null</span><span class="sy0">,</span><span class="re0">$null</span><span class="sy0">,</span><span class="re0">$null</span><span class="sy0">,</span><span class="re0">$null</span><span class="sy0">,</span><span class="re0">$pwd</span><span class="sy0">,</span><span class="re0">$null</span><span class="sy0">,</span><span class="re0">$null</span><span class="sy0">,</span><span class="re0">$null</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1">    <span class="br0">&#125;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol></pre></div></div>
<p>if there are no matches, the script will bomb.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cbtr.net/2010/08/10/change-windows-service-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.cbtr.net/2010/08/10/change-windows-service-password/</feedburner:origLink></item>
		<item>
		<title>PowerGUI Visual Studio Extension</title>
		<link>http://feedproxy.google.com/~r/cbtr/~3/DpX8KMyPplo/</link>
		<comments>http://www.cbtr.net/2010/08/07/powergui-visual-studio-extension/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 06:53:42 +0000</pubDate>
		<dc:creator>Mr.M</dc:creator>
				<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://www.cbtr.net/?p=535</guid>
		<description><![CDATA[I found this addin for Visual Studio this evening. Ever since I started to write powershell code, I was wondering when Microsoft was going to incorporate powershell into Visual Studio. The addin can be found here.]]></description>
			<content:encoded><![CDATA[<p>I found this addin for Visual Studio this evening.  Ever since I started to write powershell code, I was wondering when Microsoft was going to incorporate powershell into Visual Studio.  The addin can be found <a href="http://powerguivsx.codeplex.com/" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cbtr.net/2010/08/07/powergui-visual-studio-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.cbtr.net/2010/08/07/powergui-visual-studio-extension/</feedburner:origLink></item>
		<item>
		<title>Powershell WMI Explorer</title>
		<link>http://feedproxy.google.com/~r/cbtr/~3/N7kj-E637UI/</link>
		<comments>http://www.cbtr.net/2010/08/06/powershell-wmi-explorer/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 00:23:10 +0000</pubDate>
		<dc:creator>Mr.M</dc:creator>
				<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://www.cbtr.net/?p=532</guid>
		<description><![CDATA[I was looking around to fix some wmiobject errors on one of my windows 2008 r2 and came across this script. It is pretty cool. You can either download it from my site, using the icon below or directly or The PowerShell Guy]]></description>
			<content:encoded><![CDATA[<p>I was looking around to fix some wmiobject errors on one of my windows 2008 r2 and came across this script.  It is pretty cool.  You can either download it from my site, using the icon below or directly</p>
<p><a class="downloadlink dlimg" href="http://www.cbtr.net/download/WmiExplorer.zip" title=" downloaded 0 times" ><img src="http://www.cbtr.net/wp-content/plugins/download-monitor/img/arrow.png" alt="Download PowerShell WMI Explorer " /></a><br />
or<br />
<a href="http://thepowershellguy.com/blogs/posh/archive/2007/03/22/powershell-wmi-explorer-part-1.aspx">The PowerShell Guy</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cbtr.net/2010/08/06/powershell-wmi-explorer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.cbtr.net/2010/08/06/powershell-wmi-explorer/</feedburner:origLink></item>
		<item>
		<title>Syspolicy_purge_history</title>
		<link>http://feedproxy.google.com/~r/cbtr/~3/lc-hE1VwgVE/</link>
		<comments>http://www.cbtr.net/2010/08/03/syspolicy-purge-history/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 19:55:58 +0000</pubDate>
		<dc:creator>Mr.M</dc:creator>
				<category><![CDATA[sql]]></category>
		<category><![CDATA[sp_syspolicy_set_config_history_retention]]></category>
		<category><![CDATA[Syspolicy_purge_history]]></category>

		<guid isPermaLink="false">http://www.cbtr.net/?p=523</guid>
		<description><![CDATA[In SQL 2008, there&#8217;s a built in SQL job named: Syspolicy_purge_history. This sucker&#8217;s job is to purge your job history every day so that it doesn&#8217;t pile up on your server. In the past, it would take forever to get the SQL job log because it has to read all those past job run. By [...]]]></description>
			<content:encoded><![CDATA[<p>In SQL 2008, there&#8217;s a built in SQL job named: Syspolicy_purge_history.  This sucker&#8217;s job is to purge your job history every day so that it doesn&#8217;t pile up on your server.  In the past, it would take forever to get the SQL job log because it has to read all those past job run.  By default, this job purge your SQL job once a day, sometimes making it rather hard to try to identify how long a job took to complete.</p>
<p>Use the following command to see the duration:</p>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="sql" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="kw1">SELECT</span> current_value</div></li><li class="li1"><div class="de1"><span class="kw1">FROM</span> msdb<span class="sy0">.</span>dbo<span class="sy0">.</span>syspolicy_configuration</div></li><li class="li1"><div class="de1"><span class="kw1">WHERE</span> name <span class="sy0">=</span> N<span class="st0">'HistoryRetentionInDays'</span>;</div></li></ol></pre></div></div>
<p>Then use the following store proc to change the value:</p>
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="sql" style="font-family:monospace;"><ol><li class="li1"><div class="de1">exec msdb<span class="sy0">.</span>dbo<span class="sy0">.</span>sp_syspolicy_set_config_history_retention <span class="br0">&#91;</span> @value <span class="sy0">=</span> <span class="br0">&#93;</span> value</div></li></ol></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.cbtr.net/2010/08/03/syspolicy-purge-history/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.cbtr.net/2010/08/03/syspolicy-purge-history/</feedburner:origLink></item>
		<item>
		<title>Downloading Files</title>
		<link>http://feedproxy.google.com/~r/cbtr/~3/otVVHx42SUg/</link>
		<comments>http://www.cbtr.net/2010/07/30/downloading-files/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 00:14:02 +0000</pubDate>
		<dc:creator>Mr.M</dc:creator>
				<category><![CDATA[powershell]]></category>
		<category><![CDATA[$webclient.DownloadFile]]></category>
		<category><![CDATA[download file powershell function]]></category>
		<category><![CDATA[system.net.webclient]]></category>
		<category><![CDATA[system.net.webproxy]]></category>

		<guid isPermaLink="false">http://www.cbtr.net/?p=519</guid>
		<description><![CDATA[A while back, I was asked to download some files off the Internet whenever there was a change. Never wanting to manually do any work, I wrote up a Powershell function that will download the files for me. Of course, the following is just a small portion of the overall code. I have it wrapped [...]]]></description>
			<content:encoded><![CDATA[<p>A while back, I was asked to download some files off the Internet whenever there was a change.  Never wanting to manually do any work, I wrote up a Powershell function that will download the files for me.  Of course, the following is just a small portion of the overall code.  I have it wrapped in a foreach loop and it will download as many files and I have in the array.</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="kw3">function</span> download<span class="sy0">-</span>files<span class="br0">&#40;</span><span class="re0">$dldFrom</span> <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="kw3">throw</span> <span class="st0">&quot;source URL is empty&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="re0">$dldTo</span> <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="kw3">throw</span> <span class="st0">&quot;download to where?&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">	<span class="co1"># if your company doesn't need a proxy server, you don't need this part</span></div></li><li class="li1"><div class="de1">	<span class="re0">$proxy</span> <span class="sy0">=</span> <span class="kw1">new-object</span> System.Net.WebProxy <span class="st0">&quot;your_web_proxy_server:80&quot;</span></div></li><li class="li1"><div class="de1">	<span class="re0">$proxy</span>.UseDefaultCredentials <span class="sy0">=</span> <span class="re0">$true</span></div></li><li class="li1"><div class="de1">	<span class="re0">$webclient</span> <span class="sy0">=</span> <span class="kw1">new-object</span> System.Net.WebClient</div></li><li class="li1"><div class="de1">	<span class="re0">$webclient</span>.proxy <span class="sy0">=</span> <span class="re0">$proxy</span></div></li><li class="li1"><div class="de1">	<span class="re0">$webclient</span>.DownloadFile<span class="br0">&#40;</span><span class="re0">$dldFrom</span><span class="sy0">,</span> <span class="re0">$dldTo</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.cbtr.net/2010/07/30/downloading-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.cbtr.net/2010/07/30/downloading-files/</feedburner:origLink></item>
		<item>
		<title>Checking file timestamp</title>
		<link>http://feedproxy.google.com/~r/cbtr/~3/8fMbsi2FAQs/</link>
		<comments>http://www.cbtr.net/2010/07/23/checking-file-timestamp/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 19:39:40 +0000</pubDate>
		<dc:creator>Mr.M</dc:creator>
				<category><![CDATA[powershell]]></category>
		<category><![CDATA[check file timestamp]]></category>
		<category><![CDATA[do() ... until()]]></category>
		<category><![CDATA[do() ... until() loop]]></category>
		<category><![CDATA[get-item]]></category>
		<category><![CDATA[send-mailmessage cmdlet]]></category>

		<guid isPermaLink="false">http://www.cbtr.net/?p=514</guid>
		<description><![CDATA[A while back, a dev lead came over and asked me if I can write a script to monitor a file. Check to see if the file is older than 30minutes. If it is older than 30 minutes, an email should be to sent to a certain people and distribution list. I came up with [...]]]></description>
			<content:encoded><![CDATA[<p>A while back, a dev lead came over and asked me if I can write a script to monitor a file.  Check to see if the file is older than 30minutes.  If it is older than 30 minutes, an email should be to sent to a certain people and distribution list.  I came up with the following.</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="co1"># Get file properties n compare time difference</span></div></li><li class="li1"><div class="de1"><span class="re0">$hotlist</span> <span class="sy0">=</span> <span class="kw1">get-item</span> <span class="st0">&quot;\path\to\file\filename.txt&quot;</span></div></li><li class="li1"><div class="de1"><span class="re0">$now</span> <span class="sy0">=</span> <span class="br0">&#91;</span>datetime<span class="br0">&#93;</span>::now</div></li><li class="li1"><div class="de1"><span class="re0">$diff</span> <span class="sy0">=</span> <span class="re0">$now</span> <span class="sy0">-</span> <span class="re0">$hotlist</span>.LastWriteTime</div></li><li class="li1"><div class="de1"><span class="kw1">write-output</span> <span class="st0">&quot;Time Diff: &quot;</span> <span class="re0">$diff</span>.Minutes</div></li><li class="li1"><div class="de1"><span class="kw3">if</span> <span class="br0">&#40;</span><span class="re0">$diff</span>.Minutes <span class="kw4">-ge</span> <span class="nu0">30</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">    <span class="st0">&quot;sending mail&quot;</span></div></li><li class="li1"><div class="de1">    send<span class="sy0">-</span>email <span class="re0">$emailaddress</span> `</div></li><li class="li1"><div class="de1">    <span class="st0">&quot; - filename.txt is older than 30 minutes&quot;</span> `</div></li><li class="li1"><div class="de1">    <span class="st0">&quot;Please open a ticket to address/investigate why the txt file is older than 30 minutes. The location of the file is: \path\to\file&quot;</span></div></li><li class="li1"><div class="de1">    <span class="co1"># Keep checking the file until it is -lt 5 minutes</span></div></li><li class="li1"><div class="de1">    <span class="kw3">do</span></div></li><li class="li1"><div class="de1">    <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">        <span class="st0">&quot;checking timestamp&quot;</span></div></li><li class="li1"><div class="de1">        <span class="kw2">sleep</span> 60</div></li><li class="li1"><div class="de1">        <span class="re0">$hotlist</span> <span class="sy0">=</span> <span class="kw1">get-item</span> <span class="st0">&quot;\path\to\file\filename.txt&quot;</span></div></li><li class="li1"><div class="de1">        <span class="re0">$now</span> <span class="sy0">=</span> <span class="br0">&#91;</span>datetime<span class="br0">&#93;</span>::now</div></li><li class="li1"><div class="de1">        <span class="re0">$diff</span> <span class="sy0">=</span> <span class="re0">$now</span> <span class="sy0">-</span> <span class="re0">$hotlist</span>.LastWriteTime</div></li><li class="li1"><div class="de1">    <span class="br0">&#125;</span></div></li><li class="li1"><div class="de1">    <span class="kw3">until</span> <span class="br0">&#40;</span><span class="re0">$diff</span>.Minutes <span class="kw4">-lt</span> <span class="nu0">5</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1">    <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">        <span class="st0">&quot;sending green email&quot;</span></div></li><li class="li1"><div class="de1">        send<span class="sy0">-</span>email <span class="re0">$emailaddress</span> `</div></li><li class="li1"><div class="de1">        <span class="st0">&quot; - filename.txt up-to-date&quot;</span> `</div></li><li class="li1"><div class="de1">        <span class="st0">&quot;The txt file has been updated less than 1 minute ago&quot;</span></div></li><li class="li1"><div class="de1">    <span class="br0">&#125;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol></pre></div></div>
<p>The send-email is a function I wrote which utilize powershell&#8217;s Send-MailMessage cmdlet.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cbtr.net/2010/07/23/checking-file-timestamp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.cbtr.net/2010/07/23/checking-file-timestamp/</feedburner:origLink></item>
		<item>
		<title>Stopping Windows Service</title>
		<link>http://feedproxy.google.com/~r/cbtr/~3/zASveqUjm6w/</link>
		<comments>http://www.cbtr.net/2010/07/22/stopping-windows-service/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 00:39:35 +0000</pubDate>
		<dc:creator>Mr.M</dc:creator>
				<category><![CDATA[powershell]]></category>
		<category><![CDATA[checking windows service]]></category>
		<category><![CDATA[get-service]]></category>
		<category><![CDATA[stop-service]]></category>

		<guid isPermaLink="false">http://www.cbtr.net/?p=504</guid>
		<description><![CDATA[After patching Windows servers, I had to check whether or not the firewall service is enabled. If it is, they need to be disable. Enjoy.]]></description>
			<content:encoded><![CDATA[<p>After patching Windows servers, I had to check whether or not the firewall service is enabled.  If it is, they need to be disable.</p>
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="re0">$servers</span> <span class="sy0">=</span> <span class="kw2">gc</span> <span class="st0">&quot;D:\Patch\bin\not_useServers.txt&quot;</span></div></li><li class="li1"><div class="de1"><span class="re0">$serviceName</span> <span class="sy0">=</span> <span class="st0">&quot;MpsSvc&quot;</span></div></li><li class="li1"><div class="de1"><span class="kw3">foreach</span><span class="br0">&#40;</span><span class="re0">$server</span> <span class="kw3">in</span> <span class="re0">$servers</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">    <span class="re0">$service</span> <span class="sy0">=</span> <span class="kw1">get-service</span> <span class="kw5">-computername</span> <span class="re0">$server</span> <span class="kw5">-name</span> <span class="re0">$serviceName</span></div></li><li class="li1"><div class="de1">    <span class="kw3">if</span><span class="br0">&#40;</span><span class="re0">$service</span>.Status <span class="kw4">-eq</span> <span class="st0">&quot;Running&quot;</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1">    <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">        <span class="kw1">write-output</span> <span class="st0">&quot;Windows firewall is running on: $($server).  Attempting to stop it.&quot;</span></div></li><li class="li1"><div class="de1">        <span class="re0">$service</span> <span class="sy0">|</span> <span class="kw1">stop-service</span></div></li><li class="li1"><div class="de1">    <span class="br0">&#125;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol></pre></div></div>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cbtr.net/2010/07/22/stopping-windows-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.cbtr.net/2010/07/22/stopping-windows-service/</feedburner:origLink></item>
		<item>
		<title>pinging servers</title>
		<link>http://feedproxy.google.com/~r/cbtr/~3/hGnwLdUE7pQ/</link>
		<comments>http://www.cbtr.net/2010/07/21/pinging-servers/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 23:32:44 +0000</pubDate>
		<dc:creator>Mr.M</dc:creator>
				<category><![CDATA[powershell]]></category>
		<category><![CDATA[cbtr]]></category>
		<category><![CDATA[cbtr.net]]></category>
		<category><![CDATA[get-wmiobject win32_pingstatus]]></category>
		<category><![CDATA[ping a server]]></category>
		<category><![CDATA[ping server powershell]]></category>
		<category><![CDATA[powershell ping server]]></category>
		<category><![CDATA[send-mailmessage]]></category>
		<category><![CDATA[sending email]]></category>
		<category><![CDATA[win32_pingstatus]]></category>
		<category><![CDATA[win32_pingstatus statuscode]]></category>
		<category><![CDATA[www.cbtr.net]]></category>

		<guid isPermaLink="false">http://www.cbtr.net/?p=468</guid>
		<description><![CDATA[On my SQL server, I wrote a stored procedure that ping servers and if the result come back as &#8220;request not found&#8221; an email will be sent. It is a pretty ugly looking script, use of temp table, memory table, etc, and was slated to run every 45 seconds. Looking at the system recompiled plan, [...]]]></description>
			<content:encoded><![CDATA[<p>On my SQL server, I wrote a stored procedure that ping servers and if the result come back as &#8220;request not found&#8221; an email will be sent.  It is a pretty ugly looking script, use of temp table, memory table, etc, and was slated to run every 45 seconds. Looking at the system recompiled plan, this stored procedure dominated the list.  Not that it was doing anything to the server because it was an ops server, meaning it is lightly used.  Heck, I hardly log onto that server now a days.</p>
<p>Keen on converting anything I can into powershell, I wrote a little function to ping servers.  The following function will take one or more servers.  Using a foreach loop to run thru the array, any server w/ statusCode != 0, an email will be sent, using the send-mail function I wrote.</p>
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="kw3">function</span> ping<span class="sy0">-</span>server<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="re3">string</span><span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#93;</span><span class="re0">$servers</span> <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="kw3">throw</span> <span class="st0">&quot;Nothing to Ping!&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="re0">$emailaddress</span> <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="kw3">throw</span> <span class="st0">&quot;empty email list!&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">	<span class="kw3">foreach</span><span class="br0">&#40;</span><span class="re0">$server</span> <span class="kw3">in</span> <span class="re0">$servers</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1">	<span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">		<span class="re0">$result</span> <span class="sy0">=</span> <span class="kw1">get-wmiobject</span> win32_pingStatus <span class="kw4">-f</span> <span class="st0">&quot;address = '$($server)'&quot;</span></div></li><li class="li1"><div class="de1">		<span class="kw3">if</span> <span class="br0">&#40;</span><span class="re0">$result</span>.StatusCode <span class="kw4">-ne</span> 0<span class="br0">&#41;</span></div></li><li class="li1"><div class="de1">		<span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">		send<span class="sy0">-</span>email <span class="re0">$emailaddress</span> <span class="st0">&quot;$($server) is unavailable &quot;</span> <span class="st0">&quot;.&quot;</span></div></li><li class="li1"><div class="de1">		<span class="br0">&#125;</span></div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol></pre></div></div>
<p>The send-email is a function I put together yesterday.  You can find it <a href="http://www.cbtr.net/2010/07/20/params-and-invoke-command/" target="_blank">here</a> at the bottom of the post.</p>
<div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1">Usage:</div></li><li class="li1"><div class="de1"><span class="co1">#import the function</span></div></li><li class="li1"><div class="de1">. .\ping_server.ps1</div></li><li class="li1"><div class="de1"><span class="co1">#declaring variables</span></div></li><li class="li1"><div class="de1"><span class="re0">$servers</span> <span class="sy0">=</span> <span class="kw1">get-content</span> <span class="st0">&quot;d:\listOfServers.txt&quot;</span></div></li><li class="li1"><div class="de1"><span class="re0">$emailaddress</span> <span class="sy0">=</span> <span class="st0">&quot;abc@xyz.com&quot;</span><span class="sy0">,</span><span class="st0">&quot;def@xyz.com&quot;</span></div></li><li class="li1"><div class="de1"><span class="co1">#making use of the function</span></div></li><li class="li1"><div class="de1">ping<span class="sy0">-</span>server <span class="re0">$servers</span> $emailaddress</div></li></ol></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.cbtr.net/2010/07/21/pinging-servers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.cbtr.net/2010/07/21/pinging-servers/</feedburner:origLink></item>
		<item>
		<title>params and invoke-command</title>
		<link>http://feedproxy.google.com/~r/cbtr/~3/gVdMfYulxv4/</link>
		<comments>http://www.cbtr.net/2010/07/20/params-and-invoke-command/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 19:52:55 +0000</pubDate>
		<dc:creator>Mr.M</dc:creator>
				<category><![CDATA[powershell]]></category>
		<category><![CDATA[cbtr]]></category>
		<category><![CDATA[cbtr.net]]></category>
		<category><![CDATA[invoke-command]]></category>
		<category><![CDATA[invoke-expression]]></category>
		<category><![CDATA[param and invoke-command]]></category>
		<category><![CDATA[parameters and invoke-command]]></category>
		<category><![CDATA[passing parameters into invoke-command]]></category>
		<category><![CDATA[send-mailmessage]]></category>

		<guid isPermaLink="false">http://www.cbtr.net/?p=458</guid>
		<description><![CDATA[Yesterday, I was working with invoke-command in powershell. I was basically making a call to a remote server that can send emails. The command look like so: This worked as expected; But I wanted to pass the value of a variable ($url) into the subject line or body of the email: I got the email [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I was working with invoke-command in powershell.  I was basically making a call to a remote server that can send emails.  The command look like so:</p>
<div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1">invoke<span class="sy0">-</span>command <span class="kw5">-computername</span> smtp1 <span class="br0">&#123;</span>send<span class="sy0">-</span>mailmessage <span class="sy0">-</span>from <span class="st0">&quot;abc@def.com&quot;</span> `</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>to <span class="st0">&quot;xyz@gogo123.com&quot;</span> `</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>subject <span class="st0">&quot;Regarding: http://www.cbtr.net&quot;</span> `</div></li><li class="li1"><div class="de1">    <span class="kw5">-body</span> <span class="st0">&quot;New section has been added: Powershell &quot;</span>`</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>smtpserver <span class="st0">&quot;some.smtp.server&quot;</span><span class="br0">&#125;</span></div></li></ol></pre></div></div>
<p>This worked as expected; But I wanted to pass the value of a variable ($url) into the subject line or body of the email:</p>
<div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="re0">$url</span> <span class="sy0">=</span> <span class="st0">&quot;http://www.cbtr.net&quot;</span></div></li><li class="li1"><div class="de1">invoke<span class="sy0">-</span>command <span class="kw5">-computername</span> smtp1 <span class="br0">&#123;</span>send<span class="sy0">-</span>mailmessage <span class="sy0">-</span>from <span class="st0">&quot;abc@def.com&quot;</span> `</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>to <span class="st0">&quot;xyz@gogo123.com&quot;</span> `</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>subject <span class="st0">&quot;Regarding: $($url)&quot;</span> `</div></li><li class="li1"><div class="de1">    <span class="kw5">-body</span> <span class="st0">&quot;New section has been added: Powershell &quot;</span>`</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>smtpserver <span class="st0">&quot;some.smtp.server&quot;</span><span class="br0">&#125;</span></div></li></ol></pre></div></div>
<p>I got the email for this one as well except that the subject of the email was [Regarding:] and was missing &#8220;http://www.cbtr.net.&#8221;</p>
<p>I&#8217;ve tried to set the $url to be a global variable ($global:url = &#8220;http://www.cbtr.net&#8221;) and calling it in the scriptblock of the invoke-command but that didn&#8217;t work. So much for the global variable idea.  I finally caved in and used the param within the script block:</p>
<div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="re0">$url</span> <span class="sy0">=</span> <span class="st0">&quot;http://www.cbtr.net&quot;</span></div></li><li class="li1"><div class="de1">invoke<span class="sy0">-</span>command <span class="kw5">-computername</span> smtp1 <span class="br0">&#123;</span><span class="kw3">param</span><span class="br0">&#40;</span><span class="re0">$url</span><span class="br0">&#41;</span> send<span class="sy0">-</span>mailmessage <span class="sy0">-</span>from <span class="st0">&quot;abc@def.com&quot;</span> `</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>to <span class="st0">&quot;xyz@gogo123.com&quot;</span> `</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>subject <span class="st0">&quot;Regarding: $($url)&quot;</span> `</div></li><li class="li1"><div class="de1">    <span class="kw5">-body</span> <span class="st0">&quot;New section has been added: Powershell &quot;</span>`</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>smtpserver <span class="st0">&quot;some.smtp.server&quot;</span><span class="br0">&#125;</span> <span class="kw5">-ArgumentList</span> $url</div></li></ol></pre></div></div>
<p>This worked.  It sent me the result I was looking for; but I wanted to try another method.</p>
<div id="wpshdo_15" class="wp-synhighlighter-outer"><div id="wpshdt_15" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_15"></a><a id="wpshat_15" class="wp-synhighlighter-title" href="#codesyntax_15"  onClick="javascript:wpsh_toggleBlock(15)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_15" onClick="javascript:wpsh_code(15)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_15" onClick="javascript:wpsh_print(15)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_15" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="re0">$url</span> <span class="sy0">=</span> <span class="st0">&quot;http://www.cbtr.net&quot;</span></div></li><li class="li1"><div class="de1"><span class="re0">$command</span> <span class="sy0">=</span> <span class="st0">&quot;invoke-command -computername smtp1 {write-host <span class="es0">`&quot;</span>hello: $($url)<span class="es0">`&quot;</span>}&quot;</span></div></li><li class="li1"><div class="de1"><span class="re0">$command</span> <span class="sy0">|</span> <span class="kw1">invoke-expression</span></div></li></ol></pre></div></div>
<p>This work as well except that I didn&#8217;t want to send an email.  This last method can get real ugly real fast because you have to make use of the escape char (`).</p>
<p>In fact, I&#8217;ve made a function out of it:</p>
<div id="wpshdo_16" class="wp-synhighlighter-outer"><div id="wpshdt_16" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_16"></a><a id="wpshat_16" class="wp-synhighlighter-title" href="#codesyntax_16"  onClick="javascript:wpsh_toggleBlock(16)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_16" onClick="javascript:wpsh_code(16)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_16" onClick="javascript:wpsh_print(16)" title="Print code"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.cbtr.net/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_16" class="wp-synhighlighter-inner" style="display: block;"><pre class="powershell" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="kw3">function</span> send<span class="sy0">-</span>email<span class="br0">&#40;</span><span class="re0">$from</span><span class="sy0">,</span> <span class="br0">&#91;</span><span class="re3">string</span><span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#93;</span><span class="re0">$to</span><span class="sy0">,</span> <span class="re0">$subject</span> <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="kw3">throw</span> <span class="st0">&quot;subject required&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="re0">$body</span> <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="kw3">throw</span> <span class="st0">&quot;body required&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">    <span class="co1"># eg: $a = &quot;abc@xyz.com&quot;,&quot;gef@xyz.com&quot;</span></div></li><li class="li1"><div class="de1">    <span class="co1"># eg: send-email &quot;opq@xyz.com&quot; $a &quot;Subject&quot; &quot;Body&quot;</span></div></li><li class="li1"><div class="de1">    invoke<span class="sy0">-</span>command <span class="kw5">-computername</span> servername <span class="br0">&#123;</span><span class="kw3">param</span><span class="br0">&#40;</span><span class="re0">$from</span><span class="sy0">,</span> <span class="re0">$to</span><span class="sy0">,</span> <span class="re0">$subject</span><span class="sy0">,</span> <span class="re0">$body</span><span class="br0">&#41;</span> send<span class="sy0">-</span>mailmessage <span class="sy0">-</span>from <span class="re0">$from</span> `</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>to <span class="re0">$to</span> `</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>subject <span class="re0">$subject</span> `</div></li><li class="li1"><div class="de1">    <span class="kw5">-body</span> <span class="re0">$body</span> `</div></li><li class="li1"><div class="de1">    <span class="sy0">-</span>smtpserver <span class="st0">&quot;some.smtp1.server&quot;</span><span class="br0">&#125;</span> <span class="kw5">-ArgumentList</span> <span class="re0">$from</span><span class="sy0">,</span> <span class="re0">$to</span><span class="sy0">,</span> <span class="re0">$subject</span><span class="sy0">,</span> <span class="re0">$body</span></div></li><li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.cbtr.net/2010/07/20/params-and-invoke-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.cbtr.net/2010/07/20/params-and-invoke-command/</feedburner:origLink></item>
	</channel>
</rss>
