Skip to content

Commit 80f5b9c

Browse files
Add special handling for System.Management.Automation assembly
Ensures that the already loaded System.Management.Automation assembly is always returned, regardless of requested version. Logs a warning if the requested version differs from the loaded version to improve compatibility and debugging.
1 parent dd6a44b commit 80f5b9c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

dbatools.library.psm1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ 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")
69+
{
70+
var loaded = AppDomain.CurrentDomain.GetAssemblies()
71+
.FirstOrDefault(a => a.GetName().Name == "System.Management.Automation");
72+
if (loaded != null)
73+
{
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;
83+
}
84+
// If not found, fall through to normal logic (should not happen in PowerShell host)
85+
}
6686
string filelocation = "$dir" + dll + ".dll";
6787
//Console.WriteLine(filelocation);
6888
return Assembly.LoadFrom(filelocation);

0 commit comments

Comments
 (0)