So I have embedded my own custom PSHost in my C#-Application. I would prefer using an already existing PowershellWay to etablish a Connection directly from a default PowershellConsole. Enter-PSSession isn’t able to directly connect to an running instance I think so I tried Enter-PSHostProcess but for some Reason I get this wierd Error when connecting to the Process via a default PS:
Enter-PSHostProcess : The term 'Measure-Object' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + Enter-PSHostProcess 18160 + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Microsoft.Power...tProcessCommand:EnterPSHostProcessCommand) [Enter-PSHostProcess], RemoteException + FullyQualifiedErrorId : EnterPSHostProcessCannotPushRunspace,Microsoft.PowerShell.Commands.EnterPSHostProcessCommand
Measure-Object is available in my application as well as in the default PS and I can not find anything fixing this error on google. Get-PSHostProcessInfo also detects the process as a PSHostProcess. For RemoteConnections from a diffrent PC I want to use Enter-PSSession and there Enter-PSHostProcess.
Here is a little SampleCode wich gave me the same Error:
using System; using System.Management.Automation; namespace StandardPowershellCoreTest { class Program { static void Main(string[] args) { PowerShell ps = PowerShell.Create(); while (true) { var pipe=ps.Runspace.CreatePipeline(); pipe.Commands.AddScript(Console.ReadLine()); pipe.Invoke(); } } } }
I mostly use the .net Core Version of the PowershellSDK but i guess it should work vice versa in the net framework version.
I hope there is a way like this I just couldn’t find. Otherwise I have no idea what to do.