Logging a PSSession

Hi all,

TL;DR - is there a way to pull the definition for the script or script block that Invoke-Command runs, in the context of the remote session?

I’m working on building a framework for delegated, constrained endpoints in our organization. Â The basic idea would be to provide granular access to end users who cannot be provided this access through the system itself.

I would like to head off any requests to log this activity. Â Presumably management will want to know who initiates commands if the commands run with a service account.

It seems like interactive sessions are easy enough to handle by adding logging functions to the end block of out-default (or all the commands necessary for interactive sessions), assuming I don’t whitelist anything that precludes the use of this.

I’m having trouble logging everything that takes place from Invoke-Command. Â Ideally, I could call some variable that stores the definition of the scriptblock or script being run in the session. Â Another option would be to add a logging function to the functions I define. Â This is a bit of a pain as $myinvocation.line does not appear to be populated in remote sessions.

Any suggestions? Â Has anyone else set up a logging system for delegated sessions? Â Your insight would be greatly appreciated!

I mostly use it with scheduled tasks and not invoked script blocks, but see if Start-Transcript might solve this for you. http://technet.microsoft.com/en-us/library/hh849687.aspx

I don’t believe this will work with Start-Transcript.

To clarify, the logging needs to take place within the remote session. Â My issue is that within the remote session, I’m having trouble identifying what code was called from the invoke-command that spawned the session.

You can’t easily access that from the remote session. Keep in mind that the remote computer isn’t running familiar old PowerShell.exe; it’s running wsmprovhost.exe, which doesn’t offer all the same features as the full console. So it’s not like you’ve done a Telnet into a remote copy of PowerShell. The product isn’t currently geared to provide logging like you’re looking for, although there are third-party proxy solutions (BeyondTrust makes one) that basically act as a middleman, and capture/log/authorize commands as you send them.

Now what oyu might be able to try (just thinking aloud, I’ve not tried this yet) is create a CUSTOM remoting endpoint that runs some kind of script or something on startup, BEFORE any incoming commands are run. That might be a key to getting hold of those commands, although I’m not quite sure how you’d do so. A DEFINITE option would be to program you own host, and register IT as an endpoint instead of wsmprovhost.exe. That’d take C# programming, but it would let you do whatever you wanted.

Hi Don,

Thanks for the confirmation! Â Haven’t had any luck setting this up in the startup script, will keep digging.


Note to Microsoft:

Providing the ability to run everything with delegated credentials is handy. Â Adding some sort of simple to use logging / auditing to this would make it valuable. Â As it is, I can tell you many management folk will be hesitant to open up this sort of functionality if we can’t see who did what, regardless of how locked down the endpoint is.


For those of you who stumble on this, assuming Microsoft does not address the situation, I have rudimentary logging up and running in an example startup script here.

It uses a few basic functions and messy logic. Â Basically:

  • Start-Log creates or appends to a log indicating a connection has been established.
  • Check-InvokeCommand checks if a a session is using invoke command (i.e. whether we can rely on history)
  • If using interactive remoting, create public proxies that call 'Write-Log' in the end block
  • Write-Log writes to a log file with the command run (based on history or a string), runas account for the session, who is connected to the session, date
  • Logic is included in functions we define that gets the command, parameters, and their values programatically, or using the history depending on whether running in interactive session. Â Could probably make a function out of this, got lazy : )
The end result is that I get logging from enter-pssession and invoke-command. Â My apologies for the messiness, just getting the ball rolling, will refine from here or hope for a solution from Microsoft or the community!