Skip to content

Commit 65415c3

Browse files
SteveL-MSFTadityapatwardhan
authored andcommitted
allow * to be used in registry path for remove-item (PowerShell#4866)
* [feature] allow * to be used in registry path for remove-item * [feature] address PR feedback have remove-item return error if -literalpath doesn't resolve to path * [feature] fix in navigation exposed an issue with WSMan Config Provider tests that require -Recurse to be used otherwise a confirmation prompt shows up
1 parent 3237d43 commit 65415c3

File tree

4 files changed

+60
-23
lines changed

4 files changed

+60
-23
lines changed

src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3107,6 +3107,18 @@ protected override void ProcessRecord()
31073107
try
31083108
{
31093109
resolvedPSPaths = SessionState.Path.GetResolvedPSPathFromPSPath(path, currentContext);
3110+
if (null != LiteralPath && 0 == resolvedPSPaths.Count)
3111+
{
3112+
ItemNotFoundException pathNotFound =
3113+
new ItemNotFoundException(
3114+
path,
3115+
"PathNotFound",
3116+
SessionStateStrings.PathNotFound);
3117+
WriteError(new ErrorRecord(
3118+
pathNotFound.ErrorRecord,
3119+
pathNotFound));
3120+
continue;
3121+
}
31103122
}
31113123
finally
31123124
{
@@ -3253,18 +3265,23 @@ protected override void ProcessRecord()
32533265

32543266
bool shouldRecurse = Recurse;
32553267
bool treatAsFile = false;
3256-
try
3268+
3269+
// only check if path is a directory using DirectoryInfo if using FileSystemProvider
3270+
if (resolvedPath.Provider.Name.Equals(FileSystemProvider.ProviderName, StringComparison.OrdinalIgnoreCase))
32573271
{
3258-
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(providerPath);
3259-
if (di != null && (di.Attributes & System.IO.FileAttributes.ReparsePoint) != 0)
3272+
try
32603273
{
3261-
shouldRecurse = false;
3262-
treatAsFile = true;
3274+
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(providerPath);
3275+
if (di != null && (di.Attributes & System.IO.FileAttributes.ReparsePoint) != 0)
3276+
{
3277+
shouldRecurse = false;
3278+
treatAsFile = true;
3279+
}
3280+
}
3281+
catch (System.IO.FileNotFoundException)
3282+
{
3283+
// not a directory
32633284
}
3264-
}
3265-
catch (System.IO.FileNotFoundException)
3266-
{
3267-
// not a directory
32683285
}
32693286

32703287
if (!treatAsFile && !Recurse && hasChildren)

test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ Describe "Handling of globbing patterns" -Tags "CI" {
370370
Copy-Item -LiteralPath $file.FullName -Destination $newPath
371371
Test-Path -LiteralPath $newPath | Should Be $true
372372
}
373+
374+
It "Remove-Item -LiteralPath should fail if it contains asterisk" {
375+
{ Remove-Item -LiteralPath ./foo*.txt -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand"
376+
}
373377
}
374378
}
375379

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,22 @@ Describe "Extended Registry Provider Tests" -Tags @("Feature", "RequireAdminOnWi
469469
$result."$testPropertyName" | Should Be $testPropertyValue
470470
}
471471
}
472+
473+
Context "Validate -LiteralPath" {
474+
It "Verify New-Item and Remove-Item work with asterisk" {
475+
try {
476+
$tempPath = "HKCU:\_tmp"
477+
$testPath = "$tempPath\*\sub"
478+
$null = New-Item -Force $testPath
479+
$testPath | Should Exist
480+
Remove-Item -LiteralPath $testPath
481+
$testPath | Should Not Exist
482+
}
483+
finally {
484+
Remove-Item -Recurse $tempPath -ErrorAction SilentlyContinue
485+
}
486+
}
487+
}
472488
}
473489

474490
} finally {

test/powershell/Modules/Microsoft.WSMan.Management/ConfigProvider.Tests.ps1

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,11 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
292292
# not a .Net type so can't use BeOfType
293293
$newItem.PSObject.TypeNames[0] | Should Be "Microsoft.WSMan.Management.WSManConfigContainerElement#ComputerLevel"
294294
$newItem.Name | Should Be $expected
295-
Remove-Item WSMan:\$name
295+
Remove-Item WSMan:\$name -Recurse -Force
296296
"WSMan:\$name" | Should Not Exist
297297
}
298298
finally {
299-
Remove-Item WSMan:\$name -Force -ErrorAction SilentlyContinue
299+
Remove-Item WSMan:\$name -Recurse -Force -ErrorAction SilentlyContinue
300300
}
301301
}
302302

@@ -314,11 +314,11 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
314314
$property.Value | Should Be $listenerXml.Listener.$($property.Name)
315315
}
316316
}
317-
Remove-Item -Path "WSMan:\localhost\Listener\$listenerName" -Force
317+
Remove-Item -Path "WSMan:\localhost\Listener\$listenerName" -Recurse -Force
318318
$newListener.PSPath | Should Not Exist
319319
}
320320
finally {
321-
Remove-Item -Path "WSMan:\localhost\Listener\$listenerName" -Force -ErrorAction SilentlyContinue
321+
Remove-Item -Path "WSMan:\localhost\Listener\$listenerName" -Recurse -Force -ErrorAction SilentlyContinue
322322
}
323323
}
324324

@@ -332,11 +332,11 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
332332
-RunAsCredential $creds
333333
$expectedMissingProperties = @("InitializationParameters")
334334
Test-Plugin -Plugin $plugin -expectedMissingProperties $expectedMissingProperties
335-
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\
335+
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\ -Recurse -Force
336336
"WSMan:\localhost\Plugin\TestPlugin2" | Should Not Exist
337337
}
338338
finally {
339-
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\ -Force -ErrorAction SilentlyContinue
339+
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\ -Recurse -Force -ErrorAction SilentlyContinue
340340
}
341341
}
342342

@@ -363,11 +363,11 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
363363
try {
364364
$plugin = New-Item -Path WSMan:\localhost\Plugin -File $testdrive\plugin.xml -Name TestPlugin2
365365
Test-Plugin -Plugin $plugin
366-
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\
366+
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\ -Recurse -Force
367367
"WSMan:\localhost\Plugin\TestPlugin2\" | Should Not Exist
368368
}
369369
finally {
370-
Remove-Item "WSMan:\localhost\Plugin\TestPlugin2\" -Force -ErrorAction SilentlyContinue
370+
Remove-Item "WSMan:\localhost\Plugin\TestPlugin2\" -Recurse -Force -ErrorAction SilentlyContinue
371371
}
372372
}
373373

@@ -379,11 +379,11 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
379379
$properties = Get-ChildItem $resource.PSPath
380380
($properties | Where-Object { $_.Name -eq "ResourceUri" }).Value | Should Be "http://foo/"
381381
($properties | Where-Object { $_.Name -eq "Capability" })[0].Value | Should Be "shell"
382-
Remove-Item $resource.PSPath
382+
Remove-Item $resource.PSPath -Recurse -Force
383383
$resource.PSPath | Should Not Exist
384384
}
385385
finally {
386-
Remove-Item $resource.PSPath -Force -ErrorAction SilentlyContinue
386+
Remove-Item $resource.PSPath -Recurse -Force -ErrorAction SilentlyContinue
387387
}
388388
}
389389

@@ -394,7 +394,7 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
394394
$parameterObj = Get-Item $parameter.PSPath
395395
$parameterObj.Name | Should Be "foo"
396396
$parameterObj.Value | Should Be "bar"
397-
Remove-Item $parameter.PSPath
397+
Remove-Item $parameter.PSPath -Force
398398
$parameter.PSPath | Should Not Exist
399399
}
400400
finally {
@@ -407,16 +407,16 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
407407
$sddl = "O:NSG:BAD:P(A;;GA;;;BA)"
408408
$resource = Get-ChildItem -Path WSMan:\localhost\Plugin\TestPlugin\Resources\ | Select-Object -First 1
409409
# remove existing security resource since the folder name is just a hash of the resource uri
410-
Get-ChildItem "$($resource.PSPath)\Security" | Remove-Item
410+
Get-ChildItem "$($resource.PSPath)\Security" | Remove-Item -Recurse -Force
411411
$security = New-Item "$($resource.PSPath)\Security" -SDDL $sddl -Force
412412
$security.PSPath | Should Exist
413413
$securityObj = Get-Item $security.PSPath
414414
(Get-ChildItem $securityObj.PSPath | Where-Object { $_.Name -eq 'sddl' }).Value | Should Be $sddl
415-
Remove-Item $security.PSPath
415+
Remove-Item $security.PSPath -Recurse -Force
416416
$security.PSPath | Should Not Exist
417417
}
418418
finally {
419-
Remove-Item $security.PSPath -Force -ErrorAction SilentlyContinue
419+
Remove-Item $security.PSPath -Recurse -Force -ErrorAction SilentlyContinue
420420
}
421421
}
422422
}

0 commit comments

Comments
 (0)