posted on Tuesday, January 16, 2007 3:38 PM by marathonsqlguy

PowerShell and SMO

I started seriously trying to learn the new Windows PowerShell scripting language this week. I started with the TechNet Webcast: An Overview of Windows PowerShell (Level 200) that Don Jones did in December. I worked with Don at Microsoft on the MCITP:Database Developer Objective Domain, and he's got a great presentation style, so that complicated things become clear quickly. I then started searching for sites that specifically used SMO and PowerShell together.

After remembered having read about using SMO from PowerShell in Bob Beauchemin's blog, I downloaded his sample code and found the directory with his scripts. I had difficulty getting them to work, so started searching again.

The next thing I found was in Dan Sullivan's blog, where he used a script to initialize the environment and create some functions he calls PowerSMO. His code disallows its use in production code, though, so I continued my search.

I then found Rob Farley's blog, and he presented a much simpler script, which I was able to play with and actually start extracting information about servers other than my local server using SMO.

That, in fact, is my litmus test of a good demo - does it work against a server other than my local server? In my presentation at PASS on SMO I cautioned the attendees that my examples were in VB.Net because VBScript only allows you to connect with your local default server. Peter Ward had a session on SMO two days later where he told those present that there was "no problem" using VBScript to script SMO tasks, and then proceeded to demonstrate his code, connecting to his local default server. Don't get me wrong, I like Peter, and have learned from his sample code, but I felt he did a disservice to those present in his session.

So, now I'm working on building some useable scripts using PowerShell and SMO. I hope to put together a presentation once I get comfortable with the new language because I see amazing possibilities with it.

Allen

Comments

# re: PowerShell and SMO @ Tuesday, January 16, 2007 3:59 PM

Allen,

The script from my SQL user-group presentation on Powershell can be downloaded from:
http://msmvps.com/files/folders/robfarley/entry482629.aspx

Rob

Rob Farley

# re: PowerShell and SMO @ Tuesday, January 16, 2007 4:52 PM

It can be really tough to connect to a different server in a demo. You often don't have the same connectivity you'd have somewhere else, and the last thing you want is to have something fail because of a connectivity issue.

When I present, I prefer to be offline. But I can appreciate it could be good to try to connect to a remote server, with a fallback plan in case you can't connect nicely.

Rob Farley

# re: PowerShell and SMO @ Wednesday, January 17, 2007 9:09 AM

Rob, thanks for the link to the script. I understand the comments about demo environments and in my PASS presentation I used two named instances running on my demo machine so the attendees could see how to connect to a specified server. Until I found your script I was having trouble figuring out how to connect to a specific server, and that's what engendered my comment.

Allen

marathonsqlguy