SQLvariations: SQL Server, a little PowerShell, maybe some Hyper-V Rotating Header Image

SQL Server 2008

Implicit Remoting with PowerShell

Last week I spoke to the Atlanta PowerShell User Group about Remoting and Background Jobs.  There are a couple different flavors of Remoting but the one I’m most excited to show people is called Implicit Remoting.  What if you were on a machine that only had SSMS 2005 and you had to run something against a remote server that had SSMS 2008?  Enter Implicit Remoting.

With Implicit Remoting, essentially you temporarily download the cmdlets from the remote machine onto your local machine. When you execute the cmdlet it runs against the remote machine.

Before we start I want to set the scene.  On your local machine you need to have PowerShell 2.0.  The remote machine needs PowerShell 2.0 and SSMS 2008 installed.

Setting up the Remove Server
Log into the remote server via Remote Desktop and create a profile (notepad $pshome\profile.ps1) that contains this:

add-pssnapin SqlServerCmdletSnapin100
add-pssnapin SqlServerProviderSnapin100
 

Next create a new Remoting Configuration to load your SQL Items into your profile:

(* I used the ISE profile)

Register-PSSessionConfiguration -Name SQLSupport `
-StartupScript C:\Users\YourUserNameHere!!!\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

Setting Up Your Local Machine
Open a Power Shell window that has no profile loaded. Run the following:

$RemoteSession = New-PSSession -ComputerName MyRemoteServer -ConfigurationName SQLSupport

Connect to the remote session you just defined in the variable:

Enter-PSSession $RemoteSession

Run this command to test your setup. You will see the SQL Server cmdlets from the snapin that was loaded with the Remoting Configuration.

get-command invoke-sqlcmd

Exit the session:

Exit-PSSession

image

Now we want to Import invoke-sqlcmd.  This will allow you to run your scripts against the remote server session that you defined.  This might not seem like much, but if you are working over a VPN from home this could be very useful.  Plus you don’t have to keep switching between your local shell and the remote one (*more on that another day).

Import-PSSession $RemoteSession -CommandName invoke-sqlcmd
get-command invoke-sqlcmd

Putting it to Use
Now that all of this is done what command will you run?  Well anything.  Just because I wanted to see if it could be done I went ahead and ran Back Woodys ‘backup all user databases script’.   Run anything you want, but a good start is something simple like:

invoke-sqlcmd -query "sp_databases" -database master `
-serverinstance localhost | format-table
Your Environment
Now please think outside the box because the SQL cmdlets might not be the ones that you personally want to download.  I was just using them as a common example us SQL folks could talk about.  You might be more interested in downloading the Clustering cmdlets in your case.  Or, you might see this as a tool to centralize scripts that someone else in your company might ocassionally use but doesn’t need to worry about keeping  locally (think CodePlex projects).  You might even want to do the reverse for some reason, you might want to be able to download cmdlets from your local machine to a server for a one time use so that you don’t have to install anything.  Either way I hope you remember this as one of the tools available to you.

Happy scripting.

I’ll be speaking at SQL Satrurday #40 in Miami

imageIt feels like forever since I spoke at a SQL Saturday!  The South Florida SQL Saturday will be help on July 31st, 2010 at Devry University – South Florida, 2300 SW 145 Ave, Miramar, FL 33027. Event checking will be at 7:30 with the Keynote beginning at 8:00 and sessions beginning at 8:30.    I can’t wait to get down there.

Map pictureMax Trinidad ( blog | twitter ) has put together an entire day of PowerShell training for this event and I get to be one of the speakers!  I’ll be doing my PowerShell for Data Professionals session.  Which I am very proud to say I will be presenting  a longer and more in depth version of this session at this year’s PASS Summit, so come cathch the preview!  I’ve got a new trick to show the fine folks of South Florida and I can’t wait to get their feedback.

Besides myself the three other PowerShell speakers are going to be: Ronald Dameron ( blogtwitter ), David Corrales from Sapien, and of course Maximo Trinidad.

Here’s what that PowerShell track is going to look like:

PoSh
Ronald Dameron
Why SQL Server DBAs should learn PowerShell
Ronald Dameron
Automate ID Administration w/ PowerShell & SQLPSX
Aaron Nelson
PowerShell for the Data Professional
David Corrales
Sneak Preview: SAPIEN’s Visual PowerShell 2011
Maximo Trinidad
Working with SQL Server – SQLPS
Maximo Trinidad
Using PowerShell with SQL Server Agent

PowerShell Week at SQL University – Post 3: Providers, PSDrive

Providers

What are Providers? Providers in PowerShell are a hierarchical namespace way flattening out an infrastructure to make it accessible and traverse-able just like a directory structure hard drive. Why do you need them? Well you quite possibly may not ‘need’ them per-say but they sure do make things easier to work with from time to time. In case you don’t already know, you can traverse your SQL Server [2008] as if it were just another drive in either SSMS 2008 or in the PowerShell ISE if you add the Provider Snapin.

To make sure that you have this snapin installed on your machine see yesterday’s post but this time we are going to add the ProviderSnapin instead of the CmdletSnapin.

image

To do that just run

add-pssnapin SqlServerProviderSnapin100

and poof you can traverse SQL Server. Let’s take a look at what we might see. Run

get-psdrive

and you should see a list of drives including SQL Server. Now if you have more that one instance you will be able to access all of them through this one “SQLServer:\” PSDrive that you now have. In a lot cases what we find inside of these drives will be similar to what we see in the Object Explorer view in SSMS but almost always have something extra too. Let’s take a look at that here by changing directories down to our databases

cd SQLSERVER:\sql\YourComputerName\YourInstanceName\

cd SQLSERVER:\sql\WIN7\KILIMANJARO\

Now let’s go ahead and run the good old “Dir” command and we should get back something like this

dir

image

As I mentioned before, we got back more than we see if we expand our SQL Server node in the Object Explorer window of SSMS, now if we CD to Databases and do another dir we will see not just more than what we see in SSMS, we see what at first glace to a SQL person is just garbage (but it’s not).

cd Databases
dir

image

What you’re seeing here is the Methods and Properties of the databases in your instance. What are Methods and Properties? Well if you’re like I was 6 months ago you have no idea what Methods and Properties are. Methods are the Verbs of what you can do to your database (Create, Rename, Drop, Shrink) and Properties and the Adjectives that describe your database (Collation, CreateDate, Compatibility Level, Owner, AutoShrink Enabled). To get just the list of the names of your databases you’re going to have to run this:

dir | select-object name

image

Ok so now we can see a list of databases that our Provider made available to us. So what? Well I’m going to dive a little deeper tomorrow but for now let’s just go with one final example on scripting out tables (more on that here). Run this: cd AdventureWorks\Tables Then this:

foreach ($tbl in dir )

{
$k="C:\Temp\" + $($tbl.name) + "_table.SQL"
$tbl.Script() > $k
}

I really hope you have temp directory on your C:\ drive. If so go have a look what’s there now :-)

So is that it? Almost. While SQL Server 2008 only comes with one provider and it’s for the Relational Engine a few people have taken it upon themselves to create their own Provider for things like Analysis Services, Reporting Services, and event BizTalk up on the codeplex site. I can say that I have tried the Analysis Services project and it does work.

PowerShell Week at SQL University – Post 2

Cmdlets, Functions and Snapins

So far we’ve learned how to open up PowerShell and add on to it with some modules; then we learned how to create a variable and populate it with anything from some numbers to an entire table. Today we’re going to quickly look at another way of expanding PowerShell with something called a snapin so that we can get to some cmdlets.

First, what are cmdlets? Cmdlets are similar to DOS commands but have some noteworthy differences. In SQL Server terms you might think of cmdlets as the System Stored Procedures of PowerShell and think of functions as the regular user defined Stored Procedures of PowerShell.

Cmdlets and functions accept parameters just like stored procedures do. Just like stored procedures you can even define default values for these parameters in case the user doesn’t have one to pass in. Unlike stored procedures though there is actually a drive that you can go to and see all of your functions. Just run this and you’ll see what I mean dir function:

Snapins are akin to finished product code. They were created for PowerShell 1.0 and were succeeded in PowerShell 2.0 by modules– which are more like the manifest list for deploying a bunch of SSIS packages. If you have SSMS 2008 installed you’ve got yourself two extra snapins already. And if you have Idera’s SQL Diagnostic Manager installed you may be in for a real surprise when you retrieve your list of snapins. Run this command to see all of the extra snapins that are available to you:

get-pssnapinregistered

At the very least you should see this:

image

If you don’t see those two you’ll need to install SSMS 2008. If you don’t have a copy of that handy you can get the free version of it here. The SqlServerCmdletSnapin100 snapin give us the 5 cmdlets that come with SQL Server 2008 and today we’re going to focus on Invoke-SQLcmd which basically just calls out to good old sqlcmd. To load this just run:

add-pssnapin SqlServerCmdletSnapin100

From there we can do a quick query like this:

invoke-sqlcmd -query “sp_databases” -database master -serverinstance YourServerName\YourInstanceName | format-table

invoke-sqlcmd -query "sp_databases" -database master -serverinstance WIN7\Kilimanjaro | format-table
image

Now the reason that I brought up functions is because they override cmdlets and that’s really important to know. If you run this command get-command invoke-sqlcmd you will only see one thing returned for invoke-sqlcmd. However if we create a new function like:

function invoke-sqlcmd { get-date }

You end up overriding the Invoke-SQLcmd with your new function. Give it a try, run just this invoke-sqlcmd and you should end up seeing something like this:

image

So how do you know when you’ve done something like that? Just run get-command invoke-sqlcmd again and this time you will see this:

image

I realize that we kind of rushed through that info but thankfully there is only one more piece to the basics before we can get on to code that you can use on a daily basis :-)

PowerShell Week at SQL University – Post 1

Getting Started With PowerShell Variables

Yesterday we laid the ground work for PowerShell Week.  Today we will learn about Variables in PowerShell. You will notice that PowerShell variables work very differently from SQL Server variables.

The evolution of variables in SQL Server is pretty straight forward. In SQL Server 2000 you had to DECLARE a variable as a specific type in one statement and then SET the value of the variable in another statement, like this: DECLARE @SQLvariable VARCHAR(12) SET @SQLvariable = ‘OldFeature’. By the time SQL 2008 hit the streets we could DECLARE and SET a variable in one statement like this: DECLARE @SQLvariable VARCHAR(12) = ‘NewFeature’ . PowerShell evolves variables to the next logical step.  You no longer need to declare or type your variable. Instead PowerShell uses the value of the variable to infer it’s type. If a variable does not alreayd exist PowerShell simply creates it for you.  In PowerShell our statement looks like this: $SQLvariable = ‘NewFeature’. (Beginer tip: $ identifies variables in PowerShell just like @ identifies them in SQL Server.)

Since it doesn’t work in SQL let’s try it in PowerShell: $SQLvariable = ‘NewFeature’.  OK so we loaded data into a variable, ‘big deal’ right?  How do we know that we actually loaded it and more importantly what datatype is it?  Well in PowerShell you can simply call the variable to get its value. (Another beginer tip: you don’t need to use a keyword like SELECT for this, just the variable name: $SQLvariable.)  What if you just want to determine the datatype?  All you have to do is tack on the .GetType() Method: $SQLvariable.GetType().

$SQLvariable = 'NewFeature'
$SQLvariable
$SQLvariable.GetType()

When you run this here’s what your result will be:

image

As I alluded to earlier, another feature of PowerShell is that it remembers your variables for you, even between executions.  Basically as long as you don’t end your session the variable, it’s datatype, and its value will persist.  You will see later than you can also set a variable in one script and consume it in another.

Let’s try this again with a new variable and set it to a different value:

$PoShvariable = 123
$PoShvariable
$PoShvariable.GetType()

image

What happens if once the datatype is set we try to set it to a different, incompatible datatype?  It’s going to explode right?  Nope, you’ll have to throw something more difficult than that at it.  Give this a try:

$PoShvariable.GetType()
$PoShvariable = 'NewFeature'
$PoShvariable
$PoShvariable.GetType()

 

 image 

Since PowerShell relies on .NET a variable could be any datatype in .NET so if you’re sitting inside the AdventureWorks database you can see for yourself, just run this:

$MyTable = get-item TABLESProduction.TransactionHistory
$MyTable.GetType()            

$MyStoredProc = get-item StoredProceduresHumanResources.uspUpdateEmployeePersonalInfo
$MyStoredProc.GetType()
image

Now earlier I mentioned that PowerShell keeps track of and remembers the variables that you previously declared.  So how do you see what variables are already there?  Just run this command and you can see every variable you have running in the session, even the internal ones.

Variable

In the list of items that get returned you’ll see one called Error that holds the last error message that occurred in your session.  This handy feature can help you when you have to debug your own work.

Is that all there is to know about variables in PowerShell?  Not hardly, but I think that’s a good stopping point for this post.  More on variables later.

PowerShell Week at SQL University – Post 0

Welcome to PowerShell Week at SQL University. For regulars at SQLvariant, SQL University is something Jorge Segarra organized to get industry experts together to create learning courses around SQL Server. These learning courses are in one week segments that focus on a specific area.

When I began blogging about PowerShell there were some complications based on your operating system. There were so many people blogging about these issues that I didn’t address it initially. Since this is an introductory course I decided to go ahead and spin up a brand new Windows 7 VM so that I can walk you through these small but vital details. If you don’t have Windows 7 this would be a great time to upgrade. If you can’t upgrade then download PowerShell for you operating system from this link. Now, onto getting started:

Disclaimer:
PowerShell opens in a protected state as a security measure. To enjoy the full power of PowerShell (and run scripts you downloaded off of the internet) you need to run without these safeguards. In today’s lesson we are simply launching PowerShell and priming it for the scripts and work we will interact with in the next several lessons.

To get started with PowerShell just click the Start button and type in ISE to locate the Integrated Script Editor in your program listing. Right click and launch PowerShell in Run as Adminsitrator mode:

image

Enter Get-ExecutionPolicy as a command. If you haven’t used PowerShell yet you will most likely see that the Execution Policy is set to “Restricted”. You’ll need to run the command Set-ExecutionPolicy RemoteSigned to be able to run the scripts and follow along with the lesson. You’ll also get a popup that you’ll have to say yes to.

image

Your output should look very non-informative like this:

image

Now that we have that set let’s go to SQLPSX.CodePlex.com and and download the SQLPSX project. It’s currently in version 2.2.1 and consists of 9 modules containing 133 advanced functions, only 2 cmdlets and 7 scripts. It’s for working with ADO.NET, SMO, SQL Agent, RMO, SSIS, SQL script files and using the PowerShell ISE as a SQL query tool:

NOTE: Update at bottom with workaround:

image

During the installation we will run it without modifying the profile (just yet).

image

Once you’ve installed it successfully you should now have a WindowsPowerShell directory under your My Documents if you didn’t have one already (which is really important):

image

After it’s installed we should be able to open up the ISE again and run this command :

Get-Module -ListAvailable

image

And finally we’re going to get to the step where we load something up. Run import-module SQLServer to load up the main SQL Server module. After you run that command it won’t give you any output telling you that it successfully loaded so you can run this command write-host -foregroundcolor DarkGreen “Modules Loaded: $(get-module)” to have it tell you what Modules it currently has loaded.

image

Alright that pretty much wraps it up for today. I know we didn’t do a lot with PowerShell and SQL Server yet but this gets some important ground work out of the way so that hopefully everyone is able to script along for the rest of the week. (If you’re dying to read ahead have a look at this post: Basic Querying From PowerShell)

If you run into any issues please leave a comment and I’ll be glad to lend any help that I can. See all you back here tomorrow for the next post in this series Post 1.

UPDATE:

There seems to be an issue with loading the SQLServer Module as I had described. I have found this to be a work-around: Go to \\~\My Documents\WindowsPowerShell\Modules\SQLIse\Modules\ and copy everything except for the SQLISE folder, then go to the \\~\My Documents\WindowsPowerShell\Modules\ folder and paste all of those items. At this point you should now be able to run the command and get it to load up properly.

I will post more when I found out other alternatives.

PASS Virtual Chapters Late April Presentations

To finish up the month of April we’ve got sessions from AppDev and DBA and I’m even including one from PASS on ‘Intro to Speaking At PASS’.  The AppDev group will be hosting the SARGability session twice, once for people having Lunch in Australia or Dinner in L.A. and once as a Late lunch for people in the UK (*I think).

pass_logo
_____________________________________________________________
Understanding SARGability (to make your queries run faster)
April 27th 12:30 PM Adelaide, AUS CST  (GMT +9:30) [April 26th at 8:00 PM PDT (GMT -7)]
Add to Calendar (AUS)
April 27th 8:00 AM EDT (GMT -4)
Add to Calendar
Presenter: Rob Farley

SARGable means Search ARGument able. It relates to the ability to search through an index for a value, but unfortunately, many database professionals don’t really understand it. This can lead to queries which miss out on the potential to run much quicker. This is a talk involving lots of demos, showing plenty of queries and execution plans. Any time I’m not typing on the screen, I’ll be waving my hands around to explain things, with apologies to those tuned in through Live Meeting.

Rob Farley:
rob_side
Rob Farley runs LobsterPot Solutions, a consultancy based in Adelaide, Australia. He has been consulting in IT since completing a Computer Science degree with first class honours in 1997 and using SQL Server since 1998. He is a regular presenter at conferences such as TechEd Australia, heads up the Adelaide SQL Server User Group and holds many Microsoft certifications. He is a Microsoft Certified Trainer and is a recipient of the Microsoft MVP Award for SQL Server. Rob has also helped create several exams for Microsoft Learning, and has recently written two chapters for the book SQL Server MVP Deep Dives. Rob’s blog can be read at http://sqlblog.com/blogs/rob_farley/

How do I view the presentation?

Attendee URL (AUS):  Live Meeting link

Attendee URL: Live Meeting Link
_____________________________________________________________

SQL Server 2008 Audit
April 28, Noon Eastern Time (GMT -4)
Presenter: Thomas LaRock

With SQL Server 2008 you now have the ability to set up auditing with SQL Server Audit. Attend this session for an overview of SQL Server Audit, learn how to initially configure an audit for your server or databases, and examine the audit logs to report on the information that is captured.

Thomas LaRock:
DBA_Survivor
Thomas LaRock is a seasoned IT professional with over a decade of technical and management experience. Thomas is a member of Quest Software’s Association of SQL Server Experts, currently serves on the Board of Directors for the Professional Association for SQL Server (PASS), and is a SQL Server MVP. Thomas can also be found blogging at http://thomaslarock.com and is the author of DBA Survivor: Become a Rock Star DBA.


Door Prize: There will be a drawing for a $50.00 Amazon Gift Certificate
Registration: You can attend the meeting without registering but if you want to be entered in the drawing, you must register at https://www.livemeeting.com/lrs/8000181573/Registration.aspx?pageName=fmxpl8tdzwcrc6z0 no later than 5:00 PM Eastern on April 27th.

Live Meeting Link: https://www.livemeeting.com/cc/8000181573/join?id=6K8GPR&role=attend

_____________________________________________________________

Intro to Speaking At PASS: Creating a Winning Abstract (If I Can Do It, Anyone Can!)
April 28, Noon Central Time (GMT -5)
Presenter: Chuck Heinzelman

Have you ever attended a conference (such as PASS) and thought to yourself, “Wow, I wish I could speak at something like this!” Speaking at a conference like PASS is not something reserved for the superstar speakers. Each year PASS actively calls out for presentation proposals from members of the SQL Server community. During this session we’ll talk about what goes into creating a submission for the call for speakers with the goal of reducing the fear and anxiety that can go along with the process.

Live Meeting Link: https://www.livemeeting.com/lrs/0000000379_116/Registration.aspx?pageName=nk4md836r0tzp94j

______________________________________________________

Coming in May…

Stuart Ainsworth presents “You Got XML In My Database? What’s Up With That?”

and

Patrick LeBlanc presents “Introduction to SQL Server Profiler”

LiveJournal Tags: ,,,

I’ll be speaking at CodeStock!

I’ll have more info for you guys later but this will be my first non-SQL-centric event that I will be speaking at so I just wanted to share :-)  I’m extra-excited about this event because this year they are making a push for Women In Technology.  Here’s a snippet from that announcement:

This year we are hoping to aid in raising awareness for women in technology.  The US Department of Labor reported in 2009 women made up only 22.9% of computer programmers, and just 20.9% of computer software engineers.  For comparison, women chief executives counted for 23.4% – more women are CEOs than programmers!  (As an aside, 79.2% of meeting and convention planners are women, I’m a rarity it seems.)

This problem isn’t a simple one, and it not going to be fixed by a clever logo, but we can help by raising awareness.

Here’s some more info on the event:

The theme for CodeStock 2010 is about connections.  Connecting with peers and building a professional network.  Connecting with IT roles outside of your own.  Connecting with the people who can help make your ideas reality.

  • Keynote by Microsoft Developer Evangelist Rachel Appel
  • Sessions for Developers, IT Professionals, and Entrepreneurs
  • 100+ break out sessions + Open Spaces (self-organizing sessions)
  • Panel discussions and live podcasts
  • CodeStock Social Event to meet and make connections

Space is limited so register today at CodeStock.org

Join me at CodeStock