Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb5fbe9e

Browse files
authored
FixGet-ItemProperty to report non-terminating error for cast exception (#21115)
1 parente127345 commitb5fbe9e

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

‎src/System.Management.Automation/namespaces/RegistryProvider.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,8 +1825,19 @@ public void GetProperty(
18251825
notePropertyName=LocalizedDefaultToken;
18261826
}
18271827

1828-
propertyResults.Properties.Add(newPSNoteProperty(notePropertyName,key.GetValue(valueName)));
1829-
valueAdded=true;
1828+
try
1829+
{
1830+
propertyResults.Properties.Add(newPSNoteProperty(notePropertyName,key.GetValue(valueName)));
1831+
valueAdded=true;
1832+
}
1833+
catch(InvalidCastExceptioninvalidCast)
1834+
{
1835+
WriteError(newErrorRecord(
1836+
invalidCast,
1837+
invalidCast.GetType().FullName,
1838+
ErrorCategory.ReadError,
1839+
path));
1840+
}
18301841
}
18311842

18321843
key.Close();

‎test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,47 @@ Describe "Extended Registry Provider Tests" -Tags @("Feature", "RequireAdminOnWi
471471
}
472472
}
473473
}
474+
475+
Context"Validate Get-ItemProperty Cast Exception" {
476+
BeforeAll {
477+
if ($IsWindows) {
478+
$registrySubkeyPath='HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\badreg'
479+
480+
# Below will import .reg file with 64 bit integer in 32 bit DWORD
481+
$badRegistryContent=@"
482+
Windows Registry Editor Version 5.00
483+
484+
[$registrySubkeyPath]
485+
"NoModify"=hex(4):01,00,00,00,00,00,00,00
486+
"@
487+
488+
$badRegistryPath=Join-Path-Path$TestDrive-ChildPath badreg.reg
489+
$badRegistryContent|Set-Content-Path$badRegistryPath
490+
reg.exe import$badRegistryPath
491+
492+
$registryProviderSubkeyPath='HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\badreg'
493+
}
494+
}
495+
496+
It"Validate non-terminating error for cast" {
497+
Get-ItemProperty-Path$registryProviderSubkeyPath-ErrorVariable err-ErrorAction SilentlyContinue
498+
$err| Should-HaveCount1
499+
$err[0].Exception| Should-BeOfType [System.InvalidCastException]
500+
$err[0].TargetObject| Should-BeExactly$registrySubkeyPath
501+
$err[0].CategoryInfo.Category| Should-BeExactly'ReadError'
502+
$err[0].FullyQualifiedErrorId| Should-BeExactly'System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand'
503+
}
504+
505+
It"Validate terminating error for cast" {
506+
{Get-ItemProperty-Path$registryProviderSubkeyPath-ErrorAction Stop }| Should-Throw-ErrorId'System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand'
507+
}
508+
509+
AfterAll {
510+
if ($IsWindows) {
511+
reg.exe delete$registrySubkeyPath/f
512+
}
513+
}
514+
}
474515
}
475516

476517
}finally {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp