CTP3/V2b - Add-Type -a "Microsoft.SqlServer.Smo" won't load SMO assemblies - by MaxTrinidad

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 417844 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 2/25/2009 3:30:23 AM
Access Restriction Public


 had to stay working all night so I have tested the Add-Type in both Window 7 (V2b), WS08 (CTP3), and Vista (CTP3).  I'm still getting the error.
Windows 7 has SQL Server 2008 and SQL Server Express 2008 with the SMO SQL 2008 feature pack - fail
Windows Server 2008 has only one SQL Server 2008 Enterprise with the SMO SQL 2008 feature pack - fail
Windows Vista have all three SQL Servers (2K, 2K5, and 2K8) with the SMO SQL 2008 feature pack - fail
Windows Server 2003 with PowerShell (CTP2), SQL Server 2000 and SQL Server 2005 SMO Feature pack - WORKS GREAT!
The clue on this message is that, on machine with SQL 2008, the error will come up "...'Microsoft.SqlServer.Smo, Version=".  
This is for SQL Server 2005.  It should be "..., Version=10.0...".  Why??  it most be a bug only with SQL Server 2008.
Here's the error again:
PS C:\> Add-Type -a "Microsoft.SqlServer.Smo"

Add-Type : Could not load file or assembly 'Microsoft.SqlServer.Smo, Version=, Culture=neutral, Publ

=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

At line:1 char:9

+ Add-Type <<<<  -a "Microsoft.SqlServer.Smo"

    + CategoryInfo          : NotSpecified: (:) [Add-Type], FileNotFoundException

    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.AddTypeCommand
Any other comments??
Sign in to post a comment.
Posted by Microsoft on 2/25/2009 at 10:50 AM
For a background on the issue, see http://www.leeholmes.com/blog/HowDoIEasilyLoadAssembliesWhenLoadWithPartialNameHasBeenDeprecated.aspx.

The list is hard coded so that anybody writing “Add-Type –Assembly Microsoft.SqlServer.Smo” gets the same version. It’s not about security, it’s really all about functionality. Add-Type loads v9 of SMO. The fact that you’re running into this issue means that the corresponding script is at high risk for breaking anyways through LoadWithPartialName(), as it pulls SMO 10.0 on one machine, and SMO 9.0 on another.

Here’s a few perfect examples of LoadWithPartialName (on SMO, even) breaking scripts:
-    http://www.codeplex.com/SQLPSX/Thread/View.aspx?ThreadId=35302
-    http://sqlblog.com/blogs/allen_white/archive/2007/10/19/sql-2008-smo-doing-a-little-rearranging.aspx
-    http://sqlblog.com/blogs/allen_white/archive/2009/02/20/smo-changes-from-sql-2005-to-sql-2008.aspx

Add-Type accepts a strong name as an argument, so if you know you are working against the 10.0 library, you can use its strong name:

Add-Type -Assembly 'Microsoft.SqlServer.Smo, Version=, Culture=neutral, PublicKeyToken=89845dcd8080cc91'

Now, if you know it will work on both versions (and have tested it, etc,) then by all means accept either:

## Load either v9 or v10 SMO assemblies
try { Add-Type -Assembly Microsoft.SqlServer.Smo }
catch { Add-Type -Assembly 'Microsoft.SqlServer.Smo, Version=, Culture=neutral, PublicKeyToken=89845dcd8080cc91' }

And if this is for personal use and you don’t mind debugging random script breaks by hand, LoadWithPartialName() is a perfectly acceptable alternative.