Skip to content
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
using System.Runtime.InteropServices;

using SMA = System.Management.Automation;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for the $outputencoding log

using System.Management.Automation;
using System.Collections;
using System.Management.Automation.Runspaces;

#if CoreCLR
using System.Runtime.Loader;
Expand Down Expand Up @@ -339,23 +342,20 @@ private string GetPSOutputEncoding()

private void LogPowerShellDetails()
{
using (var pwsh = SMA.PowerShell.Create(SMA.RunspaceMode.CurrentRunspace))
{
string psVersion = pwsh.AddScript("$PSVersionTable.PSVersion").Invoke()[0].ToString();
PSLanguageMode languageMode = Runspace.DefaultRunspace.SessionStateProxy.LanguageMode;

_logger.Log(PsesLogLevel.Verbose, $@"
_logger.Log(PsesLogLevel.Verbose, $@"
== PowerShell Details ==
- PowerShell version: {psVersion}
- Language mode: {pwsh.Runspace.SessionStateProxy.LanguageMode}
- PowerShell version: {GetPSVersion()}
- Language mode: {languageMode}
");
}
}

private void LogOperatingSystemDetails()
{
_logger.Log(PsesLogLevel.Verbose, $@"
== Environment Details ==
- OS description: {RuntimeInformation.OSDescription}
- OS description: {RuntimeInformation.OSDescription}
- OS architecture: {GetOSArchitecture()}
- Process bitness: {(Environment.Is64BitProcess ? "64" : "32")}
");
Expand Down Expand Up @@ -406,5 +406,18 @@ private void ValidateConfiguration()
throw new ArgumentNullException(nameof(_hostConfig.PSHost));
}
}

private static object GetPSVersion()
{
// In order to read the $PSVersionTable variable,
// we are forced to create a new runspace to avoid concurrency issues,
// which is expensive.
// Rather than do that, we instead go straight to the source,
// which is a static property, internal in WinPS and public in PS 6+
return typeof(PSObject).Assembly
.GetType("System.Management.Automation.PSVersionInfo")
.GetMethod("get_PSVersion", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
.Invoke(null, Array.Empty<object>());
}
}
}