I am building some PowerShell cmdlets using the C#/.NET APIs and have a cmdlet that needs access to all the currently imported modules. If I run Get-Module in the terminal I can see 10+ modules loaded, but when my cmdlet runs:
using(var ps = PowerShell.Create(RunspaceMode.Current))
{
var modules = ps.AddCommand("Get-Module").Invoke<PSModuleInfo[]>();
/// ...
}
modules here is empty. Its almost like a scoping problem, but I thought RunspaceMode.Current would allow you to access that underlying runspace that was opened with you first open the pwsh shell.
I must be missing something or misunderstanding exactly how runspaces work.