Quick Post – Creating SMO Type Accelerators


I was reading the AWESOME post from my good friend Boe Prox :

Quick Hits: Finding, Creating and Removing PowerShell Type Accelerators

and in that post he shows how to create your own type accelerator.

Well, generally speaking, a type acellerator is a short cut to some place or, in the Boe words, “a shorthand type for a full .Net type”.

That means we can play around the SMO .net type and create some pretty cool stuff.

First we need to load the type acceleratos : (using Boe Code)

$accelerators = [PSObject].Assembly.GetType(‘System.Management.Automation.TypeAccelerators’)

Then it is just add the new type :

$accelerators ::add(‘TypeName’,’.Net type’)

Lets play around SMO and create a SMO Server type accelerator for the Server Class:

[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SMO’)
$accelerators::Add(‘SMOSERVER’,’Microsoft.SqlServer.Management.Smo.Server’)
[SMOSERVER]’Vader’

and I can access all the members using :

([smoserver]’vader’).Member   – IE :

A Property :

([smoserver]’vader’).name

Or a Method

([smoserver]’vader’).KillAllProcesses(‘Master’)

We can also add the Database Class :

$accelerators::Add(‘SMODATABASE’,’Microsoft.SqlServer.Management.Smo.Database’)
[SMODATABASE]([smoserver]’Vader’).Databases[‘Master’]

Or Table class

$accelerators::Add(‘SMOTABLE’,’Microsoft.SqlServer.Management.Smo.Table’)
[SMOTABLE]([SMODATABASE]([smoserver]’Vader’).Databases[‘Master’]).Tables[‘MSreplication_options’]

This way you can add these code in your profile and load your type accelerator, simplifing your code.

Pretty cool !!!

Tkx Boe 🙂

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Algo que Esqueci de Categorizar. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s