Adding this to your code:
Collection<PSObject> PSOutput;
using (PowerShell PowerShellInstance = PowerShell.Create()) {
PowerShellInstance.AddScript("Show-Command -name Get-Content -PassThru");
PSOutput = PowerShellInstance.Invoke();
if (!PowerShellInstance.HadErrors ) {
foreach (var item in PSOutput) {
Console.WriteLine(item);
}
} else {
foreach (var item in PowerShellInstance.Streams.Error) {
Console.WriteLine(item);
Console.WriteLine(item.Exception.StackTrace);
}
}
}
returns this output
Object reference not set to an instance of an object.
at Microsoft.PowerShell.Commands.ShowCommandInternal.ShowCommandHelper.GetHostWindow(PSCmdlet cmdlet)
at Microsoft.PowerShell.Commands.ShowCommandInternal.ShowCommandHelper.CallShowDialog(PSCmdlet cmdlet)
at Microsoft.PowerShell.Commands.ShowCommandInternal.ShowCommandHelper.ShowCommandWindow(PSCmdlet cmdlet, Object commandViewModelObj, Double windowWidth, Double windowHeight, Boolean passThrough)
Which is pretty much is indicating that there's no HostWindow object, which is to be expected using the PS Management Objects.
Using ILSpy to examine the code in Microsoft.PowerShell.Commands.Utility.dll
it appears that the Show-Command
cmdlet invokes a ShowCommandProxy
object that is looking for a System.Management.Automation.Internal GraphicalHostReflectionWrapper
object, which is not set.
--Update--
I'd take a look at this question: Custom PSHostUserInterface is ignored by Runspace which show that you're going to have to implement your own PSHost (https://msdn.microsoft.com/en-us/library/system.management.automation.host.pshost(v=vs.85).aspx) and implement your own PSHostUserInterface
and finally your own PSHostRawUserInterface
to handle all the I/O.
MS has an example here https://msdn.microsoft.com/en-us/library/ee706551(v=vs.85).aspx that does a custom host implementation.