mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Retrieve ProductVersion using informational version attribute in AmsiUtils.Init() (#15527)
This commit is contained in:
@@ -78,7 +78,7 @@ namespace System.Management.Automation
|
||||
s_psVersionTable = new PSVersionHashTable(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
Assembly currentAssembly = typeof(PSVersionInfo).Assembly;
|
||||
string productVersion = currentAssembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
|
||||
ProductVersion = currentAssembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
|
||||
|
||||
// Get 'GitCommitId' and 'PSVersion' from the 'productVersion' assembly attribute.
|
||||
//
|
||||
@@ -93,11 +93,11 @@ namespace System.Management.Automation
|
||||
// productVersion = '6.0.0 SHA: f1ec9...' convert to GitCommitId = 'v6.0.0'
|
||||
// PSVersion = '6.0.0'
|
||||
string rawGitCommitId;
|
||||
string mainVersion = productVersion.Substring(0, productVersion.IndexOf(' '));
|
||||
string mainVersion = ProductVersion.Substring(0, ProductVersion.IndexOf(' '));
|
||||
|
||||
if (productVersion.Contains(" Commits: "))
|
||||
if (ProductVersion.Contains(" Commits: "))
|
||||
{
|
||||
rawGitCommitId = productVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g");
|
||||
rawGitCommitId = ProductVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -182,6 +182,8 @@ namespace System.Management.Automation
|
||||
}
|
||||
}
|
||||
|
||||
internal static string ProductVersion { get; }
|
||||
|
||||
internal static string GitCommitId
|
||||
{
|
||||
get
|
||||
|
||||
@@ -1336,39 +1336,27 @@ namespace System.Management.Automation
|
||||
|
||||
internal static class AmsiUtils
|
||||
{
|
||||
private static string GetProcessHostName(string processName)
|
||||
{
|
||||
return string.Concat("PowerShell_", processName, ".exe_0.0.0.0");
|
||||
}
|
||||
|
||||
internal static int Init()
|
||||
{
|
||||
Diagnostics.Assert(s_amsiContext == IntPtr.Zero, "Init should be called just once");
|
||||
|
||||
lock (s_amsiLockObject)
|
||||
{
|
||||
Process currentProcess = Process.GetCurrentProcess();
|
||||
string hostname;
|
||||
string appName;
|
||||
try
|
||||
{
|
||||
hostname = string.Concat("PowerShell_", Environment.ProcessPath, "_",
|
||||
currentProcess.MainModule.FileVersionInfo.ProductVersion);
|
||||
appName = string.Concat("PowerShell_", Environment.ProcessPath, "_", PSVersionInfo.ProductVersion);
|
||||
}
|
||||
catch (ComponentModel.Win32Exception)
|
||||
catch (Exception)
|
||||
{
|
||||
// This exception can be thrown during thread impersonation (Access Denied for process module access).
|
||||
hostname = GetProcessHostName(currentProcess.ProcessName);
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
// This exception can occur if the file is renamed or moved to some other folder
|
||||
// (This has occurred during Exchange set up).
|
||||
hostname = GetProcessHostName(currentProcess.ProcessName);
|
||||
// Fall back to 'Process.ProcessName' in case 'Environment.ProcessPath' throws exception.
|
||||
Process currentProcess = Process.GetCurrentProcess();
|
||||
appName = string.Concat("PowerShell_", currentProcess.ProcessName, ".exe_", PSVersionInfo.ProductVersion);
|
||||
}
|
||||
|
||||
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
|
||||
|
||||
var hr = AmsiNativeMethods.AmsiInitialize(hostname, ref s_amsiContext);
|
||||
var hr = AmsiNativeMethods.AmsiInitialize(appName, ref s_amsiContext);
|
||||
if (!Utils.Succeeded(hr))
|
||||
{
|
||||
s_amsiInitFailed = true;
|
||||
|
||||
@@ -67,7 +67,6 @@ Describe "Validate start of console host" -Tag CI {
|
||||
if ($IsWindows) {
|
||||
$allowedAssemblies += @(
|
||||
'Microsoft.PowerShell.CoreCLR.Eventing.dll'
|
||||
'System.Diagnostics.FileVersionInfo.dll'
|
||||
'System.DirectoryServices.dll'
|
||||
'System.Management.dll'
|
||||
'System.Security.Claims.dll'
|
||||
|
||||
Reference in New Issue
Block a user