<?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>PowerShell Station &#187; SQL Server</title>
	<atom:link href="http://powershellstation.com/category/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://powershellstation.com</link>
	<description>Mike&#039;s PowerShell Musings</description>
	<lastBuildDate>Thu, 05 Apr 2012 03:05:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Checking a Field for NULL in PowerShell</title>
		<link>http://powershellstation.com/2010/04/09/checking-a-field-for-null-in-powershell/</link>
		<comments>http://powershellstation.com/2010/04/09/checking-a-field-for-null-in-powershell/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 03:45:30 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[NULL]]></category>
		<category><![CDATA[Script]]></category>

		<guid isPermaLink="false">http://powershellstation.com/2010/04/09/checking-a-field-for-null-in-powershell/</guid>
		<description><![CDATA[It’s been a long time (over 2 months) since I last posted.  I’ll try to get back into a rhythm of posting at least weekly.  Anyway, this is something that occurred to me at work when writing a script. I usually avoid nullable columns, but sometimes date fields make sense to be null (rather than [...]]]></description>
			<content:encoded><![CDATA[<p>It’s been a long time (over 2 months) since I last posted.  I’ll try to get back into a rhythm of posting at least weekly.  Anyway, this is something that occurred to me at work when writing a script.</p>
<p>I usually avoid nullable columns, but sometimes date fields make sense to be null (rather than use sentinel values like 1/1/1900).  In this case, I had a nullable date column and I needed to check in PowerShell whether the field was in fact null or not.  In SQL, I would have just used an IS NULL, or used the IsNull() function to replace the null value with something a little easier to deal with.  My first (feeble) attempt was to do this:</p>
<pre class="brush:powershell">if (!$_.completedDate){
# it’s null
}</pre>
<p>Unfortunately for me, that doesn’t work.  Next, I used this (which worked, but wasn’t very satisfactory either):</p>
<pre class="brush:powershell">if ($_.completedDate.ToString() -eq ''){
# it’s null
}</pre>
<p>Realizing that I was being stupid, I googled “PowerShell SQL NULL and after looking at several pages which didn’t really address the issue, I found <a href="http://blogs.technet.com/industry_insiders/pages/testing-for-database-null-values-from-powershell.aspx">this.</a> A little work to change it into a function, and voilà.</p>
<pre class="brush:powershell">function is-null($value){
  return  [System.DBNull]::Value.Equals($value)
}</pre>
<p>A few quick tests and this is what I wanted. Now, my code looks like this:</p>
<pre class="brush:powershell">if (is-null $_.completedDate){
# it’s null
}</pre>
<p>I find it hard to believe I haven’t written this function before (or seen it).</p>
<p>By the way…be watching the <a href="http://sqlpsx.codeplex.com">SQL PowerShell Extensions</a> project.  Chad released version 2.1, which includes SQL mode for the ISE (really nice).  I know he and several others are collaborating on an update which should be out sometime soon.</p>
<p>-Mike</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;title=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" id="wpa2a_2"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/04/09/checking-a-field-for-null-in-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL PowerShell Extensions (SQLPSX) 2.0 Released</title>
		<link>http://powershellstation.com/2010/01/05/sql-powershell-extensions-sqlpsx-2-0-released/</link>
		<comments>http://powershellstation.com/2010/01/05/sql-powershell-extensions-sqlpsx-2-0-released/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 00:39:17 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Script]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=233</guid>
		<description><![CDATA[The first module-based release of the SQL PowerShell Extensions (SQLPSX) was released recently on CodePlex.  It features very handy wrappers for most of the SMO objects used to manipulate SQL Server metadata, SSIS packages, Replication, and (new in the 2.0 release) an ADO.NET module which I wrote based on the code in this post.  There&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>The first module-based release of the SQL PowerShell Extensions (SQLPSX) was released recently on CodePlex.  It features very handy wrappers for most of the SMO objects used to manipulate SQL Server metadata, SSIS packages, Replication, and (new in the 2.0 release) an ADO.NET module which I wrote based on the code in this <a href="http://powershellstation.com/2009/09/15/executing-sql-the-right-way-in-powershell/">post</a>.  There&#8217;s also a data-collection process and Reporting Services reports to help you get your SQL Server installations under control.</p>
<p>Chad Miller, the driving force behind SQLPSX, has put a lot of effort into this release, and you&#8217;ll find really good examples of advanced functions (with comment-based help, even).</p>
<p>If you deal with SQL Server in any way, you&#8217;ll almost certainly be able to use this set of modules to streamline your scripting experience (and probably learn something about SMO in the process).</p>
<p>You can find the release <a href="http://sqlpsx.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=38047">here</a>.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;title=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" id="wpa2a_4"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/01/05/sql-powershell-extensions-sqlpsx-2-0-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get-EventLog and Get-WMIObject</title>
		<link>http://powershellstation.com/2009/12/16/get-eventlog-and-get-wmiobject/</link>
		<comments>http://powershellstation.com/2009/12/16/get-eventlog-and-get-wmiobject/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 05:51:39 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[WMI]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=209</guid>
		<description><![CDATA[Recently, we had an occasion to write a process to read event logs on several sql servers to try to determine login times for different sql and Windows logins.  Since we have begun using PowerShell v2.0, and since get-eventlog now has a -computername parameter, it seemed like an obvious solution. The event message we were [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, we had an occasion to write a process to read event logs on several sql servers to try to determine login times for different sql and Windows logins.  Since we have begun using PowerShell v2.0, and since get-eventlog now has a -computername parameter, it seemed like an obvious solution.</p>
<p>The event message we were interested in looked something like &#8220;Login succeeeded for uesr &#8216;UserName&#8217; &#8230;.&#8221;.  The code we were trying to use was:</p>
<pre class="brush: powershell">
get-eventlog -computername $servername -logname Application -message "Login succeeded for user*" -after ((get-date).AddDays(-1))
</pre>
<p>I expected that, given a date parameter and a leading string to match wouldn&#8217;t be too bad, but this ended up taking several minutes per server.  As there are over a hundred servers to scan, that didn&#8217;t work well for us.</p>
<p>We ended up falling back to get-wmiobject.</p>
<pre class="brush: powershell">
$BeginDate=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime((get-date).AddDays(-1))
get-wmiobject -class win32_ntlogevent -computerName $servername -filter "(EventCode=18453)  and (LogFile='Application') and (TimeGenerated &gt;'$BeginDate')"
</pre>
<p>Cons:</p>
<ul>
<li><span style="color: #000000;">We have to encode the date parameter (instead of using a nice datetime parameter like get-eventlog has)</span></li>
<li><span style="color: #000000;">We have to write a WQL where-clause to match the parameters<br />
</span></li>
</ul>
<p><span style="color: #000000;">Pros:</span></p>
<ul>
<li><span style="color: #000000;">We get to use the event code (rather than a string match)</span></li>
<li><span style="color: #000000;">The code is orders of magnitude faster (39 servers in 13 minutes as a test case)<br />
</span></li>
</ul>
<p><span style="color: #000000;"> I think that you might have a positive experience using get-eventlog if you need to scan a range of time (for instance if you&#8217;re reporting on what happened on the server), but if you need to look for a specific event (or set of events) you&#8217;re probably going to want to use get-wmiobject.</span></p>
<p><span style="color: #000000;">-Mike<br />
</span></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;title=Get-EventLog%20and%20Get-WMIObject" id="wpa2a_6"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2009/12/16/get-eventlog-and-get-wmiobject/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Executing SQL the Right Way in PowerShell</title>
		<link>http://powershellstation.com/2009/09/15/executing-sql-the-right-way-in-powershell/</link>
		<comments>http://powershellstation.com/2009/09/15/executing-sql-the-right-way-in-powershell/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 03:32:17 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=74</guid>
		<description><![CDATA[We all know that using string concatenation (or substitution) in SQL is a &#8220;bad thing&#8221;.  If you google &#8220;SQL Injection&#8221;, you&#8217;ll find hundreds of pages that tell you not to do things this way and that you need to use parameterized queries.  However, I still see a lot of code in PowerShell that does this: [...]]]></description>
			<content:encoded><![CDATA[<p>We all know that using string concatenation (or substitution) in SQL is a &#8220;bad thing&#8221;.  If you google &#8220;SQL Injection&#8221;, you&#8217;ll find hundreds of pages that tell you not to do things this way and that you need to use parameterized queries.  However, I still see a lot of code in PowerShell that does this:</p>
<pre class="brush: powershell; gutter: false">$cmd.ExecuteNonQuery("delete from Table1 where Column1='$value'")</pre>
<p>Since code like this is obviously prone to SQL injection attacks, it must be that doing it the right way is difficult, right?  Actually, no.  Here&#8217;s a simple function that allows you to run parameterized queries easily using dictionaries.</p>
<pre class="brush: powershell">function exec-query( $sql,$parameters=@{},$conn,$timeout=30,[switch]$help){
 if ($help){
 $msg = @"
Execute a sql statement.  Parameters are allowed.
Input parameters should be a dictionary of parameter names and values.
Return value will usually be a list of datarows.
"@
 Write-Host $msg
 return
 }
 $cmd=new-object system.Data.SqlClient.SqlCommand($sql,$conn)
 $cmd.CommandTimeout=$timeout
 foreach($p in $parameters.Keys){
 [Void] $cmd.Parameters.AddWithValue("@$p",$parameters[$p])
 }
 $ds=New-Object system.Data.DataSet
 $da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
 $da.fill($ds) | Out-Null

 return $ds
}</pre>
<p>Example usage:</p>
<pre class="brush: powershell">$conn=new-object data.sqlclient.sqlconnection "Server=servername;Integrated Security=True"
$conn.open()
exec-query 'select * from sys.databases' -conn $conn
exec-query 'select FirstName,LastName from AdventureWorks2008.Person.Person where FirstName=@fname' -parameter @{fname='Mike'} -conn $conn</pre>
<p>As you can see from the second example, using a parameterized function wasn&#8217;t much harder than it would have been to do string concatenation or substitution.  And you don&#8217;t have to worry about little <a href="http://www.bobby-tables.com/">Bobby Tables</a>.</p>
<p>The code above still needs some work to take care of output parameters, but I&#8217;ll save that for another post.</p>
<p>Let me know if you have any questions, comments, or complaints.</p>
<p>Mike</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fpowershellstation.com%2F2009%2F09%2F15%2Fexecuting-sql-the-right-way-in-powershell%2F&amp;title=Executing%20SQL%20the%20Right%20Way%20in%20PowerShell" id="wpa2a_8"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2009/09/15/executing-sql-the-right-way-in-powershell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  powershellstation.com/category/sql-server/feed/ ) in 0.20275 seconds, on May 19th, 2012 at 7:17 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 20th, 2012 at 7:17 am UTC -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- Quick Cache Is Fully Functional :-) ... A Quick Cache file was just served for (  powershellstation.com/category/sql-server/feed/ ) in 0.00057 seconds, on May 20th, 2012 at 1:22 am UTC. -->
