I have a .NET 5 that uses the plugin capability, as specified here.
I've created a plugin that uses PowerShell (Microsoft.PowerShell.SDK nuget package (v7.1.4)).
Inside the plugin, I'm executing this code:
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript("Get-Disk");
ps.Invoke();
Console.WriteLine(string.Join(", ", ps.Streams.Error));
}
The output I'm getting is:
The 'Get-Disk' command was found in the module 'Storage', but the module could not be loaded. For more information, run 'Import-Module Storage'.
So, I tried executing
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript("Import-Module Storage; Get-Disk");
ps.Invoke();
Console.WriteLine(string.Join(", ", ps.Streams.Error));
}
And this is what I get:
One or more errors occurred. (The getter method should be public, not void, static, and have one parameter of the type PSObject.);The 'Get-Disk' command was found in the module 'Storage', but the module could not be loaded. For more information, run 'Import-Module Storage'.
What am I doing wrong? This code works perfectly if I invoke the code outside the loaded plugin.
EDIT
I've created a minimal reproducible example of the problem, you can see it here. The code is super simple. Be sure to compile every project before running it. Otherwise, the plugin won't be compiled and won't load from the main app.