<?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; Server Management</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/server-management/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Wed, 01 Sep 2010 17:02:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<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="Powershell Execution Policy" 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('p72code7'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p727"><td class="code" id="p72code7"><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('p72code8'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p728"><td class="code" id="p72code8"><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('p72code9'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p729"><td class="code" id="p72code9"><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('p72code10'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7210"><td class="code" id="p72code10"><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="The Twitter Public Timeline in Powershell" 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('p72code11'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7211"><td class="code" id="p72code11"><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('p72code12'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7212"><td class="code" id="p72code12"><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="SQL DBA Duties with Powershell" 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>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/powershell-nuggets/72/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
