<?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; Powershell</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/powershell/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>Kill all processes by name with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853</link>
		<comments>http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853#comments</comments>
		<pubDate>Tue, 10 Aug 2010 19:10:58 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[kill all processes]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853</guid>
		<description><![CDATA[A reader commented on a previous post pointing out a deficiency in one of the scripts used to kill processes on remote computers. If more than one instance of the specified process was running on the target computer the script would buckle. This is pretty easy to rectify. The below script will kill all process [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853">Kill all processes by name with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>A reader commented on a <a href="http://www.youdidwhatwithtsql.com/more-powershell-nuggets/239" target="_blank">previous post</a> pointing out a deficiency in one of the scripts used to kill processes on remote computers. If more than one instance of the specified process was running on the target computer the script would buckle. This is pretty easy to rectify. The below script will kill all process instances on the target machine.</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('p853code3'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8533"><td class="code" id="p853code3"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Kill all processes on a remote machine with a specific name</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;notepad.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>
&nbsp;
<span style="color: #008000;"># This original part of the script dies if $process is an array of more than one calc.exe process</span>
<span style="color: #008000;"># Kill the process and get exit status 0 = OK</span>
<span style="color: #008000;"># $status = $process.InvokeMethod(&quot;Terminate&quot;, $null);</span>
<span style="color: #008000;"># switch($status)</span>
<span style="color: #008000;"># {</span>
<span style="color: #008000;">#              0 { Write-Host -ForegroundColor Green &quot;Killed $processToKill on $computer&quot;};</span>
<span style="color: #008000;">#              default { Write-Host -ForegroundColor Red &quot;Error, couldn't kill $processToKill on $computer&quot;};</span>
<span style="color: #008000;">#};</span>
&nbsp;
<span style="color: #800080;">$count</span> <span style="color: pink;">=</span> <span style="color: #804000;">1</span>;
&nbsp;
<span style="color: #008000;"># This will work regardless if $process is an array or not</span>
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$ps</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$process</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;Kill count = $count&quot;</span>;
				<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Handle = &quot;</span> <span style="color: #800080;">$ps</span>.Handle;
                <span style="color: #800080;">$status</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ps</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>;
                <span style="color: #000000;">&#125;</span>;
                <span style="color: #800080;">$count</span><span style="color: pink;">++</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>This will procedure output similar to below.</p>
<pre>Attempting to Kill notepad.exe on localhost
Kill count = 1
Handle =  3788
Killed notepad.exe on localhost
Kill count = 2
Handle =  4916
Killed notepad.exe on localhost
Kill count = 3
Handle =  5884
Killed notepad.exe on localhost
Kill count = 4
Handle =  3488
Killed notepad.exe on localhost
Kill count = 5
Handle =  6232
Killed notepad.exe on localhost</pre>
<p>A similar thing can be achieved , on the localhost, with a one-liner.</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('p853code4'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8534"><td class="code" id="p853code4"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Kills all processes called 'calc' on the localhost</span>
<span style="color: #008080; font-weight: bold;">ps</span> calc <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">kill</span>;</pre></td></tr></table></div>

<p>With a little bit of <a href="http://blogs.msdn.com/b/powershell/archive/2008/05/10/remoting-with-powershell-quickstart.aspx" target="_blank">Remoting</a>, available in Powershell V2, it would be simple enough to achieve the same functionality in the one-liner to execute this on remote computers.</p>
<p><map name='google_ad_map_853_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/853?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_853_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=853&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fkill-all-processes-by-name-with-powershell%2F853' title="Kill all processes by name with Powershell" alt=" Kill all processes by name with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853">Kill all processes by name with Powershell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Searching database objects with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/searching-database-objects-with-powershell/722</link>
		<comments>http://www.youdidwhatwithtsql.com/searching-database-objects-with-powershell/722#comments</comments>
		<pubDate>Sun, 11 Apr 2010 14:50:51 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/searching-database-objects-with-powershell/722</guid>
		<description><![CDATA[Sometimes it&#8217;s useful to get a quick overview of what objects are referencing a particular table, view or function. This may arise when we think we may need to drop an object but want to double-check if anything in the database is still referencing it. Here&#8217;s a quick solution in the form of a Powershell [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/searching-database-objects-with-powershell/722">Searching database objects with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Sometimes it&#8217;s useful to get a quick overview of what objects are referencing a particular table, view or function. This may arise when we think we may need to drop an object but want to double-check if anything in the database is still referencing it. Here&#8217;s a quick solution in the form of a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script. To get started you just need to modify the values for a few variables before executing the script.</p>
<ul>
<li><strong>$server -</strong> The <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> instance you wish to search against. </li>
<li><strong>$database &#8211; </strong>The database you wish to search. </li>
<li><strong>$matchText &#8211; </strong>The text you wish to search for in the objects. </li>
</ul>
<p>This script will search the all of the stored procedures, functions, views and triggers for the text specified in $matchText.</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('p722code6'); 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="p722code6"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$server</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;RHYS-PC\SQL2005&quot;</span>;
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;AdventureWorks&quot;</span>;
<span style="color: #800080;">$matchText</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Person&quot;</span>;
&nbsp;
<span style="color: #008000;"># Load the SQL Management Objects assembly (Pipe out-null suppresses output)</span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.SMO&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">out-null</span>
&nbsp;
<span style="color: #008000;"># Create our SMO objects</span>
<span style="color: #800080;">$srv</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #800000;">&quot;Microsoft.SqlServer.Management.SMO.Server&quot;</span> <span style="color: #800080;">$server</span>;
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.Management.SMO.Database&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008000;"># Get the database</span>
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases<span style="color: #000000;">&#91;</span><span style="color: #800080;">$database</span><span style="color: #000000;">&#93;</span>;
&nbsp;
<span style="color: #008000;"># For each stored procedure in the database</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$proc</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.StoredProcedures<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># For each matching stored procedure</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$proc</span>.TextBody <span style="color: #FF0000;">-match</span> <span style="color: #800080;">$matchText</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;Procedure: &quot;</span> <span style="color: #800080;">$proc</span>.Name <span style="color: #800000;">&quot; contains $matchText&quot;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># For each function in the database</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$func</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.UserDefinedFunctions<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># For each matching user defined function</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$func</span>.TextBody <span style="color: #FF0000;">-match</span> <span style="color: #800080;">$matchText</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;Function: &quot;</span> <span style="color: #800080;">$func</span>.Name <span style="color: #800000;">&quot; contains $matchText&quot;</span>;
	<span style="color: #000000;">&#125;</span>	
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># For each view in the database</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$view</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.Views<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># For each matching view</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$view</span>.TextBody <span style="color: #FF0000;">-match</span> <span style="color: #800080;">$matchText</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;View: &quot;</span> <span style="color: #800080;">$view</span>.Name <span style="color: #800000;">&quot; contains $matchText&quot;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># For each trigger in the database</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$trigger</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.Triggers<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># For each matching trigger</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$trigger</span>.TextBody <span style="color: #FF0000;">-match</span> <span style="color: #800080;">$matchText</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;Trigger: &quot;</span> <span style="color: #800080;">$trigger</span>.Name <span style="color: #800000;">&quot; contains $matchText&quot;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Here&#8217; an example of the output when run against the <a href="http://msdn.microsoft.com/en-us/library/ms124501.aspx" target="_blank">AdventureWorks</a> database searching objects for &quot;Person&quot;.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/searching_database_objects_for_text.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="searching database objects for text" border="0" alt="searching database objects for text" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/searching_database_objects_for_text_thumb.png" width="644" height="350" /></a></p>
<p><map name='google_ad_map_722_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/722?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_722_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=722&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fsearching-database-objects-with-powershell%2F722' title="Searching database objects with Powershell" alt=" Searching database objects with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/searching-database-objects-with-powershell/722">Searching database objects with Powershell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/searching-database-objects-with-powershell/722/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Check the time on multiple servers</title>
		<link>http://www.youdidwhatwithtsql.com/check-the-time-on-multiple-servers/711</link>
		<comments>http://www.youdidwhatwithtsql.com/check-the-time-on-multiple-servers/711#comments</comments>
		<pubDate>Tue, 30 Mar 2010 20:29:48 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/check-the-time-on-multiple-servers/711</guid>
		<description><![CDATA[After the recent change in our clocks due to BST I noticed that one of our servers was a hour slow. I wanted to check the rest since we run a lot of time dependant processes. Now we have Powershell any thought of manually checking each one is madness. Here&#8217;s a quick script I knocked [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/check-the-time-on-multiple-servers/711">Check the time on multiple servers</a></p>
]]></description>
			<content:encoded><![CDATA[<p>After the recent change in our clocks due to <a href="http://wwp.greenwichmeantime.co.uk/time-zone/europe/uk/time/british-summer-time/" target="_blank">BST</a> I noticed that one of our servers was a hour slow. I wanted to check the rest since we run a lot of time dependant processes. Now we have <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> any thought of manually checking each one is madness. Here&#8217;s a quick script I knocked up to check the time on multiple servers. Just create a text file called <strong>servers.txt </strong>with each server name on a new line. Place this onto your desktop and you&#8217;re ready to go.</p>
<p>Since my standard Windows Domain account didn&#8217;t have access to these servers I&#8217;ve used the <a href="http://technet.microsoft.com/en-us/library/dd315327.aspx" target="_blank">Get-Credential</a> cmdlet. When you execute the script you will see the below window asking for authentication details.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/03/GetCredential_Powershell.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Get-Credential Powershell" border="0" alt="Get-Credential Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/03/GetCredential_Powershell_thumb.png" width="613" height="484" /></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('p711code8'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7118"><td class="code" id="p711code8"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Get servers from text file on Desktop</span>
<span style="color: #800080;">$servers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;$env:USERPROFILE\Desktop\servers.txt&quot;</span>;
<span style="color: #008000;"># Get credentials needed to access these servers</span>
<span style="color: #800080;">$credential</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Credential</span>;
&nbsp;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$server</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$servers</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #800080;">$timeObj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$servers</span> <span style="color: #008080; font-style: italic;">-Class</span> Win32_LocalTime <span style="color: #008080; font-style: italic;">-Credential</span> <span style="color: #800080;">$credential</span>;
	<span style="color: #800080;">$hour</span> <span style="color: pink;">=</span> <span style="color: #800080;">$timeObj</span>.Hour; 
	<span style="color: #800080;">$minute</span> <span style="color: pink;">=</span> <span style="color: #800080;">$timeObj</span>.Minute;
	<span style="color: #800080;">$second</span> <span style="color: pink;">=</span> <span style="color: #800080;">$timeObj</span>.Second;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;$server set time is $hour $minute $second&quot;</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The reported times will differ by a few seconds, due to latency, but it should be easy to spot any that are way out.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/03/Powershell_Server_Time_Check.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Powershell Server Time Check" border="0" alt="Powershell Server Time Check" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/03/Powershell_Server_Time_Check_thumb.png" width="644" height="350" /></a></p>
<p><map name='google_ad_map_711_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/711?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_711_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=711&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcheck-the-time-on-multiple-servers%2F711' title="Check the time on multiple servers" alt=" Check the time on multiple servers" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/check-the-time-on-multiple-servers/711">Check the time on multiple servers</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/check-the-time-on-multiple-servers/711/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server Audit with Powershell Excel Automation</title>
		<link>http://www.youdidwhatwithtsql.com/sql-server-audit-with-powershell-excel-automation/703</link>
		<comments>http://www.youdidwhatwithtsql.com/sql-server-audit-with-powershell-excel-automation/703#comments</comments>
		<pubDate>Sat, 27 Mar 2010 18:57:50 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Excel Automation]]></category>
		<category><![CDATA[SQL Server Database Audit]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/sql-server-audit-with-powershell-excel-automation/703</guid>
		<description><![CDATA[Here&#8217;s neat little Powershell script you can use to audit your SQL Server databases. The script is dependant on the SQL Browser service, to discover instances, so you will need to make sure this is running. This will allow you to audit all SQL Server instances on the localhost with details of your databases and [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/sql-server-audit-with-powershell-excel-automation/703">SQL Server Audit with Powershell Excel Automation</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s neat little <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script you can use to audit your <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> databases. The script is dependant on the <a href="http://msdn.microsoft.com/en-us/library/ms181087.aspx" target="_blank">SQL Browser</a> service, to discover instances, so you will need to make sure this is running. This will allow you to audit all SQL Server instances on the localhost with details of your databases and associated information. I use <a href="http://msdn.microsoft.com/en-us/library/ms162169.aspx" target="_blank">SMO</a> here so it should be pretty easy to customise for your own purposes. Just run the script and a nicely formatted Excel report of your databases will be produced. Enjoy!</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('p703code10'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p70310"><td class="code" id="p703code10"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.Smo&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
<span style="color: #800080;">$smoObj</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Microsoft.SqlServer.Management.Smo.SmoApplication<span style="color: #000000;">&#93;</span>;
&nbsp;
<span style="color: #008000;"># This gets the sql server</span>
<span style="color: #800080;">$sql</span> <span style="color: pink;">=</span> <span style="color: #800080;">$smoObj</span>::EnumAvailableSqlServers<span style="color: #000000;">&#40;</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008000;"># Automate Excel</span>
<span style="color: #800080;">$xl</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-ComObject</span> Excel.Application;
<span style="color: #800080;">$xl</span>.Visible <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
<span style="color: #800080;">$xl</span> <span style="color: pink;">=</span> <span style="color: #800080;">$xl</span>.Workbooks.Add<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$Sheet</span> <span style="color: pink;">=</span> <span style="color: #800080;">$xl</span>.Worksheets.Item<span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #800080;">$row</span> <span style="color: pink;">=</span> <span style="color: #804000;">1</span>;
&nbsp;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$sqlserver</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$sql</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	 <span style="color: #008000;"># headers</span>
     <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Sql Server:&quot;</span>;
     <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$sqlserver</span>.Name;
     <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>.Font.Bold <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
     <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span>.Font.Bold <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
	<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">3</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>;
     	<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">4</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Instance:&quot;</span>;
     	<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">5</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$sqlserver</span>.Instance;
     	<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">4</span><span style="color: #000000;">&#41;</span>.Font.Bold <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
     	<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">5</span><span style="color: #000000;">&#41;</span>.Font.Bold <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">6</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">7</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Version: &quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">8</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$sqlserver</span>.Version;
     	<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">7</span><span style="color: #000000;">&#41;</span>.Font.Bold <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
     	<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">8</span><span style="color: #000000;">&#41;</span>.Font.Bold <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
&nbsp;
	 <span style="color: #008000;"># Prettify headers</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;">1</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">8</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;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#41;</span>.Interior.ColorIndex <span style="color: pink;">=</span> <span style="color: #804000;">50</span>;
     		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#41;</span>.Font.ColorIndex <span style="color: pink;">=</span> <span style="color: #804000;">20</span>;
	 <span style="color: #000000;">&#125;</span>
&nbsp;
	 <span style="color: #008000;"># Create obj for this sql server</span>
	 <span style="color: #800080;">$srv</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #800000;">&quot;Microsoft.SqlServer.Management.Smo.Server&quot;</span> <span style="color: #800080;">$sqlserver</span>.Name;
	 <span style="color: #008000;"># Get the databases on this sql server</span>
	 <span style="color: #800080;">$databases</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases;
&nbsp;
	 <span style="color: #008000;"># Increase rowcount for formatting</span>
	 <span style="color: #800080;">$row</span> <span style="color: pink;">+=</span> <span style="color: #804000;">2</span>;
&nbsp;
	 <span style="color: #008000;"># Add column headers for databases</span>
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Database&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Size&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">3</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SpaceAvailable&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">4</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;State&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">5</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Table Count&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">6</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Collation&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">7</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Compatibility Level&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">8</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Create Date&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">9</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Index Space Usage&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">10</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Owner&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">11</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Last Backup&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">12</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Trigger Count&quot;</span>;
	 <span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">13</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;UDF Count&quot;</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;">1</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">13</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;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#41;</span>.Interior.ColorIndex <span style="color: pink;">=</span> <span style="color: #804000;">35</span>;
     		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#41;</span>.Font.ColorIndex <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #800080;">$i</span><span style="color: #000000;">&#41;</span>.Font.Bold <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
	 <span style="color: #000000;">&#125;</span>
	 <span style="color: #800080;">$row</span><span style="color: pink;">++</span>;
	 <span style="color: #008000;"># Work through each database in the collection</span>
	 <span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$db</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$databases</span><span style="color: #000000;">&#41;</span>
	 <span style="color: #000000;">&#123;</span>
	 	<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.Name;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.Size;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">3</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.SpaceAvailable;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">4</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.State;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">5</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.Tables.Count;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">6</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.Collation;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">7</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.CompatibilityLevel;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">8</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.CreateDate;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">9</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.IndexSpaceUsage;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">10</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.Owner;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">11</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.LastBackupDate;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">12</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.Triggers.Count;
		<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span> <span style="color: #804000;">13</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.UserDefinedFunctions.Count;
		<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;">1</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">13</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;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#41;</span>.Interior.ColorIndex <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
     			<span style="color: #800080;">$Sheet</span>.Cells.Item<span style="color: #000000;">&#40;</span><span style="color: #800080;">$row</span><span style="color: pink;">,</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#41;</span>.Font.ColorIndex <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;			
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800080;">$row</span><span style="color: pink;">++</span>;
	 <span style="color: #000000;">&#125;</span>
&nbsp;
	 <span style="color: #800080;">$row</span><span style="color: pink;">++</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Apply autoformat</span>
<span style="color: #800080;">$Sheet</span>.UsedRange.EntireColumn.AutoFit<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/03/powershell_excel_sql_server_database_audit.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="powershell excel sql server database audit" border="0" alt="powershell excel sql server database audit" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/03/powershell_excel_sql_server_database_audit_thumb.png" width="644" height="393" /></a></p>
<p><map name='google_ad_map_703_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/703?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_703_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=703&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fsql-server-audit-with-powershell-excel-automation%2F703' title="SQL Server Audit with Powershell Excel Automation" alt=" SQL Server Audit with Powershell Excel Automation" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/sql-server-audit-with-powershell-excel-automation/703">SQL Server Audit with Powershell Excel Automation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/sql-server-audit-with-powershell-excel-automation/703/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Get TFL Tube data with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689</link>
		<comments>http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689#comments</comments>
		<pubDate>Sat, 27 Feb 2010 19:18:24 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[data.gov.uk]]></category>
		<category><![CDATA[London Datastore]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689</guid>
		<description><![CDATA[The London Datastore has loads of datasets available that we can use for free. One of the datasets available is a list of TFL Station Locations. The station location feed is a geo-coded KML feed of most of London Underground, DLR and London Overground stations. Here&#8217;s Powershell script that will extract this data from a [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689">Get TFL Tube data with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://data.london.gov.uk/" target="_blank">London Datastore</a> has loads of datasets available that we can use for free. One of the datasets available is a list of <a href="http://data.london.gov.uk/datastore/package/tfl-station-locations" target="_blank">TFL Station Locations</a>. The station location feed is a geo-coded KML feed of most of London Underground, DLR and London Overground stations. Here&#8217;s <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script that will extract this data from a url and write it to a pipe-delimited file ready for import into the database of your choice. </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('p689code12'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p68912"><td class="code" id="p689code12"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$xml</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> XML
<span style="color: #800080;">$url</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;http://www.tfl.gov.uk/tfl/syndication/feeds/stations.kml&quot;</span>
<span style="color: #800080;">$csvFile</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$env:UserProfile\Desktop\tfl_tubes.csv&quot;</span>;
&nbsp;
<span style="color: #008000;"># Empty file if it already exists</span>
<span style="color: #008080; font-weight: bold;">Set-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$csvFile</span> <span style="color: #800080;">$null</span>;
<span style="color: #008000;"># Add headers to file</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$csvFile</span> <span style="color: #800000;">&quot;Station|Address|Coordinates&quot;</span>;
&nbsp;
<span style="color: #008000;"># Load the xml</span>
<span style="color: #800080;">$xml</span>.Load<span style="color: #000000;">&#40;</span><span style="color: #800080;">$url</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$stations</span> <span style="color: pink;">=</span> <span style="color: #800080;">$xml</span>.kml.Document.Placemark;
&nbsp;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$station</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$stations</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$station</span>.name;
	<span style="color: #800080;">$description</span> <span style="color: pink;">=</span> <span style="color: #800080;">$station</span>.description;
	<span style="color: #800080;">$coordinates</span> <span style="color: pink;">=</span> <span style="color: #800080;">$station</span>.Point.coordinates
	<span style="color: #008000;"># This data needs cleaning a bit</span>
	<span style="color: #800080;">$name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$name</span>.Trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$description</span> <span style="color: pink;">=</span> <span style="color: #800080;">$description</span>.Trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$coordinates</span> <span style="color: pink;">=</span> <span style="color: #800080;">$coordinates</span>.Trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #008000;"># Ad line to the csv file</span>
	<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$csvFile</span> <span style="color: #800000;">&quot;$name|$description|$coordinates&quot;</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>After running the script check your desktop for a file called <strong>tfl_tubes.csv</strong> which should look something like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/tfl_tubes.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="TFL Station Locations csv data file" border="0" alt="TFL Station Locations csv data file" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/tfl_tubes_thumb.png" width="244" height="183" /></a></p>
<p><map name='google_ad_map_689_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/689?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_689_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=689&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fget-tfl-tube-data-with-powershell%2F689' title="Get TFL Tube data with Powershell" alt=" Get TFL Tube data with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689">Get TFL Tube data with Powershell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Console input with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/console-input-with-powershell/673</link>
		<comments>http://www.youdidwhatwithtsql.com/console-input-with-powershell/673#comments</comments>
		<pubDate>Sun, 21 Feb 2010 19:51:07 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/console-input-with-powershell/673</guid>
		<description><![CDATA[I&#8217;m looking at building some Powershell scripts that can accept user input to perform different tasks with a wizard style interface. As it happens this is fairly easily achieved with the Read-Host cmdlet. Here&#8217;s a quick script showing how such a powershell script may look. ?View Code POWERSHELLfunction mainMenu&#40;&#41; &#123; Clear-Host; Write-Host &#34;============&#34;; Write-Host &#34;= [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/console-input-with-powershell/673">Console input with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m looking at building some <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> scripts that can accept user input to perform different tasks with a wizard style interface. As it happens this is fairly easily achieved with the <a href="http://technet.microsoft.com/en-us/library/ee176935.aspx" target="_blank">Read-Host</a> cmdlet. Here&#8217;s a quick script showing how such a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">powershell</a> script may look.</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('p673code14'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p67314"><td class="code" id="p673code14"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> mainMenu<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Clear<span style="color: pink;">-</span>Host;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;============&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;= MAINMENU =&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;============&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;1. Press '1' for this option&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;2. Press '2' for this option&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;3. Press '3' for this option&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;4. Press '4' for this option&quot;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">function</span> returnMenu<span style="color: #000000;">&#40;</span><span style="color: #800080;">$option</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Clear<span style="color: pink;">-</span>Host;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;You chose option $option&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Press any key to return to the main menu.&quot;</span>;
	<span style="color: #800080;">$host</span>.UI.RawUI.ReadKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;NoEcho,IncludeKeyDown&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">do</span>
<span style="color: #000000;">&#123;</span>
	mainMenu;
	<span style="color: #800080;">$input</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Read-Host</span> <span style="color: #800000;">&quot;Enter a number for an option or type <span style="color: #008080; font-weight: bold;">`&quot;</span>quit<span style="color: #008080; font-weight: bold;">`&quot;</span> to finish.&quot;</span>
	<span style="color: #0000FF;">switch</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$input</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800000;">&quot;1&quot;</span>
		<span style="color: #000000;">&#123;</span>
			returnMenu <span style="color: #800080;">$input</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;2&quot;</span>
		<span style="color: #000000;">&#123;</span>
			returnMenu <span style="color: #800080;">$input</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;3&quot;</span>
		<span style="color: #000000;">&#123;</span>
			returnMenu <span style="color: #800080;">$input</span>;		
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;4&quot;</span>
		<span style="color: #000000;">&#123;</span>
			returnMenu <span style="color: #800080;">$input</span>;		
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;quit&quot;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008000;"># nothing</span>
		<span style="color: #000000;">&#125;</span>
		default
		<span style="color: #000000;">&#123;</span>
			Clear<span style="color: pink;">-</span>Host;
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Invalid input. Please enter a valid option. Press any key to continue.&quot;</span>;
			<span style="color: #800080;">$host</span>.UI.RawUI.ReadKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;NoEcho,IncludeKeyDown&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span> <span style="color: #0000FF;">until</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$input</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;quit&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
Clear<span style="color: pink;">-</span>Host;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/powershell_console_input_menu.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="powershell console input menu" border="0" alt="powershell console input menu" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/powershell_console_input_menu_thumb.png" width="644" height="350" /></a> </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/powershell_console_input_menu_1.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="powershell console input menu 1" border="0" alt="powershell console input menu 1" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/powershell_console_input_menu_1_thumb.png" width="644" height="350" /></a></p>
<p><map name='google_ad_map_673_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/673?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_673_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=673&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fconsole-input-with-powershell%2F673' title="Console input with Powershell" alt=" Console input with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/console-input-with-powershell/673">Console input with Powershell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/console-input-with-powershell/673/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Managing Index Fragmentation with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622</link>
		<comments>http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622#comments</comments>
		<pubDate>Tue, 09 Feb 2010 22:24:41 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[index fragmentation]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622</guid>
		<description><![CDATA[Here&#8217;s a Powershell script that can be used to manage index fragmentation in SQL Server databases. The strategy I&#8217;ve used in the script is based on a recommendation from Pinal Dave (blog &#124; twitter) in his article&#160; Difference Between Index Rebuild and Index Reorganize Explained with T-SQL Script. Just set the $sqlserver and $database variables [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622">Managing Index Fragmentation with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script that can be used to manage index fragmentation in <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> databases. The strategy I&#8217;ve used in the script is based on a recommendation from Pinal Dave (<a href="http://blog.sqlauthority.com" target="_blank">blog</a> | <a href="http://twitter.com/pinaldave" target="_blank">twitter</a>) in his article&nbsp; <a href="http://blog.sqlauthority.com/2007/12/22/sql-server-difference-between-index-rebuild-and-index-reorganize-explained-with-t-sql-script/" target="_blank">Difference Between Index Rebuild and Index Reorganize Explained with T-SQL Script</a>. Just set the <strong>$sqlserver </strong>and <strong>$database </strong>variables to something appropriate for your environment. Enjoy!</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('p622code16'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p62216"><td class="code" id="p622code16"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Load SMO</span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">'Microsoft.SqlServer.SMO'</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
&nbsp;
<span style="color: #008000;"># Set sql server and database name here</span>
<span style="color: #800080;">$sqlserver</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost\sql2005&quot;</span>;
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;AdventureWorks&quot;</span>;
&nbsp;
<span style="color: #800080;">$srv</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #800000;">&quot;Microsoft.SqlServer.Management.SMO.Server&quot;</span> <span style="color: #800080;">$sqlserver</span>;
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.Management.SMO.Database&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases<span style="color: #000000;">&#91;</span><span style="color: #800080;">$database</span><span style="color: #000000;">&#93;</span>;
&nbsp;
<span style="color: #008000;"># Get table count</span>
<span style="color: #800080;">$table_count</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.Tables.Count;
<span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #008000;"># First script out the drops</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$table</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.Tables<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checking table $table&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">/</span> <span style="color: #800080;">$table_count</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing indexes&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
	<span style="color: #800080;">$i</span><span style="color: pink;">++</span>;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$index</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$table</span>.Indexes<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$index_name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$index</span>.Name;
		<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checking table $table&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">/</span> <span style="color: #800080;">$table_count</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing index $index_name&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
		<span style="color: #008000;"># Get the fragmentation stats</span>
		<span style="color: #800080;">$frag_stats</span> <span style="color: pink;">=</span> <span style="color: #800080;">$index</span>.EnumFragmentation<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #008000;"># Get the properties we need to work with the index</span>
		<span style="color: #800080;">$frag_stats</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: #800080;">$Index_Name</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.Index_Name;
						<span style="color: #800080;">$Index_Type</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.Index_Type;
						<span style="color: #800080;">$Average_Fragmentation</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.AverageFragmentation;
									<span style="color: #000000;">&#125;</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;$Index_Type $Index_Name has a fragmentation percentage of $Average_Fragmentation&quot;</span>;
&nbsp;
		<span style="color: #008000;"># Here we decide what to do based on the level on fragmentation</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Average_Fragmentation</span> <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">40.00</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> Red <span style="color: #800000;">&quot;$Index_Name is more than 40% fragmented and will be rebuilt.&quot;</span>;
			<span style="color: #800080;">$index</span>.Rebuild<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</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;$Index_Name has been rebuilt.&quot;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">elseif</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Average_Fragmentation</span> <span style="color: #FF0000;">-ge</span> <span style="color: #804000;">10.00</span> <span style="color: #FF0000;">-and</span> <span style="color: #800080;">$Average_Fragmentation</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">40.00</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> Red <span style="color: #800000;">&quot;$Index_Name is between 10-40% fragmented and will be reorganized.&quot;</span>;
			<span style="color: #800080;">$index</span>.Reorganize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</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;$Index_Name has been reorganized.&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> Red <span style="color: #800000;">&quot;$Index_Name is healthy, with $Average_Fragmentation% fragmentation, and will be left alone.&quot;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Finished processing <span style="color: #008080; font-weight: bold;">`&quot;</span>$database<span style="color: #008080; font-weight: bold;">`&quot;</span> indexes.&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">100</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Done&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Seconds</span> <span style="color: #804000;">2</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWork_Index_Management.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="AdventureWork Index Management with Powershell" border="0" alt="AdventureWork Index Management with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/AdventureWork_Index_Management_thumb.png" width="644" height="373" /></a></p>
<p><map name='google_ad_map_622_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/622?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_622_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=622&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmanaging-index-fragmentation-with-powershell%2F622' title="Managing Index Fragmentation with Powershell" alt=" Managing Index Fragmentation with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622">Managing Index Fragmentation with Powershell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/managing-index-fragmentation-with-powershell/622/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Powershell Random sort</title>
		<link>http://www.youdidwhatwithtsql.com/powershell-random-sort/596</link>
		<comments>http://www.youdidwhatwithtsql.com/powershell-random-sort/596#comments</comments>
		<pubDate>Mon, 01 Feb 2010 11:56:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[random sort]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/powershell-random-sort/596</guid>
		<description><![CDATA[Here&#8217;s a Powershell snippet that randomly sorts the lines in a file. The snippet below reads a text file, called file.txt, located in your user profile directory. The data in the file will be written back with the lines in a different order. ?View Code POWERSHELL$data = New-Object System.Object; $data = Get-Content &#34;$Env:USERPROFILE\file.txt&#34;; # Random [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/powershell-random-sort/596">Powershell Random sort</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> snippet that randomly sorts the lines in a file. The snippet below reads a text file, called <strong>file.txt</strong>, located in your user profile directory. The data in the file will be written back with the lines in a different order.</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('p596code19'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p59619"><td class="code" id="p596code19"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$data</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Object;
<span style="color: #800080;">$data</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\file.txt&quot;</span>;
<span style="color: #008000;"># Random sort the lines in the file</span>
<span style="color: #800080;">$data</span> <span style="color: pink;">=</span> <span style="color: #800080;">$data</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">sort</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span>System.Guid<span style="color: #000000;">&#93;</span>::NewGuid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span>;
<span style="color: #008080; font-weight: bold;">Set-Content</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\file.txt&quot;</span> <span style="color: #800080;">$data</span>;</pre></td></tr></table></div>

<p>If your file started off like this&#8230;</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/file_ordered.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="file ordered" border="0" alt="file ordered" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/file_ordered_thumb.png" width="244" height="208" /></a> </p>
</p>
<p>It will end up looking something like&#8230;</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/file_random_sort1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="file random sort" border="0" alt="file random sort" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/file_random_sort_thumb.png" width="244" height="234" /></a> </p>
<p>This is functionally equivalent to&#8230;</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('p596code20'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p59620"><td class="code" id="p596code20"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Customers</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> NEWID<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><map name='google_ad_map_596_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/596?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_596_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=596&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fpowershell-random-sort%2F596' title="Powershell Random sort" alt=" Powershell Random sort" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/powershell-random-sort/596">Powershell Random sort</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/powershell-random-sort/596/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PsFetch</title>
		<link>http://www.youdidwhatwithtsql.com/psfetch/580</link>
		<comments>http://www.youdidwhatwithtsql.com/psfetch/580#comments</comments>
		<pubDate>Tue, 26 Jan 2010 21:04:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Tools]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/psfetch/580</guid>
		<description><![CDATA[I really used to like apt-get when I used to run Linux as my home os a few years ago. Once you got the hang of it installing and managing software was easy. I&#8217;m obviously pleased to see PsFetch in development. This tool is the Windows Powershell take on apt-get which will allow you to [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/psfetch/580">PsFetch</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I really used to like <a href="http://en.wikipedia.org/wiki/Advanced_Packaging_Tool" target="_blank">apt-get</a> when I used to run <a href="http://www.linux.org/" target="_blank">Linux</a> as my home os a few years ago. Once you got the hang of it installing and managing software was easy. I&#8217;m obviously pleased to see <a href="http://www.psfetch.net/" target="_blank">PsFetch</a> in development. This tool is the Windows <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> take on apt-get which will allow you to get your hand on cmdlets, scripts, and by the looks of it, tools from <a href="http://www.codeplex.com" target="_blank">codeplex</a>. They&#8217;re currently building up a <a href="http://www.psfetch.net/browse/" target="_blank">repository</a> with lots of cool scripts.</p>
<p>Read <a href="http://www.psfetch.net/about/" target="_blank">more about PsFetch</a> and sign-up for <a href="http://www.psfetch.net/download/" target="_blank">public beta testing</a>.</p>
<p><map name='google_ad_map_580_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/580?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_580_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=580&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fpsfetch%2F580' title="PsFetch" alt=" PsFetch" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/psfetch/580">PsFetch</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/psfetch/580/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowershellPack STA Error</title>
		<link>http://www.youdidwhatwithtsql.com/powershellpack-sta-error/579</link>
		<comments>http://www.youdidwhatwithtsql.com/powershellpack-sta-error/579#comments</comments>
		<pubDate>Mon, 25 Jan 2010 20:51:29 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell STA mode]]></category>
		<category><![CDATA[Powershell Tools]]></category>
		<category><![CDATA[PowerShellPack]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/powershellpack-sta-error/579</guid>
		<description><![CDATA[I&#8217;m really into Powershell so I like trying out all the available tools. Somehow I&#8217;ve missed the release of the PowershellShellPack which has ten modules offering all kinds of additional functionality including file handling, operating system information, GUI and code generation. I followed the Channel 9 video and ran into a problem on the first [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/powershellpack-sta-error/579">PowershellPack STA Error</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m really into <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> so I like trying out all the available tools. Somehow I&#8217;ve missed the release of the <a href="http://code.msdn.microsoft.com/PowerShellPack" target="_blank">PowershellShellPack</a> which has ten modules offering all kinds of additional functionality including file handling, operating system information, GUI and code generation. I followed the <a href="http://ecn.channel9.msdn.com/o9/ch9/3/6/0/8/8/4/Powershell4_ch9.wmv" target="_blank">Channel 9 video</a> and ran into a problem on the first example given.</p>
<p>First I ran the command <strong>Import-Module PowerShellPack</strong>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Powershell_Import_Module_PowerShellPack.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Powershell Import Module PowerShellPack" border="0" alt="Powershell Import Module PowerShellPack" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Powershell_Import_Module_PowerShellPack_thumb.png" width="644" height="375" /></a> </p>
<p>Then I tried to create, and display, a label as in the video example; <strong>New-Label &quot;Hello World&quot; -FontSize 40 -Show</strong></p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/powershell_sta_mode_error.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Powershell STA modeerror" border="0" alt="Powershell STA modeerror" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/powershell_sta_mode_error_thumb.png" width="644" height="375" /></a> </p>
<p>This resulted in the following error.</p>
<pre>

New-Object : Exception calling &quot;.ctor&quot; with &quot;0&quot; argument(s): &quot;The calling thread must be STA, because many UI components require this.&quot;
At C:\Users\Rhys\Documents\WindowsPowerShell\Modules\WPK\GeneratedControls\PresentationFramework.ps1:30292 char:29
+         $Object = New-Object &lt;&lt;&lt;&lt;  System.Windows.Controls.Label
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

</pre>
<p>OK, I seem to need to be running in STA mode. I Googled &quot;Powershell STA mode&quot; and discovered a <a href="http://stackoverflow.com/questions/1508704/does-powershell-sta-mode-eliminate-sharepoint-memory-leak-issue" target="_blank">Stackoverflow thread</a>. After reading this I thought I&#8217;d try starting Powershell with the switch <strong>-STA</strong>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/powershell_start_in_sta_mode.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="powershell start in sta mode" border="0" alt="powershell start in sta mode" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/powershell_start_in_sta_mode_thumb.png" width="644" height="375" /></a> </p>
<p>OK, that seemed to work. Now lets try to create that label again. First load the <a href="http://code.msdn.microsoft.com/PowerShellPack" target="_blank">PowerShellPack</a> modules again since we&#8217;ve started a new session.</p>
<p><strong>Import-Module PowerShellPack</strong></p>
<p><font color="#666666">Now try to create the label again.</font></p>
<p><strong>New-Label &quot;Hello World&quot; -FontSize 40 -Show</strong></p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Powershell_Hello_World_Label.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Powershell Hello World Label" border="0" alt="Powershell Hello World Label" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Powershell_Hello_World_Label_thumb.png" width="644" height="375" /></a></p>
<p>OK, that&#8217;s that hurdle over. Now I can get on with exploring what this tool can do for me.</p>
<p><map name='google_ad_map_579_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/579?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_579_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=579&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fpowershellpack-sta-error%2F579' title="PowershellPack STA Error" alt=" PowershellPack STA Error" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/powershellpack-sta-error/579">PowershellPack STA Error</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/powershellpack-sta-error/579/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://ecn.channel9.msdn.com/o9/ch9/3/6/0/8/8/4/Powershell4_ch9.wmv" length="57443159" type="video/x-ms-wmv" />
		</item>
	</channel>
</rss>
