Skip to content

Commit 0a07de2

Browse files
Generalize assembly loading logic for known DLLs
Replaces special handling for System.Management.Automation with a generalized approach for all known DLLs. Now, if an assembly with the same name is already loaded, it is returned and a warning is logged if the requested version differs from the loaded version.
1 parent 80f5b9c commit 0a07de2

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

dbatools.library.psm1

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,20 @@ if ($PSVersionTable.PSEdition -ne "Core") {
6363
{
6464
if (assemblyName == dll)
6565
{
66-
// Special handling for System.Management.Automation:
67-
// Always return the already loaded assembly, regardless of requested version.
68-
if (assemblyName == "System.Management.Automation")
66+
// Generalized handling for all known DLLs:
67+
// If an assembly with the same name is already loaded, return it (warn if version differs).
68+
var loaded = AppDomain.CurrentDomain.GetAssemblies()
69+
.FirstOrDefault(a => a.GetName().Name == assemblyName);
70+
if (loaded != null)
6971
{
70-
var loaded = AppDomain.CurrentDomain.GetAssemblies()
71-
.FirstOrDefault(a => a.GetName().Name == "System.Management.Automation");
72-
if (loaded != null)
72+
var requestedVersion = name.Version;
73+
var loadedVersion = loaded.GetName().Version;
74+
if (requestedVersion != null && loadedVersion != null && requestedVersion != loadedVersion)
7375
{
74-
var requestedVersion = name.Version;
75-
var loadedVersion = loaded.GetName().Version;
76-
if (requestedVersion != null && loadedVersion != null && requestedVersion != loadedVersion)
77-
{
78-
// Log a warning if the requested version does not match the loaded version.
79-
System.Diagnostics.Trace.TraceWarning(
80-
$"[dbatools] Requested System.Management.Automation version {requestedVersion}, but loaded version is {loadedVersion}. Using loaded version.");
81-
}
82-
return loaded;
76+
System.Diagnostics.Trace.TraceWarning(
77+
$"[dbatools] Requested {assemblyName} version {requestedVersion}, but loaded version is {loadedVersion}. Using loaded version. This may cause compatibility issues if APIs differ.");
8378
}
84-
// If not found, fall through to normal logic (should not happen in PowerShell host)
79+
return loaded;
8580
}
8681
string filelocation = "$dir" + dll + ".dll";
8782
//Console.WriteLine(filelocation);

0 commit comments

Comments
 (0)