Home > Articles > Data > SQL Server

SQL Server Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

SQL Server 2008 PowerShell Provider

Last updated Mar 28, 2003.

Of the improvements in SQL Server 2008, many of them are aimed at the process of managing the system. Of course, there are a lot of development improvements as well, including new data types. When you consider the improvements in Visual Studio like LINQ, you have a pretty impressive package.

But there’s one improvement that spans both developers and DBA’s. I’ve written before about Microsoft’s new scripting language, called PowerShell. Those tutorials show you how to work with SQL Server by loading the Server Management Objects (SMO) library.

The SQL Server team at Microsoft has created a new “minishell,” which means that they took the code from PowerShell, embedded a set of methods that can interact with SQL Server, and then re-compiled it. Let’s take a closer look at what this shell provides, how it works, and how you can use it to manage your system and even interact with data and objects like a programming language.

This re-packaged version of PowerShell, which is named SQLPS.EXE, is included when you install SQL Server 2008. As of this writing in 2008, this is the only way to get the provider, but there are plans to make it available with a “feature pack” later on. The SQL Server PowerShell Provider (which I’ll just call SQLPS from here on out) can be installed side-by-side with another instance of PowerShell, or if PowerShell doesn’t exist on the system it will install the minishell automatically.

Something to keep in mind is that since the PowerShell provider is a copy of PowerShell version 1, when a newer version of PowerShell comes out you’ll have to wait for SQL Server to upgrade the SQLPS version to keep in line with the improvements. But there is a different way to work with this provider, which I’ll explain at the end of this article, which allows you to load the SQL Server Provider into the PowerShell environment that you already have. That way when you upgrade PowerShell, the SQL Server Provider comes along with it.

You can launch the PowerShell provider in two ways: by typing SQLPS.EXE at a command prompt, or by right-clicking an object in SQL Server Management Studio (SSMS). In the first case you’re dropped at a SQL SERVER prompt, and in SSMS you’re dropped wherever you launched it — in other words, if you right-click a table and select “Start PowerShell,” you’ll be placed at that table location.

And that’s the first interesting thing you’ll find in SQLPS. The various SQL Server objects, including tables, views, databases and even the server itself, are treated like a path on your hard drive. What that means is the standard “CD” (change directory) and “DIR” (show directory contents) work just as they would on a hard drive. Let’s look at an example.

I’ll start with the SQLPS.EXE command, since that places me in the “outermost” SQL Server container. At this level, there are four basic objects available with a DIR command:

Folder

Contains

SQLSERVER:\SQL

The Database Engine, SQL Server Agent, Service Broker, Database Mail

SQLSERVER:\SQLPolicy

Policy Based Management

SQLSERVER:\SQLRegistration

Registered Servers

SQLSERVER:\DataCollection

SQL Server 2008 Data Collector

So to step in to a SQL Server object, all I have to do is use the CD command as in DOS. Also, the “Tab” key will fill out things for you. Try it: Open the SQLPS.EXE command, then type CD and press the TAB key multiple times. It will cycle through the choices for you.

So what can you do with this information? Well, let’s say that you want to know the top ten tables in the AdventureWorks database, as tallied by row count. To do that, you take the knowledge you have from the PowerShell on piping commands from one side to another, and then you combine that with the knowledge you have now on navigating SQL Server like a drive, and you can find that information.

To try this out, first open SQL Server Management Studio. Find the AdventureWorks database, and then navigate down to the “Tables” node. Then right-click that node and select “Start PowerShell”. You’re placed not only in the SQL Server PowerShell provider window, but directly into the tables “directory”. Now run this command:

DIR| Sort-Object -Property RowCount -desc | Select-Object -First 10 | Format-Table schema, name, rowcount –AutoSize

Let’s dissect that a little. First, we use the DIR command to list all of the tables in the database. But before we show it on the screen, we send that output with the pipe to a PowerShell command-let, called sort-object. Its job is to, well, sort objects.

The sort-object command-let takes several parameters, and one of those is the property you want to sort on. In this case, the DIR command has a column called RowCount. Now how did we know that? Through another help command-let, called get-member. If you were to type DIR | get-member, you would see a list of properties that the tables ‘folder” knows about. One of those is the RowCount property.

We then add the –desc parameter to show the tables with the highest rowcounts at the top. Next, we pipe all that output to yet another command-let, called select-object. This command-let grabs a set of properties from the object passed to it. With me so far?

The parameter we use on select-object is –first, meaning the first set of objects in the list — which we limit to 10. From there, we pass all that information to the format-table command-let and set the parameters there to show the schema name, the name of the table, and the rowcount. The –autosize just fits all that to the screen.

The point to all this is that you can use what you know in layers. As you add a new concept to your knowledge, you can change the script to do even more to make things easier for your day.

Although I’ve been talking about the provider bundled in SQLPS.EXE, there is another way to work with PowerShell and the SQL Server Provider. It still has to be installed, of course, but you can add in the SQL Server Provider into PowerShell if you have that installed on your system. Here’s how:

http://msdn.microsoft.com/en-us/library/cc281962.aspx

InformIT Articles and Sample Chapters

From the book, Upgrading and Repairing Networks, 5th Edition, you can read this free article on planning for the logical design for your network.

Books and eBooks

If you’d like to learn a lot more about networks, you should check out the A+ certifications. Network+ Certification Practice Questions Exam Cram 2 (Exam N10-003), 2nd Edition is a good book to practice for the exams for networking for the A+ cert (read in Safari Books Online).

Online Resources

Windows Server 2008 has a lot of new firewall features, and some of those make working with SQL Server a little more complex. More on how to do that is here.