Not too long ago I posted a PowerShell function that could provide detail abut the PowerShell engine driving your current PowerShell session. I like having a function that writes an object to the pipeline, can take parameters and offer help documentation. But there’s an alternative approach you could also take.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Most of the information is already in $PSVersiontable. So why not use that as a vehicle? This variable is a simple hashtable that is constructed every time you start a PowerShell session. This means you can add key/value pairs to it with the Add() method.
$PSVersionTable.add("Path",(Get-Process -id $pid).Path) $PSVersionTable.add("Host",$host.name) $PSVersionTable.add("Culture",$host.CurrentCulture.DisplayName) $PSVersionTable.add("Installed",(get-item $PSVersionTable.path).CreationTime) $PSVersionTable.add("LanguageMode",$host.Runspace.LanguageMode) $PSVersionTable.add("Computername",$(hostname))
Remember that this will only change the variable for the current PowerShell session. If you wanted to always have this extended information, you would need to add these lines to your PowerShell profile script or create a simple function. If you go that route, you’ll want to include some testing and error handling because you can’t add a value that already exists. When you are done you get results like these:
$PSVersiontable is not an ordered hashtable so you can't predict the display order, but it does offer a quick and dirty way to store additional information you might want to reference.
This was awesome but I ran into an issue when trying to remote –
This error would occur
WriteErrorStream : True
Exception : System.Management.Automation.Remoting.PSRemotingDataStructureException: An error has occurred
which Windows PowerShell cannot handle. A remote session might have ended. —>
System.ArgumentException: The type of the value System.Management.Automation.PSLanguageMode is
not valid. The PSPrimitiveDictionary class accepts only values of types that are fully
serializable over Windows PowerShell remoting. See the Help topic about_Remoting for a list of
fully-serializable types.
Once I removed “$PSVersionTable.add(“LanguageMode”,$host.Runspace.LanguageMode)” from my profile, all worked as expected.
Still haven’t found out the “why.”
I can’t figure out how you were using this code with a remote session so I’m not sure what is going on.