Matt H
Matt H

Reputation: 7389

Why does calling pwsh lead to an `Assembly with same name is already loaded` error?

I have a script that is run using legacy Windows Powershell 5 and is not compatible with Powershell Core (v.7+). I can't migrate this large script at this time. This script delegates out to some child scripts, and I'd like to be able to use Powershell 7+ features in some of them. My initial solution was to use:

    pwsh -WorkingDirectory . -File .\Configure.ps1

However, Configure.ps1 imports a module and it errors out on the import with:

    Import-Module: path\to\script\Configure.ps1:3
Line |
   3 |  Import-Module CustomModule
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Assembly with same name is already loaded

The CustomModule has IISAdministration as a required dependency. Anyway, it turned out the issue was reproducible just calling the same code from a Powershell 7.3.3 session and wasn't related to the old Powershell version at all. If I invoke the script directly:

   .\Configure.ps1

It runs just fine.

Why does this happen, and is there some way to work around this assembly conflict?

Upvotes: 2

Views: 358

Answers (1)

Matt H
Matt H

Reputation: 7389

So I still haven't figured out the WHY, but I did come up with a work-around with some help from the comments. Using & to invoke pwsh and adding -NoProfile worked:

    & pwsh -NoProfile -WorkingDirectory . -File .\Configure.ps1

Upvotes: 1

Related Questions