Problem using module where it works on Dashboards but not inside Scripts

I have this weird situation where I can use a Module on any Dashboard but if I want to use the same type of code inside a Script, because I want this code to run as a scheduled job, it fails.

So I have this Module called ‘Transmission’.
It is located inside ‘Repository/Modules’ folder and after a restart of PSU it seems to be picked up.
Looking at

I can see the module is loaded.

Now on a Dashboard I can execute this code just fine and get the expected results:

New-UDDashboard -Title 'PowerShell Universal' -Content {
    Set-TransmissionCredentials -Host "http://host.docker.internal:8086/transmission/rpc" -User "" -Password ""
    [array]$torrents = Get-TransmissionTorrents
    foreach($torrent in $torrents) {
        New-UDHtml -Markup "$($torrent.Name)"
    }
}

But when I want to use this code (minus Dashboard of course) in a Script like so:

# It all starts with a single line of powershell code.

Set-TransmissionCredentials -Host "http://host.docker.internal:8086/transmission/rpc" -User "" -Password ""
[array]$torrents = Get-TransmissionTorrents
return $torrents.Count

I get this error:

[error] Failed to retrieve torrents with error: One or more errors occurred. (Could not load file or assembly ‘Flurl.Http, Version=3.2.4.0, Culture=neutral, PublicKeyToken=null’. Could not find or load a specific file. (0x80131621))

I can see that the module is not able to load a depending DLL but why?
If this works for a Dashboard then why not inside a Script?

Product: PowerShell Universal
Version: 3.9.2

I did some testing as I had a feeling the .Net version could be important here.

PSU 3 uses .Net 6
It seems that the Transmission module uses .Net 7 libraries/functionality.
The default ‘Environment’ is thus PS 7.2.7

So I set the Script to use ‘Environment’ PS 7.3.4 and then the script runs successfully!
However, I have not set PS 7.3.4 on any Dashboard and yet it works over there…

If someone can explain this to me so I can understand then that would be great!

Assembly load conflicts are a general problem with PowerShell and .NET. Typically, with .NET projects, assembly conflicts are detected and resolved at compile time. Because PS loads assemblies dynamically, you won’t run into issues until runtime, like you are seeing. This could happen if you mixed two modules that use the same library (for example, Flurl) in an PowerShell prompt.

The problem is that the Universal module uses Flurl as well and it’s using an older version of the library than the Transmission module. It is happening to load it before Transmission has a chance to and that’s causing the issue. For whatever reason, the dashboard is not loading Flurl and Transmission gets a chance to load it’s copy first and that’s why it works.

Assembly loading isn’t something we control directly and the .NET runtime is taking care of that. I think our solution here would be to upgrade Flurl in PSU so that it’s the same or newer than the one used in Transmission.

Hi Adam,

Clear, that is a logical explanation.

I think we can wait till PSU v4 as that will be based on .Net 7 and forces your hand in relation to Flurl anyway :slight_smile:

For now I point the environment for the scripts/dashboards that need this momdule and it’s libraries.

Thanks!

Regards,
Steven

1 Like