|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Loads the latest SQL Server SMO .NET assembly installed on your machine |
| 4 | + |
| 5 | +.DESCRIPTION |
| 6 | + Loads the latest SQL Server SMO .NET assembly installed on your machine |
| 7 | + |
| 8 | +.EXAMPLE |
| 9 | + To call from other Scripts use: |
| 10 | + Import-Module LoadSQLSmo |
| 11 | + LoadSQLSMO |
| 12 | + |
| 13 | +.Inputs |
| 14 | +
|
| 15 | +.Outputs |
| 16 | + |
| 17 | +.NOTES |
| 18 | + Loading SQL SMO Assemblies - let me count the ways |
| 19 | +
|
| 20 | + http://www.madwithpowershell.com/2013/10/add-type-vs-reflectionassembly-in.html |
| 21 | +
|
| 22 | + 1) |
| 23 | + Original PShell 1/2 method: |
| 24 | + [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null |
| 25 | +
|
| 26 | + 2) Quick Way - but what version do you load? |
| 27 | + Add-Type -AssemblyName “Microsoft.SqlServer.Smo” |
| 28 | +
|
| 29 | + 3) "Recommended Way" |
| 30 | + Add-Type –AssemblyName “Microsoft.SqlServer.Smo, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91” |
| 31 | + |
| 32 | + 4) Hard-Coded filepath |
| 33 | + Add-Type -Path 'C:\Program Files\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.Smo.dll' |
| 34 | +
|
| 35 | + 5) Hard Coded GAC path |
| 36 | + Add-Type -Path “C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Smo\12.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Smo.dll” |
| 37 | +
|
| 38 | + List all Loaded Assemblies: |
| 39 | + [System.AppDomain]::CurrentDomain.GetAssemblies() | |
| 40 | + Where-Object Location | |
| 41 | + Sort-Object -property Location | |
| 42 | + Out-GridView |
| 43 | + |
| 44 | + Latest SMO Library Changed in 2019 to use Nuget - which is independent of SQL Server or SSMS version releases |
| 45 | + https://www.nuget.org/packages/Microsoft.SqlServer.SqlManagementObjects |
| 46 | + Find-Package -Name "Microsoft.SqlServer.SqlManagementObjects" -AllVersions -Source "https://www.nuget.org/api/v2" |
| 47 | + get-package | where-object {$_.name -match 'SqlManagementObjects'} |sort version | ogv |
| 48 | +
|
| 49 | + All NuGet Verbs |
| 50 | + https://docs.microsoft.com/en-us/nuget/reference/powershell-reference |
| 51 | +
|
| 52 | + |
| 53 | +.LINK |
| 54 | + |
| 55 | +#> |
| 56 | + |
| 57 | +function LoadSQLSMO(){ |
| 58 | + |
| 59 | + $old_ErrorActionPreference = $ErrorActionPreference |
| 60 | + $ErrorActionPreference = 'SilentlyContinue' |
| 61 | + |
| 62 | + |
| 63 | + # Try NuGet Package Version 160 |
| 64 | + try |
| 65 | + { |
| 66 | + Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SqlServer.SqlManagementObjects.160.2004021.0\lib\net462\Microsoft.SqlServer.Smo.dll" -ErrorAction Stop |
| 67 | + Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SqlServer.SqlManagementObjects.160.2004021.0\lib\net462\Microsoft.SqlServer.SmoExtended.dll" -ErrorAction Stop |
| 68 | + Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SqlServer.SqlManagementObjects.160.2004021.0\lib\net462\Microsoft.SqlServer.Management.XEvent.dll" -ErrorAction Stop |
| 69 | + Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SqlServer.SqlManagementObjects.160.2004021.0\lib\net462\Microsoft.SqlServer.Management.XEventEnum.dll" -ErrorAction Stop |
| 70 | + Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SqlServer.SqlManagementObjects.160.2004021.0\lib\net462\Microsoft.SqlServer.Management.Sdk.Sfc.dll" -ErrorAction Stop |
| 71 | + Write-Output "Using SMO Library [vNext] (160.2004021.0)" |
| 72 | + return |
| 73 | + } |
| 74 | + catch |
| 75 | + { |
| 76 | + } |
| 77 | + |
| 78 | + # Try NuGet Package Version 150 |
| 79 | + try |
| 80 | + { |
| 81 | + Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SqlServer.SqlManagementObjects.150.18208.0\lib\net45\Microsoft.SqlServer.Smo.dll" -ErrorAction Stop |
| 82 | + Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SqlServer.SqlManagementObjects.150.18208.0\lib\net45\Microsoft.SqlServer.SmoExtended.dll" -ErrorAction Stop |
| 83 | + Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SqlServer.SqlManagementObjects.150.18208.0\lib\net45\Microsoft.SqlServer.Management.XEvent.dll" -ErrorAction Stop |
| 84 | + Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SqlServer.SqlManagementObjects.150.18208.0\lib\net45\Microsoft.SqlServer.Management.XEventEnum.dll" -ErrorAction Stop |
| 85 | + Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SqlServer.SqlManagementObjects.150.18208.0\lib\net45\Microsoft.SqlServer.Management.Sdk.Sfc.dll" -ErrorAction Stop |
| 86 | + Write-Output "Using SMO Library [vNext] (150.18208.0)" |
| 87 | + return |
| 88 | + } |
| 89 | + catch |
| 90 | + { |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + # 2019 |
| 95 | + try |
| 96 | + { |
| 97 | + Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 98 | + Add-Type -AssemblyName "Microsoft.SqlServer.SMOExtended, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 99 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.XEvent, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 100 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.XEventEnum, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 101 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.Sdk.Sfc, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 102 | + Write-Output "Using SMO Library v15 (2019)" |
| 103 | + return |
| 104 | + } |
| 105 | + catch |
| 106 | + { |
| 107 | + } |
| 108 | + |
| 109 | + # 2017 |
| 110 | + try |
| 111 | + { |
| 112 | + Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 113 | + Add-Type -AssemblyName "Microsoft.SqlServer.SMOExtended, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 114 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.XEvent, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 115 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.XEventEnum, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 116 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.Sdk.Sfc, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 117 | + Write-Output "Using SMO Library v14 (2017)" |
| 118 | + return |
| 119 | + } |
| 120 | + catch |
| 121 | + { |
| 122 | + } |
| 123 | + |
| 124 | + # 2016 |
| 125 | + try |
| 126 | + { |
| 127 | + Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 128 | + Add-Type -AssemblyName "Microsoft.SqlServer.SMOExtended, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 129 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.XEvent, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 130 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.XEventEnum, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 131 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.Sdk.Sfc, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 132 | + Write-Output "Using SMO Library v13 (2016)" |
| 133 | + return |
| 134 | + } |
| 135 | + catch |
| 136 | + { |
| 137 | + } |
| 138 | + |
| 139 | + # 2014 |
| 140 | + try |
| 141 | + { |
| 142 | + Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 143 | + Add-Type -AssemblyName "Microsoft.SqlServer.SMOExtended, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 144 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.XEvent, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 145 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.XEventEnum, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 146 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.Sdk.Sfc, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 147 | + Write-Output "Using SMO Library v12 (2014)" |
| 148 | + return |
| 149 | + } |
| 150 | + catch |
| 151 | + { |
| 152 | + } |
| 153 | + |
| 154 | + # 2012 |
| 155 | + try |
| 156 | + { |
| 157 | + Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 158 | + Add-Type -AssemblyName "Microsoft.SqlServer.SMOExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 159 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.XEvent, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 160 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.XEventEnum, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 161 | + Add-Type -AssemblyName "Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 162 | + Write-Output "Using SMO Library v11 (2012)" |
| 163 | + return |
| 164 | + } |
| 165 | + catch |
| 166 | + { |
| 167 | + } |
| 168 | + |
| 169 | + try |
| 170 | + { |
| 171 | + Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 172 | + Add-Type -AssemblyName "Microsoft.SqlServer.SMOExtended, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 173 | + Write-Output "Using SMO Library 10 (2008)" |
| 174 | + } |
| 175 | + catch |
| 176 | + { |
| 177 | + } |
| 178 | + |
| 179 | + try |
| 180 | + { |
| 181 | + Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=9.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 182 | + Add-Type -AssemblyName "Microsoft.SqlServer.SMOExtended, Version=9.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -ErrorAction Stop |
| 183 | + #Write-Output "Using SMO Library 9 (2005)" |
| 184 | + } |
| 185 | + catch |
| 186 | + { |
| 187 | + } |
| 188 | + |
| 189 | + Write-output "No 2005+ SMO Libraries found on your Machine. Please load the latest version of SMO and try again" |
| 190 | + return |
| 191 | + |
| 192 | +} |
| 193 | + |
| 194 | +export-modulemember -function LoadSQLSMO |
0 commit comments