internal/Import-OldSmo.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function Import-OldSmo {
    <#
 .SYNOPSIS
 Attempts to import older versions of SMO for things like Integrated Services
 
 .DESCRIPTION
 SMO 14, which is not yet RTM, does not appear to support Integrated Services Commands.
 This helps.
 
 .EXAMPLE
 Import-OldSmo
 
 #>


    $smoversions = "13.0.0.0", "12.0.0.0", "11.0.0.0", "10.0.0.0", "9.0.242.0", "9.0.0.0"

    foreach ($smoversion in $smoversions) {
        try {
            Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=$smoversion, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop
            $smoadded = $true
        }
        catch {
            $smoadded = $false
        }
        
        if ($smoadded -eq $true) { break }
    }
    
    if ($smoadded -eq $false) {
        throw "Sorry! The current SMO files we pre-package do not yet support some things. Run this on a computer with SSMS 2016 or below."
    }
    
    $assemblies = "Management.Common", "Dmf", "Instapi", "SqlWmiManagement", "ConnectionInfo", "SmoExtended", "SqlTDiagM", "Management.Utility",
    "SString", "Management.RegisteredServers", "Management.Sdk.Sfc", "SqlEnum", "RegSvrEnum", "WmiEnum", "ServiceBrokerEnum", "Management.XEvent",
    "ConnectionInfoExtended", "Management.Collector", "Management.CollectorEnum", "Management.Dac", "Management.DacEnum", "Management.IntegrationServices"
    
    foreach ($assembly in $assemblies) {
        try {
            Add-Type -AssemblyName "Microsoft.SqlServer.$assembly, Version=$smoversion, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop
        }
        catch {
            # Don't care
        }
    }
}