Wednesday, May 02, 2007 - Posts

Using PowerShell to browse SMO objects

I'm trying to solve a problem with one of my applications, and need to browse the SMO objects for a particular database. In the past I'd have fired up VB.Net and gone into the debugger and poked around that way. Not so any more with PowerShell! I open up a PowerShell session and enter these commands to establish the environment, connect to the server, and grab the database object:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") 
$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'MyServer'
$db = $s.databases['AdventureWorks']
$db

Now, I can see the list of values in each of the properties at the Database level. If I want to drill down, say, into the DatabaseOptions properties, it's this simple:

$dbo = $db.DatabaseOptions
$dbo

And now I can see the values for each of the properties in the DatabaseOptions object. Very cool.

Allen