- Introduction
-
Table of Contents
- Microsoft SQL Server Defined
- Microsoft SQL Server Features
- Microsoft SQL Server Administration
- Microsoft SQL Server Programming
- Performance Tuning
-
Practical Applications
- Choosing the Back End
- The DBA's Toolbox, Part 1
- The DBA's Toolbox, Part 2
- Scripting Solutions for SQL Server
- Building a SQL Server Lab
- Using Graphics Files with SQL Server
- Enterprise Resource Planning
- Customer Relationship Management (CRM)
- Building a Reporting Data Server
- Building a Database Documenter, Part 1
- Building a Database Documenter, Part 2
- Data Management Objects
- Data Management Objects: The Server Object
- Data Management Objects: Server Object Methods
- Data Management Objects: Collections and the Database Object
- Data Management Objects: Database Information
- Data Management Objects: Database Control
- Data Management Objects: Database Maintenance
- Data Management Objects: Logging the Process
- Data Management Objects: Running SQL Statements
- Data Management Objects: Multiple Row Returns
- Data Management Objects: Other Database Objects
- Data Management Objects: Security
- Data Management Objects: Scripting
- Powershell and SQL Server - Overview
- PowerShell and SQL Server - Objects and Providers
- Powershell and SQL Server - A Script Framework
- Powershell and SQL Server - Logging the Process
- Powershell and SQL Server - Reading a Control File
- Powershell and SQL Server - SQL Server Access
- Powershell and SQL Server - Web Pages from a SQL Query
- Powershell and SQL Server - Scrubbing the Event Logs
- SQL Server 2008 PowerShell Provider
- SQL Server I/O: Importing and Exporting Data
- SQL Server I/O: XML in Database Terms
- SQL Server I/O: Creating XML Output
- SQL Server I/O: Reading XML Documents
- SQL Server I/O: Using XML Control Mechanisms
- SQL Server I/O: Creating Hierarchies
- SQL Server I/O: Using HTTP with SQL Server XML
- SQL Server I/O: Using HTTP with SQL Server XML Templates
- SQL Server I/O: Remote Queries
- SQL Server I/O: Working with Text Files
- Using Microsoft SQL Server on Handheld Devices
- Front-Ends 101: Microsoft Access
- Comparing Two SQL Server Databases
- English Query - Part 1
- English Query - Part 2
- English Query - Part 3
- English Query - Part 4
- English Query - Part 5
- RSS Feeds from SQL Server
- Using SQL Server Agent to Monitor Backups
- Reporting Services - Creating a Maintenance Report
- SQL Server Chargeback Strategies, Part 1
- SQL Server Chargeback Strategies, Part 2
- SQL Server Replication Example
- Creating a Master Agent and Alert Server
- The SQL Server Central Management System: Definition
- The SQL Server Central Management System: Base Tables
- The SQL Server Central Management System: Execution of Server Information (Part 1)
- The SQL Server Central Management System: Execution of Server Information (Part 2)
- The SQL Server Central Management System: Collecting Performance Metrics
- The SQL Server Central Management System: Centralizing Agent Jobs, Events and Scripts
- The SQL Server Central Management System: Reporting the Data and Project Summary
- Time Tracking for SQL Server Operations
- Migrating Departmental Data Stores to SQL Server
- Migrating Departmental Data Stores to SQL Server: Model the System
- Migrating Departmental Data Stores to SQL Server: Model the System, Continued
- Migrating Departmental Data Stores to SQL Server: Decide on the Destination
- Migrating Departmental Data Stores to SQL Server: Design the ETL
- Migrating Departmental Data Stores to SQL Server: Design the ETL, Continued
- Migrating Departmental Data Stores to SQL Server: Attach the Front End, Test, and Monitor
- Tracking SQL Server Timed Events, Part 1
- Tracking SQL Server Timed Events, Part 2
- Patterns and Practices for the Data Professional
- Managing Vendor Databases
- Consolidation Options
- Connecting to a SQL Azure Database from Microsoft Access
- SharePoint 2007 and SQL Server, Part One
- SharePoint 2007 and SQL Server, Part Two
- SharePoint 2007 and SQL Server, Part Three
- Querying Multiple Data Sources from a Single Location (Distributed Queries)
- Importing and Exporting Data for SQL Azure
- Working on Distributed Teams
- Professional Development
- Application Architecture Assessments
- Business Intelligence
- Tips and Troubleshooting
- Additional Resources
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.
