|
11 | 11 | using System.Runtime.InteropServices;
|
12 | 12 |
|
13 | 13 | using SMA = System.Management.Automation;
|
| 14 | +using System.Management.Automation; |
| 15 | +using System.Management.Automation.Runspaces; |
14 | 16 |
|
15 | 17 | #if CoreCLR
|
16 | 18 | using System.Runtime.Loader;
|
@@ -255,12 +257,9 @@ private void CheckNetFxVersion()
|
255 | 257 | private void CheckLanguageMode()
|
256 | 258 | {
|
257 | 259 | _logger.Log(PsesLogLevel.Diagnostic, "Checking that PSES is running in FullLanguage mode");
|
258 |
| - using (var pwsh = SMA.PowerShell.Create()) |
| 260 | + if (Runspace.DefaultRunspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage) |
259 | 261 | {
|
260 |
| - if (pwsh.Runspace.SessionStateProxy.LanguageMode != SMA.PSLanguageMode.FullLanguage) |
261 |
| - { |
262 |
| - throw new InvalidOperationException("Cannot start PowerShell Editor Services in Constrained Language Mode"); |
263 |
| - } |
| 262 | + throw new InvalidOperationException("Cannot start PowerShell Editor Services in Constrained Language Mode"); |
264 | 263 | }
|
265 | 264 | }
|
266 | 265 |
|
@@ -339,23 +338,20 @@ private string GetPSOutputEncoding()
|
339 | 338 |
|
340 | 339 | private void LogPowerShellDetails()
|
341 | 340 | {
|
342 |
| - using (var pwsh = SMA.PowerShell.Create(SMA.RunspaceMode.CurrentRunspace)) |
343 |
| - { |
344 |
| - string psVersion = pwsh.AddScript("$PSVersionTable.PSVersion").Invoke()[0].ToString(); |
| 341 | + PSLanguageMode languageMode = Runspace.DefaultRunspace.SessionStateProxy.LanguageMode; |
345 | 342 |
|
346 |
| - _logger.Log(PsesLogLevel.Verbose, $@" |
| 343 | + _logger.Log(PsesLogLevel.Verbose, $@" |
347 | 344 | == PowerShell Details ==
|
348 |
| -- PowerShell version: {psVersion} |
349 |
| -- Language mode: {pwsh.Runspace.SessionStateProxy.LanguageMode} |
| 345 | +- PowerShell version: {GetPSVersion()} |
| 346 | +- Language mode: {languageMode} |
350 | 347 | ");
|
351 |
| - } |
352 | 348 | }
|
353 | 349 |
|
354 | 350 | private void LogOperatingSystemDetails()
|
355 | 351 | {
|
356 | 352 | _logger.Log(PsesLogLevel.Verbose, $@"
|
357 | 353 | == Environment Details ==
|
358 |
| - - OS description: {RuntimeInformation.OSDescription} |
| 354 | + - OS description: {RuntimeInformation.OSDescription} |
359 | 355 | - OS architecture: {GetOSArchitecture()}
|
360 | 356 | - Process bitness: {(Environment.Is64BitProcess ? "64" : "32")}
|
361 | 357 | ");
|
@@ -406,5 +402,18 @@ private void ValidateConfiguration()
|
406 | 402 | throw new ArgumentNullException(nameof(_hostConfig.PSHost));
|
407 | 403 | }
|
408 | 404 | }
|
| 405 | + |
| 406 | + private static object GetPSVersion() |
| 407 | + { |
| 408 | + // In order to read the $PSVersionTable variable, |
| 409 | + // we are forced to create a new runspace to avoid concurrency issues, |
| 410 | + // which is expensive. |
| 411 | + // Rather than do that, we instead go straight to the source, |
| 412 | + // which is a static property, internal in WinPS and public in PS 6+ |
| 413 | + return typeof(PSObject).Assembly |
| 414 | + .GetType("System.Management.Automation.PSVersionInfo") |
| 415 | + .GetMethod("get_PSVersion", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) |
| 416 | + .Invoke(null, Array.Empty<object>()); |
| 417 | + } |
409 | 418 | }
|
410 | 419 | }
|
0 commit comments