diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index 80b01fcaa3b..0ac97156103 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -650,7 +650,10 @@ protected override void ProcessRecord() //if fileversion of each process is to be displayed try { - WriteObject(PsUtils.GetMainModule(process).FileVersionInfo, true); + ProcessModule mainModule = PsUtils.GetMainModule(process); + if (mainModule != null) { + WriteObject(mainModule.FileVersionInfo, true); + } } catch (InvalidOperationException exception) { diff --git a/src/System.Management.Automation/utils/PsUtils.cs b/src/System.Management.Automation/utils/PsUtils.cs index b6ede8c993f..6729b1b2db2 100644 --- a/src/System.Management.Automation/utils/PsUtils.cs +++ b/src/System.Management.Automation/utils/PsUtils.cs @@ -59,13 +59,12 @@ internal static class PsUtils internal static ProcessModule GetMainModule(Process targetProcess) { int caughtCount = 0; - ProcessModule mainModule = null; - while (mainModule == null) + while (true) { try { - mainModule = targetProcess.MainModule; + return targetProcess.MainModule; } catch (System.ComponentModel.Win32Exception e) { @@ -81,8 +80,6 @@ internal static ProcessModule GetMainModule(Process targetProcess) throw; } } - - return mainModule; } // Cache of the current process' parentId diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 index 2852b79ecf9..ff5ea1f7b8e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 @@ -13,9 +13,25 @@ Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') { } } - It "Should support -FileVersionInfo" -Skip:(!$IsWindows) { + It "Should support -FileVersionInfo" { $pwshVersion = Get-Process -Id $pid -FileVersionInfo - $PSVersionTable.PSVersion | Should -Match $pwshVersion.FileVersion + if ($IsWindows) { + $PSVersionTable.PSVersion | Should -MatchExactly $pwshVersion.FileVersion + $gitCommitId = $PSVersionTable.GitCommitId + if ($gitCommitId.StartsWith("v")) { $gitCommitId = $gitCommitId.Substring(1) } + $pwshVersion.ProductVersion.Replace("-dirty","") | Should -BeExactly $gitCommitId + } else { + $pwshVersion.FileVersion | Should -BeNullOrEmpty + } + } + + It "Run with parameter -FileVersionInfo should not hang on non Windows platform also when process' main module is null." -Skip:$IsWindows { + # Main module for idle process can be null on non-Windows platforms + { $pwshVersion = Get-Process -Id 0 -FileVersionInfo -ErrorAction Stop } | Should -Not -Throw + } + + It "Run with parameter -FileVersionInfo for idle process should throw on Windows." -Skip:(!$IsWindows) { + { $pwshVersion = Get-Process -Id 0 -FileVersionInfo -ErrorAction Stop } | Should -Throw -ErrorId "CouldNotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand" } }