public async Task <PowerShellVersion> Handle(GetVersionParams request, CancellationToken cancellationToken) { PowerShellProcessArchitecture architecture = PowerShellProcessArchitecture.Unknown; // This should be changed to using a .NET call sometime in the future... but it's just for logging purposes. string arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"); if (arch != null) { if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase)) { architecture = PowerShellProcessArchitecture.X64; } else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase)) { architecture = PowerShellProcessArchitecture.X86; } } return(new PowerShellVersion { Version = VersionUtils.PSVersionString, Edition = VersionUtils.PSEdition, DisplayVersion = VersionUtils.PSVersion.ToString(2), Architecture = architecture.ToString() }); }
/// <summary> /// Creates an instance of the PowerShellVersionDetails class. /// </summary> /// <param name="version">The version of the PowerShell runtime.</param> /// <param name="versionString">A string representation of the PowerShell version.</param> /// <param name="editionString">The string representation of the PowerShell edition.</param> /// <param name="architecture">The processor architecture.</param> public PowerShellVersionDetails( Version version, string versionString, string editionString, PowerShellProcessArchitecture architecture) { this.Version = version; this.VersionString = versionString; this.Edition = editionString; this.Architecture = architecture; }
/// <summary> /// Gets the PowerShell version details for the given runspace. /// </summary> /// <param name="logger">An ILogger implementation used for writing log messages.</param> /// <param name="pwsh">The PowerShell instance for which to to get the version.</param> /// <returns>A new PowerShellVersionDetails instance.</returns> public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerShell pwsh) { Version powerShellVersion = new(5, 0); string versionString = null; string powerShellEdition = "Desktop"; PowerShellProcessArchitecture architecture = PowerShellProcessArchitecture.Unknown; try { Hashtable psVersionTable = pwsh .AddScript("$PSVersionTable", useLocalScope: true) .InvokeAndClear<Hashtable>() .FirstOrDefault(); if (psVersionTable != null) { if (psVersionTable["PSEdition"] is string edition) { powerShellEdition = edition; } // The PSVersion value will either be of Version or SemanticVersion. // In the former case, take the value directly. In the latter case, // generate a Version from its string representation. object version = psVersionTable["PSVersion"]; if (version is Version version2) { powerShellVersion = version2; } else if (version != null) { // Expected version string format is 6.0.0-alpha so build a simpler version from that powerShellVersion = new Version(version.ToString().Split('-')[0]); } versionString = psVersionTable["GitCommitId"] is string gitCommitId ? gitCommitId : powerShellVersion.ToString(); PSCommand procArchCommand = new PSCommand().AddScript("$env:PROCESSOR_ARCHITECTURE", useLocalScope: true); string arch = pwsh .AddScript("$env:PROCESSOR_ARCHITECTURE", useLocalScope: true) .InvokeAndClear<string>() .FirstOrDefault(); if (arch != null) { if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase)) { architecture = PowerShellProcessArchitecture.X64; } else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase)) { architecture = PowerShellProcessArchitecture.X86; } } } } catch (Exception ex) { logger.LogWarning( "Failed to look up PowerShell version, defaulting to version 5.\r\n\r\n" + ex.ToString()); } return new PowerShellVersionDetails( powerShellVersion, versionString, powerShellEdition, architecture); }