VSTSC's Community Server

Community Server: The platform that enables you to build rich, interactive communities.
VSTeamSystemCentral: Your Team System community!

Welcome to VSTSC's Community Server Sign in | Join | Help
in Search

Tim Benninghoff

A SQL Server DBA attempts a personal IT project and documents the (mis)adventures.

Expandable!

When I start to learn a new technology, I find I learn best if I jump right in, get my hands dirty, and play around with it for a while.  After a couple initial ah-HA moments and early successes, I inevitably hit a wall where I can't seem to solve a particular problem.  By that time, I've built up quite a list of questions, and I'm ready for a deeper understanding of what's really going on.

At that point it's time for me to pick up a tech book and read.  And that's when I get a couple more ah-HA moments almost right away.  Case in point:  I've been working with PowerShell and SQL Server on a regular basis now for a couple months, and I've written a couple of scripts based on some tutorials I've found online and some excellent articles and blog posts people have written.  I'd hit that wall, and so based on the recommendations I'd read on a couple blogs, I picked up Bruce Payette's Windows PowerShell In Action. It's turning out to be an excellent read.

In a previous blog post I had written some code that had this little gem in it:

Get-Content $tblfile | ForEach {$trdir=$fulldir+"\dbo."+$_+"\triggers\"; cd $trdir; dir | ForEach{$_.Script()>> $outfile}}

Experienced PowerShell users can probably spot my code silliness right away.  As Bruce Payette's book explains, variables are expanded when placed within double quotes.  So, in the case above, assigning the string to $trdir is an extra, unnecessary step.  Knowing this, I could have saved my precious fingers for my accordion playing by dropping the $trdir assignment:

 Get-Content $tblfile | ForEach {cd "$fulldir\dbo.$_\triggers\"; dir | ForEach{$_.Script()>> $outfile}}

Were you to know that I was a DBA who primarily worked with T-SQL, this mistake would make sense.  T-SQL doesn't have something as nifty as expandable variables within quotes...but that doesn't make me love it any less.  But, you're a persistent reader, you went back and read my previous blog post, and you're quick to point out 'Tim!  You used double quotes to expand variables earlier in that same script!!!'  Would you believe that sometimes computers speak to me and I just KNOW what I should be doing without understanding it?  No?  You, reader, are much to clever and should be writing your own blog.  I promise to read it.

Published Wednesday, February 18, 2009 6:46 PM by timbenninghoff
Filed under: ,

Comments

No Comments
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems