Skip to content

Commit 9537037

Browse files
nohwndCopilot
andauthored
Fix #2678: Suppress Get-CimInstance access denied error (#2695)
* Fix #2678: Suppress Get-CimInstance access denied error Copilot-generated fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Add real regression test: simulate Get-CimInstance returning null The previous tests were sanity smoke tests that passed even on the buggy code (they only verified the function returned the expected hashtable shape). Add a Windows-only test that stubs SafeCommands[Get-CimInstance] to return $null (simulating access denied with -ErrorAction Ignore) and asserts the Unknown / 0.0.0.0 fallback is applied. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 840c44f commit 9537037

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

src/functions/TestResults.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,10 @@ function Get-RunTimeEnvironment {
468468
$computerName = $env:ComputerName
469469
$userName = $env:Username
470470
if ($null -ne $SafeCommands['Get-CimInstance']) {
471-
$osSystemInformation = (& $SafeCommands['Get-CimInstance'] Win32_OperatingSystem)
471+
$osSystemInformation = (& $SafeCommands['Get-CimInstance'] Win32_OperatingSystem -ErrorAction Ignore)
472472
}
473473
elseif ($null -ne $SafeCommands['Get-WmiObject']) {
474-
$osSystemInformation = (& $SafeCommands['Get-WmiObject'] Win32_OperatingSystem)
474+
$osSystemInformation = (& $SafeCommands['Get-WmiObject'] Win32_OperatingSystem -ErrorAction Ignore)
475475
}
476476
elseif ($IsMacOS -or $IsLinux) {
477477
$osSystemInformation = @{
@@ -492,7 +492,9 @@ function Get-RunTimeEnvironment {
492492
# well, we tried
493493
}
494494
}
495-
else {
495+
496+
# Fall back to unknown values if WMI/CIM returned null (e.g. access denied when not running as Administrator)
497+
if ($null -eq $osSystemInformation) {
496498
$osSystemInformation = @{
497499
Name = 'Unknown'
498500
Version = '0.0.0.0'

tst/functions/TestResults.Tests.ps1

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,49 @@ InPesterModuleScope {
9191
Pop-Location
9292

9393
}
94+
95+
# Regression tests for https://github.com/pester/Pester/issues/2678
96+
# Get-CimInstance can fail with access denied when not running as Administrator.
97+
# The fix adds -ErrorAction Ignore and a fallback to Unknown values so the report
98+
# is not broken just because OS info is unavailable.
99+
Describe "Get-RunTimeEnvironment" {
100+
It "Returns a hashtable with expected keys without throwing" {
101+
$result = Get-RunTimeEnvironment
102+
$result | Should -BeOfType [hashtable]
103+
$result.Keys | Should -Contain 'os-version'
104+
$result.Keys | Should -Contain 'platform'
105+
$result.Keys | Should -Contain 'machine-name'
106+
$result.Keys | Should -Contain 'user'
107+
$result.Keys | Should -Contain 'cwd'
108+
$result.Keys | Should -Contain 'clr-version'
109+
$result['os-version'] | Should -Not -BeNullOrEmpty
110+
$result['platform'] | Should -Not -BeNullOrEmpty
111+
}
112+
113+
It "Falls back to Unknown OS info when Get-CimInstance returns null (access denied)" -Skip:(-not $IsWindows) {
114+
# Simulate -ErrorAction Ignore swallowing an access-denied error: the call returns $null.
115+
# Before the fix, $osSystemInformation stayed $null and $osSystemInformation.Version
116+
# silently produced $null fields in the report; an ugly access-denied error was also
117+
# printed because -ErrorAction Ignore was missing.
118+
$originalCim = $SafeCommands['Get-CimInstance']
119+
$originalWmi = $SafeCommands['Get-WmiObject']
120+
try {
121+
$SafeCommands['Get-CimInstance'] = { param($Class) }
122+
if ($null -ne $originalWmi) {
123+
$SafeCommands['Get-WmiObject'] = { param($Class) }
124+
}
125+
126+
$result = Get-RunTimeEnvironment
127+
128+
$result['platform'] | Should -Be 'Unknown'
129+
$result['os-version'] | Should -Be '0.0.0.0'
130+
}
131+
finally {
132+
$SafeCommands['Get-CimInstance'] = $originalCim
133+
if ($null -ne $originalWmi) {
134+
$SafeCommands['Get-WmiObject'] = $originalWmi
135+
}
136+
}
137+
}
138+
}
94139
}

0 commit comments

Comments
 (0)