-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dbatools.library.psm1
191 lines (170 loc) · 6.85 KB
/
dbatools.library.psm1
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
function Get-DbatoolsLibraryPath {
[CmdletBinding()]
param()
if ($PSVersionTable.PSEdition -eq "Core") {
Join-Path -Path $PSScriptRoot -ChildPath core
} else {
Join-Path -Path $PSScriptRoot -ChildPath desktop
}
}
$script:libraryroot = Get-DbatoolsLibraryPath
if ($PSVersionTable.PSEdition -ne "Core") {
$dir = [System.IO.Path]::Combine($script:libraryroot, "lib")
$dir = ("$dir\").Replace('\', '\\')
if (-not ("Redirector" -as [type])) {
$source = @"
using System;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
public class Redirector
{
public Redirector()
{
this.EventHandler = new ResolveEventHandler(AssemblyResolve);
}
public readonly ResolveEventHandler EventHandler;
protected Assembly AssemblyResolve(object sender, ResolveEventArgs e)
{
string[] dlls = {
"System.Memory",
"System.Runtime.CompilerServices.Unsafe",
"System.Resources.Extensions",
"Microsoft.SqlServer.ConnectionInfo",
"Microsoft.SqlServer.Smo",
"Microsoft.Identity.Client",
"System.Diagnostics.DiagnosticSource",
"Microsoft.IdentityModel.Abstractions",
"Microsoft.Data.SqlClient",
"Microsoft.SqlServer.Types",
"System.Configuration.ConfigurationManager",
"Microsoft.SqlServer.Management.Sdk.Sfc",
"Microsoft.SqlServer.Management.IntegrationServices",
"Microsoft.SqlServer.Replication",
"Microsoft.SqlServer.Rmo",
"System.Private.CoreLib"
};
var name = new AssemblyName(e.Name);
var assemblyName = name.Name.ToString();
foreach (string dll in dlls)
{
if (assemblyName == dll)
{
string filelocation = "$dir" + dll + ".dll";
//Console.WriteLine(filelocation);
return Assembly.LoadFrom(filelocation);
}
}
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
// maybe this needs to change?
var info = assembly.GetName();
if (info.FullName == e.Name) {
return assembly;
}
}
return null;
}
}
"@
$null = Add-Type -TypeDefinition $source
}
try {
$redirector = New-Object Redirector
[System.AppDomain]::CurrentDomain.add_AssemblyResolve($redirector.EventHandler)
} catch {
# unsure
}
}
if ($IsWindows -and $PSVersionTable.PSEdition -eq "Core") {
$sqlclient = [System.IO.Path]::Combine($script:libraryroot, "lib", "win-sqlclient", "Microsoft.Data.SqlClient.dll")
} else {
$sqlclient = [System.IO.Path]::Combine($script:libraryroot, "lib", "Microsoft.Data.SqlClient.dll")
}
try {
Import-Module $sqlclient
} catch {
throw "Couldn't import $sqlclient | $PSItem"
}
if ($PSVersionTable.PSEdition -ne "Core") {
[System.AppDomain]::CurrentDomain.remove_AssemblyResolve($onAssemblyResolveEventHandler)
}
if ($PSVersionTable.PSEdition -eq "Core") {
$names = @(
'Microsoft.SqlServer.Server',
'Microsoft.SqlServer.Dac',
'Microsoft.SqlServer.Smo',
'Microsoft.SqlServer.SmoExtended',
'Microsoft.SqlServer.SqlWmiManagement',
'Microsoft.SqlServer.WmiEnum',
'Microsoft.SqlServer.Management.RegisteredServers',
'Microsoft.SqlServer.Management.Collector',
'Microsoft.SqlServer.Management.XEvent',
'Microsoft.SqlServer.Management.XEventDbScoped',
'Microsoft.SqlServer.XEvent.XELite',
'../third-party/LumenWorks/LumenWorks.Framework.IO'
'Azure.Core',
'Azure.Identity',
'Microsoft.IdentityModel.Abstractions'
)
} else {
$names = @(
'Microsoft.SqlServer.Dac',
'Microsoft.SqlServer.Smo',
'Microsoft.SqlServer.SmoExtended',
'Microsoft.SqlServer.SqlWmiManagement',
'Microsoft.SqlServer.WmiEnum',
'Microsoft.SqlServer.Management.RegisteredServers',
'Microsoft.SqlServer.Management.IntegrationServices',
'Microsoft.SqlServer.Management.Collector',
'Microsoft.SqlServer.Management.XEvent',
'Microsoft.SqlServer.Management.XEventDbScoped',
'Microsoft.SqlServer.XEvent.XELite',
'Azure.Core',
'Azure.Identity',
'Microsoft.IdentityModel.Abstractions',
'Microsoft.Data.SqlClient',
'../third-party/LumenWorks/LumenWorks.Framework.IO'
)
}
if ($Env:SMODefaultModuleName) {
# then it's DSC, load other required assemblies
$names += "Microsoft.AnalysisServices.Core"
$names += "Microsoft.AnalysisServices"
$names += "Microsoft.AnalysisServices.Tabular"
$names += "Microsoft.AnalysisServices.Tabular.Json"
}
# XEvent stuff kills CI/CD
if ($PSVersionTable.OS -match "ARM64") {
$names = $names | Where-Object { $PSItem -notmatch "XE" }
}
#endregion Names
# this takes 10ms
$assemblies = [System.AppDomain]::CurrentDomain.GetAssemblies()
try {
$null = Import-Module ([IO.Path]::Combine($script:libraryroot, "third-party", "bogus", "bogus.dll"))
} catch {
Write-Error "Could not import $assemblyPath : $($_ | Out-String)"
}
foreach ($name in $names) {
if ($name.StartsWith("win-sqlclient\") -and ($isLinux -or $IsMacOS)) {
$name = $name.Replace("win-sqlclient\", "")
if ($IsMacOS -and $name -in "Azure.Core", "Azure.Identity", "System.Security.SecureString") {
$name = "mac\$name"
}
}
$x64only = 'Microsoft.SqlServer.Replication', 'Microsoft.SqlServer.XEvent.Linq', 'Microsoft.SqlServer.BatchParser', 'Microsoft.SqlServer.Rmo', 'Microsoft.SqlServer.BatchParserClient'
if ($name -in $x64only -and $env:PROCESSOR_ARCHITECTURE -eq "x86") {
Write-Verbose -Message "Skipping $name. x86 not supported for this library."
continue
}
$assemblyPath = [IO.Path]::Combine($script:libraryroot, "lib", "$name.dll")
$assemblyfullname = $assemblies.FullName | Out-String
if (-not ($assemblyfullname.Contains("$name,".Replace("win-sqlclient\", "")))) {
$null = try {
$null = Import-Module $assemblyPath
} catch {
Write-Error "Could not import $assemblyPath : $($_ | Out-String)"
}
}
}