I am trying to fetch the printers associated with my computer inside an ASP.NET Core 5 app using the System.Management.Automation
and Microsoft.PowerShell.SDK
nuget packages.
PowerShell ps = PowerShell.Create();
ps.AddScript("Get-Printer");
ps.Invoke();
However, when I try this, I get the following error
The 'Get-Printer' command was found in the module 'PrintManagement', but the module could not be loaded. For more information, run 'Import-Module PrintManagement'
I tried Import-Module PrintManagement
, but nothing is being imported and I get no messages. I also tried setting the execution policy to unrestricted, but it still didn't work.
I tried the code in a .NET 5 application console and it worked perfectly fine so I guess the issue narrows down to ASP.NET Core 5.
Any help would be greatly appreciated!
I managed to make it work by putting the PrintManagement module folder directly in \bin\Debug\net5.0\runtimes\win\lib\net5.0\Modules
in my project files.