There are lots of ways to load the SMO assembly in PowerShell.
You can use the PowerShell V1 method:
This loads the most recent version of the assembly. It doesn’t provide a way to specify a version. For these reasons, the method has been obsoleted in the .NET API for a long time.
The PowerShell V2 way is to use the Add-Type cmdlet. According to Max Trinidad, this works well in an environment with one version of SQL Server:
I don’t work in such an environment, so the above command fails with an error.
You can specify the version to the Add-Type cmdlet, but you also have to specify the Culture and the PublicKeyToken of the underlying assembly.
I don’t know much about how .NET assemblies work. I do know that for this particular assembly, the Culture is always neutral, and the PublicKeyToken is always 89845dcd8080cc91.
Because the only attribute that varies is the version number, we can use this simple function to load an assembly of a specific version:
In my environment, the first few lines of output of the above script looks like this:
In my environment, the valid values of the Version parameter are ‘10.0.0.0’ and ‘22.214.171.124’. For you, it may vary.
To use this in another script, you might just want to copy the function body and hard-code the version number you need.
I don’t know how to create PowerShell function libraries yet, but I might like to add this to my own library of SQL Server functions.