Who is Running Your PowerShell Script?

I’ve often talked about the benefit of including Verbose output in your PowerShell scripts and functions from the very beginning. This is especially helpful when someone else is running your command but encounters a problem. You can have them start a transcript, run your command with –Verbose, close the transcript and send it to you. if you’ve written informative Verbose messaging you should be able to figure out the problem. Part of the information might include metadata about the person running the command and their environment. To simply things, I’ve created an easy to use function called Get-PSWho.

The function writes a custom object to the pipeline with information about the user, their computer and their PowerShell environment. One of the things that might be helpful is knowing where the command is being run as not all PowerShell hosts may support your code.

image

 

image

image

The other design decision I made was to deviate slightly from the recommended practice of only writing one type of object to the pipeline and not re-inventing the wheel. One of the reasons I wrote this was to make it easy to incorporate into your functions and scripts. You can either embed the function or make sure it is distributed with your commands so that you can include the output in a Write-Verbose line. But, you have to make turn the output into a string.

Since you might need a string anyway, I decided to include it as an option.

image

Or you could just as easily pipe the string version to Write-Host. The point is that I thought about how this command might be used and added what I thought would be a useful parameter. Again, I am a bit torn on whether this is the right design decision, but you may simple opt to take my code and paste it into your function directly.

You can find the code on GitHub.

Because I’m using Get-CimInstance, this command won’t run in PowerShell Core on non-Windows systems. Although I can probably take that into account and code around it.

If you encounter problems with it or have suggestions, please post an issue on the Gist page.

One thought on “Who is Running Your PowerShell Script?

Leave a Reply

Your email address will not be published. Required fields are marked *