jborean93 / pypsrp

PowerShell Remoting Protocol for Python
MIT License
324 stars 49 forks source link

Exit code for PowerShell session #146

Closed malthe closed 2 years ago

malthe commented 2 years ago

In order to retrieve the exit code for a PowerShell session (in which the exit command is used), must one instantiate a PSHost instance and provide to the runspace pool?

It seems like PowerShell.poll_invoke and/or PowerShell.end_invoke should also return this exit code, or at least it should be accessible in an easy way.

jborean93 commented 2 years ago

It's just not possible to get the exit code without first providing a host. The PSRP messages exchanged do not return this value in any shape or form except through the SetShouldExit host call so in order to retrieve it you need to provide a host yourself and implement this method. Technically WSMan itself has an ExitCode field in the ReceiveResponse of the final pipeline message but it does not reflect what was used with exit in the running pipeline (it is always 0). Other transports have no similar transport specific field for this so even if the WSMan one worked you couldn't implement it in a transport agnostic way.