question

JudyAnn avatar image
JudyAnn asked

Powershell function to get Windows Version & Load SMO

Does anybody have a Powershell function to retrieve the Windows Server Version of a server and load the appropriate SMO assembly? The .LoadWithPartialName() method I was using before is deprecated. I will be looping through a list of servers from 2008 to 2016. It appears I have to have a load statement for each possible version. It would be good to have this in a function that I can call when needed. I'm sure somebody has done this in a clean and simple way. Thanks for any help you can offer.
powershellfunctionsmoversion
2 comments
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

@JudyAnn From what I understand, even thought LoadWithPartualName is deprecated, it should still work. If not then Add-Type should work because it does not require a provision of the exact path to the assembly to load (which may differ for different versions of the servers): Add-Type -AssemblyName "Microsoft.SqlServer.Management.SMO" The assembly name is the same for all versions so it should work. If you need the server version in order to branch out the calls to Load (instead of using the Add-Type) then you can use [System.Environment]::OSVersion.Version
0 Likes 0 ·
It appears this is going to work as long as I specify the path name: Add-Type -Path 'C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Smo.dll' At least the servers I have checked have this file in this place. Thank you so much. I knew it could not be as complex as it appeared. Judy
0 Likes 0 ·

1 Answer

· Write an Answer
JudyAnn avatar image
JudyAnn answered
I will mark this question as answered with thanks to Oleg. His suggestion was to use this command which is not version dependent.: Add-Type -AssemblyName "Microsoft.SqlServer.Management.SMO" I found that this worked on all our servers as long as I used the path to where we put that dll. Add-Type -Path 'C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Smo.dll' None of the references I read pointed out that you might have to use the -path option if you stash the dll somewhere other than the default location.
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.