<?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; Shell</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/shell/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>More Powershell Nuggets</title>
		<link>http://www.youdidwhatwithtsql.com/more-powershell-nuggets/239</link>
		<comments>http://www.youdidwhatwithtsql.com/more-powershell-nuggets/239#comments</comments>
		<pubDate>Mon, 06 Jul 2009 15:47:57 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/more-powershell-nuggets/239</guid>
		<description><![CDATA[In a previous post I provided a few small Powershell code blocks suitable for beginners to digest. Here are a few more that anyone starting with Powershell might like to experiment with. A simple For Loop in Powershell Here’s just a simple for loop in Powershell. ?View Code POWERSHELL# A simple for loop for&#40;$i = [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/more-powershell-nuggets/239">More Powershell Nuggets</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.youdidwhatwithtsql.com/powershell-nuggets/72" target="_blank">previous post</a> I provided a few small <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> code blocks suitable for beginners to digest. Here are a few more that anyone starting with <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> might like to experiment with.</p>
<p><strong>A simple For Loop in Powershell</strong></p>
<p><font color="#666666">Here’s just a simple for loop in Powershell.</font></p>

<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('p239code8'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2398"><td class="code" id="p239code8"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># A simple for loop</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: #804000;">10</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;">Write-Host</span> <span style="color: #800000;">&quot;Loop = $i&quot;</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="For Loop in Powershell" border="0" alt="image thumb3 More Powershell Nuggets" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb3.png" width="644" height="380" /></a> </p>
<p><strong><font color="#666666"></font></strong></p>
<p><strong>A simple For Loop using an array in Powershell</strong></p>
<p><font color="#666666">Like all modern scripting languages, <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> offers us an easy way to iterate over arrays.</font></p>

<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('p239code9'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2399"><td class="code" id="p239code9"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Setup an array</span>
<span style="color: #800080;">$array</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: pink;">,</span> <span style="color: #804000;">3</span><span style="color: pink;">,</span> <span style="color: #804000;">4</span><span style="color: pink;">,</span> <span style="color: #804000;">5</span><span style="color: pink;">,</span> <span style="color: #804000;">6</span><span style="color: pink;">,</span> <span style="color: #804000;">7</span><span style="color: pink;">,</span> <span style="color: #804000;">8</span><span style="color: pink;">,</span> <span style="color: #804000;">9</span><span style="color: pink;">,</span> <span style="color: #804000;">10</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008000;"># A foreach loop using an array</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$item</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$array</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;">'Array item ='</span> <span style="color: #800080;">$item</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Iterating over an array with Powershell" border="0" alt="image thumb4 More Powershell Nuggets" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb4.png" width="644" height="380" /></a> </p>
<p><strong>Iterate recursively through a directory structure with Powershell</strong></p>
<p>This nugget iterates through the user profile folder structure, <strong>C:\Users\Rhys</strong> on my laptop, and reports if each txt file encountered is less than 100 bytes or greater than 100 bytes.</p>

<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('p239code10'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23910"><td class="code" id="p239code10"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$dir</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">dir</span> <span style="color: #008080; font-style: italic;">-Recurse</span> <span style="color: #800080;">$Env</span>:USERPROFILE <span style="color: pink;">*</span>.txt;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$file</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$dir</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$bytes</span> <span style="color: pink;">=</span> <span style="color: #800080;">$file</span>.Length;
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$bytes</span> <span style="color: #FF0000;">-lt</span> <span style="color: #804000;">100</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: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$file &amp;lt; 100 bytes.&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: #008080; font-style: italic;">-ForegroundColor</span> Cyan <span style="color: #800000;">&quot;$file &amp;gt;= 100 bytes.&quot;</span>;	
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image5.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Directory recursion with Powershell" border="0" alt="image thumb5 More Powershell Nuggets" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb5.png" width="644" height="350" /></a> </p>
<p><strong>Find all .mdf and .ldf files on your C:\ drive with Powershell</strong></p>
<p><font color="#666666">These two simple lines will search through your <strong>C:\</strong> drive looking for .mdf and .ldf files. You can easily search for different files by changing the extensions specified in the –<strong>Include </strong>switch.</font></p>

<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('p239code11'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23911"><td class="code" id="p239code11"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Find all mdf and ldf files in C:\ on Local machine</span>
<span style="color: #800080;">$textFiles</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Path</span> C:\ <span style="color: #008080; font-style: italic;">-Recurse</span> <span style="color: pink;">*</span> <span style="color: #008080; font-style: italic;">-Include</span> <span style="color: #800000;">&quot;*.mdf&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;*.ldf&quot;</span>;
<span style="color: #800080;">$textFiles</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Format-Table</span> <span style="color: #008080; font-style: italic;">-AutoSize</span> <span style="color: #008080; font-style: italic;">-Property</span> Name<span style="color: pink;">,</span> Length;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image6.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Find files by extension with Powershell" border="0" alt="image thumb6 More Powershell Nuggets" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb6.png" width="644" height="361" /></a> </p>
<p><strong>Find all .mdf and .ldf files on a remote Computer with Powershell</strong></p>
<p>This example is similar in function to the last one, except we’re using <a href="http://msdn.microsoft.com/en-us/library/aa394572(VS.85).aspx" target="_blank">WMI</a> here to query remote computers. The <strong>-ComputerName</strong> switch specifies the computer we wish to query and the <strong>-Filter</strong> switch specifies the extensions we wish to search for.</p>

<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('p239code12'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23912"><td class="code" id="p239code12"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Find all mdf and ldf files on a remote host</span>
<span style="color: #800080;">$dbFiles</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> CIM_DataFile <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Extension = 'mdf' OR Extension = 'ldf'&quot;</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800000;">&quot;localhost&quot;</span>;
<span style="color: #800080;">$dbFiles</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">ForEach-Object</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #000080;">$_</span>.Name; <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image7.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Find files on remote computers with Powershell" border="0" alt="image thumb7 More Powershell Nuggets" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb7.png" width="644" height="366" /></a> </p>
<p><strong>Query Processes on local and remote Computers with Powershell</strong></p>
<p><font color="#666666">Just a few lines of code in <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> allow you to do some pretty powerful things. Beginners should definitely check out the <a href="http://technet.microsoft.com/en-us/library/dd315295.aspx" target="_blank">Get-WmiObject</a> cmdlet and see how useful it is. The first line of code here creates an instance of the Win32_Process class. <strong>ComputerName</strong> specifies the computer you wish to query. The <strong>Filter </strong>flag contains the criteria by which we wish to filter the data. This is just like the WHERE clause in SQL. The second line simply takes the output and formats it nicely including only the columns we specify.</font></p>

<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('p239code13'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23913"><td class="code" id="p239code13"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$Processes</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_Process <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800000;">'localhost'</span> <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;PageFileUsage &amp;gt; 0 AND Name Like '%s%'&quot;</span>;
<span style="color: #800080;">$Processes</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Format-Table</span> <span style="color: #008080; font-style: italic;">-AutoSize</span> <span style="color: #008080; font-style: italic;">-Property</span> Name<span style="color: pink;">,</span> ProcessId<span style="color: pink;">,</span> ThreadCount<span style="color: pink;">,</span> PageFileUsage<span style="color: pink;">,</span> PeakPageFileUsage<span style="color: pink;">,</span> PeakVirtualSize<span style="color: pink;">,</span> PeakWorkingSetSize;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image8.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Query Processes on local and remote computers with Powershell" border="0" alt="image thumb8 More Powershell Nuggets" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb8.png" width="644" height="373" /></a></p>
<p><strong>Kill a Process on a remote Computer with Powershell</strong></p>
<p><font color="#666666">The script is setup to kill <strong>calc.exe </strong>on <strong>localhost </strong>but it can be changed to run for any computer, or process, that you have appropriate permissions for. To test this script make sure you have <strong>calc.exe </strong>running.</font></p>

<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('p239code14'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23914"><td class="code" id="p239code14"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Kill a process on a remote machine</span>
<span style="color: #800080;">$computer</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span>;
<span style="color: #800080;">$processToKill</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;calc.exe&quot;</span>;
<span style="color: #800080;">$process</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_Process <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Name = '$processToKill'&quot;</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$computer</span>;
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$process</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>	<span style="color: #008000;"># If null then the process may not be running</span>
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;Couldn't get process $processToKill on $computer&quot;</span>;
	<span style="color: #008080; font-weight: bold;">sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">10</span><span style="color: #000000;">&#41;</span>;
	exit;
<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;Attempting to Kill $processToKill on $computer&quot;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #008000;"># Kill the process and get exit status 0 = OK</span>
<span style="color: #800080;">$status</span> <span style="color: pink;">=</span> <span style="color: #800080;">$process</span>.InvokeMethod<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Terminate&quot;</span><span style="color: pink;">,</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>;
<span style="color: #0000FF;">switch</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$status</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #804000;">0</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;Killed $processToKill on $computer&quot;</span><span style="color: #000000;">&#125;</span>;
	default <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;Error, couldn't kill $processToKill on $computer&quot;</span><span style="color: #000000;">&#125;</span>;
&nbsp;
<span style="color: #000000;">&#125;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image9.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Kill processes on remote computers with Powershell" border="0" alt="image thumb9 More Powershell Nuggets" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb9.png" width="644" height="380" /></a></p>
<p><map name='google_ad_map_239_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/239?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_239_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=239&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmore-powershell-nuggets%2F239' title="More Powershell Nuggets" alt=" More Powershell Nuggets" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/more-powershell-nuggets/239">More Powershell Nuggets</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/more-powershell-nuggets/239" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/more-powershell-nuggets/239/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Powershell Nuggets</title>
		<link>http://www.youdidwhatwithtsql.com/powershell-nuggets/72</link>
		<comments>http://www.youdidwhatwithtsql.com/powershell-nuggets/72#comments</comments>
		<pubDate>Wed, 22 Apr 2009 19:48:56 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[Server Management]]></category>
		<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/powershell-nuggets/72</guid>
		<description><![CDATA[Here are a few Powershell nuggets for beginners to digest. First thing that might trip you up is the Execution Policy built into Powershell. I’ve turned this off on my development machine but it’s obviously advised to have it enabled on production machines. That aside, it’s easy to turn off… If you’re using Vista you [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/powershell-nuggets/72">Powershell Nuggets</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here are a few <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> nuggets for beginners to digest.</p>
<p>First thing that might trip you up is the <a href="http://myitforum.com/cs2/blogs/dhite/archive/2006/09/08/Configuring-PowerShell-Execution-Policies.aspx" target="_blank">Execution Policy</a> built into <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>. I’ve turned this off on my development machine but it’s obviously advised to have it enabled on production machines. That aside, it’s easy to turn off…</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/04/image3.png"><img title="Powershell Execution Policy" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="131" alt="image thumb3 Powershell Nuggets" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/04/image-thumb3.png" width="244" border="0" /></a> </p>
<p>If you’re using <a href="http://www.microsoft.com/windows/windows-vista/default.aspx" target="_blank">Vista</a> you may need to run Powershell in <a href="http://support.microsoft.com/kb/922708" target="_blank">Administrator mode</a> to allow the required registry change. To make my environment a little more user friendly I’ve setup a <a href="http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/profile.mspx" target="_blank">Powershell Profile</a> with the following code</p>

<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('p72code21'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7221"><td class="code" id="p72code21"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">cd</span> C:\Users\Rhys\Documents\powershell
<span style="color: #0000FF;">function</span> prompt <span style="color: #000000;">&#123;</span><span style="color: #800000;">&quot;PS: $(get-date)&amp;gt;&quot;</span><span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>This changes the current working directory to my scripts folder and keeps the prompt reasonably short. </p>
<p><strong>Write text files with Powershell</strong></p>
<p>Just look how easy it is to write text files in <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>.</p>

<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('p72code22'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7222"><td class="code" id="p72code22"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$text</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;abcdefghijklmnopqrstuvwxyz1234567890&quot;</span>
&nbsp;
<span style="color: #008000;"># Method 1: Write file using set-content. </span>
<span style="color: #008000;"># Just pipe the contents of the variable</span>
<span style="color: #800080;">$text</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">set-content</span> <span style="color: #008080; font-style: italic;">-encoding</span> ascii alphabet.txt
&nbsp;
<span style="color: #008000;"># Method 2: Use out-file to append the for loop index to a file</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;">-lt</span> <span style="color: #804000;">10</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: #800080;">$i</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">out<span style="color: #FF0000;">-file</span></span> <span style="color: #008080; font-style: italic;">-filePath</span> <span style="color: #800000;">&quot;forLoop.txt&quot;</span> <span style="color: #008080; font-style: italic;">-encoding</span> ascii <span style="color: #008080; font-style: italic;">-append</span> 
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>After executing this you should have 2 new .txt files in your working directory.</p>
<p><strong>List &amp; Count Files by Extension with Powershell</strong></p>
<p>This script will list filenames and their sizes before displaying a total count of those files. Just change the extension in the script if you want to work with different file types.</p>

<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('p72code23'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7223"><td class="code" id="p72code23"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">#####################################################</span>
<span style="color: #008000;"># Filename: DirectoryTextFiles.ps1		    #</span>
<span style="color: #008000;"># Author: Rhys Campbell				    #</span>
<span style="color: #008000;"># Description: Lists all .txt files in the current  #</span>
<span style="color: #008000;"># directory, with their length in bytes, and        #</span>
<span style="color: #008000;"># displays a total count of .txt files.		    #</span>
<span style="color: #008000;"># Date: 2009-04-22				    #</span>
<span style="color: #008000;">#####################################################</span>
&nbsp;
<span style="color: #800080;">$count</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #800080;">$files</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">dir</span> <span style="color: pink;">*</span>.txt
&nbsp;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$file</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$files</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$count</span><span style="color: pink;">++</span>;
	<span style="color: #800080;">$filename</span> <span style="color: pink;">=</span> <span style="color: #800080;">$file</span>.name
	<span style="color: #800080;">$fileLength</span> <span style="color: pink;">=</span> <span style="color: #800080;">$file</span>.Length 
	<span style="color: #008080; font-weight: bold;">echo</span> <span style="color: #800000;">&quot;The text file $filename is $fileLength bytes.&quot;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-weight: bold;">echo</span> <span style="color: #800000;">&quot;====================================================&quot;</span>
<span style="color: #008080; font-weight: bold;">echo</span> <span style="color: #800000;">&quot;Total text files in this directory = $count&quot;</span>;</pre></td></tr></table></div>

<p><strong>Get the Twitter Public Timeline with Powershell</strong></p>
<p>One line of code is all is takes with <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>!</p>

<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('p72code24'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7224"><td class="code" id="p72code24"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">xml</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">new-object</span> net.webclient<span style="color: #000000;">&#41;</span>.DownloadString<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;http://twitter.com/statuses/public_timeline.rss&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.rss.channel.item <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">format-table</span> title<span style="color: pink;">,</span>link</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/04/image4.png"><img title="The Twitter Public Timeline in Powershell" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="110" alt="image thumb4 Powershell Nuggets" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/04/image-thumb4.png" width="244" border="0" /></a> </p>
<p><strong>Ping multiple computers with Powershell</strong></p>

<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('p72code25'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7225"><td class="code" id="p72code25"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">###############################################</span>
<span style="color: #008000;"># Filename: ComputerPinger.ps1		      #</span>
<span style="color: #008000;"># Author: Rhys Campbell			      #</span>
<span style="color: #008000;"># Description: Pings multiple computers       #</span>
<span style="color: #008000;"># supplied in the $args array.		      #</span>
<span style="color: #008000;"># Date: 2009-04-16			      #</span>
<span style="color: #008000;">###############################################</span>
&nbsp;
<span style="color: #008000;"># Simple ping for each computer</span>
<span style="color: #008000;"># supplied in the $args array</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: #000080;">$args</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	ping <span style="color: #800080;">$computer</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Count Databases On Multiple Servers</strong></p>
<p>Those of you that are still with me get to see the exciting stuff. This script will count databases, and see if <a href="http://msdn.microsoft.com/en-us/library/ms189237.aspx" target="_blank">SQLAGENT</a> is running, on multiple servers. Just specify each server on the command line. This script uses Windows authentication so bear this in mind for each of your servers. It’s with examples like this that I hope you start to see the power and possibilities of <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>.</p>

<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('p72code26'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7226"><td class="code" id="p72code26"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">################################################################################</span>
<span style="color: #008000;"># Filename: MultiHostDatabaseCount.ps1					       #</span>
<span style="color: #008000;"># Description: 								       #</span>
<span style="color: #008000;"># Author: Rhys Campbell							       #</span>
<span style="color: #008000;"># Date: 2009-04-15							       #</span>
<span style="color: #008000;"># Usage: FullPathToFile\MultiHostDatabaseCount.ps1 server1 server2 server3 etc #</span>
<span style="color: #008000;">################################################################################</span>
&nbsp;
<span style="color: #0000FF;">trap</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800000;">&quot;Eek! An exception occured.&quot;</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.GetType<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.FullName<span style="color: #000000;">&#41;</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: #008000;"># Terminate the script on error</span>
	exit
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Loop through each server name supplied in the $args array</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$arg</span> <span style="color: #0000FF;">in</span> <span style="color: #000080;">$args</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># Connection string</span>
	<span style="color: #800080;">$connectionString</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Server=$arg;Database=master;Trusted_Connection=True;&quot;</span>
	<span style="color: #008000;"># setup the connection object</span>
	<span style="color: #800080;">$SqlConn</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-Typename</span> System.Data.SqlClient.SqlConnection
	<span style="color: #800080;">$SqlConn</span>.ConnectionString <span style="color: pink;">=</span> <span style="color: #800080;">$connectionString</span>
	<span style="color: #008000;"># Open the connection</span>
	<span style="color: #800080;">$SqlConn</span>.open<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$cmd</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT COUNT(*) AS cnt FROM sys.databases&quot;</span>;
	<span style="color: #008000;"># Create the sql command object</span>
	<span style="color: #800080;">$SqlCmd</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SqlCommand <span style="color: #800080;">$cmd</span><span style="color: pink;">,</span> <span style="color: #800080;">$SqlConn</span>
	<span style="color: #008000;"># Run the query</span>
	<span style="color: #800080;">$result</span> <span style="color: pink;">=</span> <span style="color: #800080;">$SqlCmd</span>.ExecuteReader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #008000;"># Move pointer to the first row</span>
	<span style="color: #800080;">$result</span>.Read<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">&amp;</span>gt; <span style="color: #800080;">$null</span>
	<span style="color: #008000;"># Get db count</span>
	<span style="color: #800080;">$dbCount</span> <span style="color: pink;">=</span> <span style="color: #800080;">$result</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>
	<span style="color: #008000;"># Close the connection</span>
	<span style="color: #800080;">$SqlConn</span>.close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #008000;"># Reopen the connection</span>
	<span style="color: #800080;">$SqlConn</span>.open<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #008000;"># New command to get SQLAGENT state</span>
	<span style="color: #800080;">$cmd</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;xp_servicecontrol 'querystate', 'SQLSERVERAGENT'&quot;</span>
	<span style="color: #008000;"># Create the sql command object</span>
	<span style="color: #800080;">$SqlCmd</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SqlCommand <span style="color: #800080;">$cmd</span><span style="color: pink;">,</span> <span style="color: #800080;">$SqlConn</span>
	<span style="color: #008000;"># Run the query</span>
	<span style="color: #800080;">$result2</span> <span style="color: pink;">=</span> <span style="color: #800080;">$SqlCmd</span>.ExecuteReader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #008000;"># Move pointer to the first row</span>
	<span style="color: #800080;">$result2</span>.Read<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">&amp;</span>gt; <span style="color: #800080;">$null</span>
	<span style="color: #008000;"># Get db count</span>
	<span style="color: #800080;">$SQLAGENT</span> <span style="color: pink;">=</span> <span style="color: #800080;">$result2</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>
	<span style="color: #008000;"># Print out the database count for the host</span>
	<span style="color: #008080; font-weight: bold;">echo</span> <span style="color: #800000;">&quot;$arg hosts $dbCount databases. The SQLAGENT Service is $SQLAGENT&quot;</span>
	<span style="color: #008000;"># Clean up</span>
	<span style="color: #800080;">$SqlConn</span>.close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/04/image5.png"><img title="SQL DBA Duties with Powershell" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="121" alt="image thumb5 Powershell Nuggets" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/04/image-thumb5.png" width="244" border="0" /></a> </p>
<p>I hope to check out the huge range of <a href="http://www.codeplex.com/site/search?projectSearchText=powershell" target="_blank">Powershell Extensions</a> and <a href="http://www.robvanderwoude.com/powershelltools.php " target="_blank">Tools</a> available to do some really cool things. I’ve had a quick play with <a href="http://www.quest.com/" target="_blank">Quest Software</a>’s <a href="http://www.powergui.org" target="_blank">PowerGUI</a>. This looks pretty good but I try to stay away from fancy tools until I’ve learned the basics of a language well. I’m looking forward to working with <a href="http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx" target="_blank">Powershell scripting</a>. I see some great possibilities for…</p>
<ul>
<li>Multiple SQL Server &amp; MySQL management. Check multiple servers in one script! Check backups, disk space, virtually anything!
<li>Adhoc access to many data sources; including text files, rss feeds, databases, Active Directory.&nbsp;
<li>Producing troubleshooting scripts for less experienced support staff. </li>
</ul>
<p>&nbsp;</p>
</p>
<pre></pre>
<p><map name='google_ad_map_72_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/72?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_72_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=72&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fpowershell-nuggets%2F72' title="Powershell Nuggets" alt=" Powershell Nuggets" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/powershell-nuggets/72">Powershell Nuggets</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/powershell-nuggets/72" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/powershell-nuggets/72/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

