Repository navigation
Is it possible to configure the Runspace for Connections into my Process with Enter-PsHostProcess? #25583
|
I use the PowerShell SDK to provide a powerfull commandline specific to my c#-software. For this I configure the runspace to my needs (e.g. I add and remove cmdlets to the InitialSessionState and add Variables to the runspace that implement PSVariable to make them readonly in the Runspace but changable on the side of my software environment). Now I want to connect to Runsapces in this process from external PowerShell-Processes. The easiest solution probably being a NamedPipeConnection with Enter-PsHostProcess to connect to my process. The connection part itself works fine but I have not found a way to configure the InitialSessionState/Runspace for connections made this way into my process. I can not provide a Session-ConfigurationFile with a NamedPipeConnection and also did not find a way to host a custom namedpipe listener that would allow me to do just that. I even tried creating my own NamedPipe and connect to that pipe but then I would have to be able to use the PSRemotingProtocol wich seems to be internal and complicated to do. Does anyone know a way to do something like this? I would really like to connect via PowerShell Remoting so I can use a PowerShell-Process as an external consolehost as well as the PowerShell SDK to send commands to my software from other tools. |
Replies: 2 comments 1 reply
|
Hi! 👋 From what I understand, Could you expose a dedicated runspace or a supported remoting endpoint from your C# application, with the required commands and variables configured when the runspace is created? That may be easier to maintain than implementing the remoting protocol over a custom named pipe. I'd also check the PowerShell SDK hosting and remoting APIs for the specific PowerShell version you're using. Hope this helps! |
|
Hi! From what you describe, the main limitation seems to be the distinction between attaching to an existing PowerShell host process and creating a new runspace with a custom
There is a public SDK type worth looking into:
It exposes For your scenario, I would consider two approaches: 1. A custom IPC bridge into your existing runspaces Keep runspace creation and configuration inside your C# application, using Expose an authenticated IPC endpoint that forwards requests to your application, where commands are dispatched through the existing PowerShell SDK infrastructure. This would let you preserve your custom cmdlets, variables, and application state. The trade-off is that this is not automatically compatible with 2. A custom PowerShell remoting endpoint If native PowerShell remoting compatibility is essential, you would need a host or endpoint implementation that creates sessions using your application's configuration. Windows WSMan session configurations support custom session environments, but they are not directly applicable to the named-pipe connection established by One additional consideration: if your So, to answer your question directly: I don't know of a supported way to provide a custom References:
|
Hi! From what you describe, the main limitation seems to be the distinction between attaching to an existing PowerShell host process and creating a new runspace with a custom
InitialSessionState.Enter-PSHostProcessprimarily provides process attachment and runspace debugging capabilities. Although it supports connecting through a named pipe, it does not expose an option to supply a customInitialSessionStatefor the runspace created for that connection.There is a public SDK type worth looking into:
System.Management.Automation.Remoting.RemoteSessionNamedPipeServerIt exposes
CreateCustomNamedPipeServer(string). However, the documented API does not appear to provide a way to inject a cus…