allow * to be used in registry path for remove-item (#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
This commit is contained in:
Steve Lee 2017-09-25 10:15:38 -07:00 committed by Aditya Patwardhan
parent 3237d43b26
commit 65415c3b9e
4 changed files with 60 additions and 23 deletions

View file

@ -3107,6 +3107,18 @@ namespace Microsoft.PowerShell.Commands
try
{
resolvedPSPaths = SessionState.Path.GetResolvedPSPathFromPSPath(path, currentContext);
if (null != LiteralPath && 0 == resolvedPSPaths.Count)
{
ItemNotFoundException pathNotFound =
new ItemNotFoundException(
path,
"PathNotFound",
SessionStateStrings.PathNotFound);
WriteError(new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
continue;
}
}
finally
{
@ -3253,18 +3265,23 @@ namespace Microsoft.PowerShell.Commands
bool shouldRecurse = Recurse;
bool treatAsFile = false;
try
// only check if path is a directory using DirectoryInfo if using FileSystemProvider
if (resolvedPath.Provider.Name.Equals(FileSystemProvider.ProviderName, StringComparison.OrdinalIgnoreCase))
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(providerPath);
if (di != null && (di.Attributes & System.IO.FileAttributes.ReparsePoint) != 0)
try
{
shouldRecurse = false;
treatAsFile = true;
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(providerPath);
if (di != null && (di.Attributes & System.IO.FileAttributes.ReparsePoint) != 0)
{
shouldRecurse = false;
treatAsFile = true;
}
}
catch (System.IO.FileNotFoundException)
{
// not a directory
}
}
catch (System.IO.FileNotFoundException)
{
// not a directory
}
if (!treatAsFile && !Recurse && hasChildren)

View file

@ -370,6 +370,10 @@ Describe "Handling of globbing patterns" -Tags "CI" {
Copy-Item -LiteralPath $file.FullName -Destination $newPath
Test-Path -LiteralPath $newPath | Should Be $true
}
It "Remove-Item -LiteralPath should fail if it contains asterisk" {
{ Remove-Item -LiteralPath ./foo*.txt -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand"
}
}
}

View file

@ -469,6 +469,22 @@ Describe "Extended Registry Provider Tests" -Tags @("Feature", "RequireAdminOnWi
$result."$testPropertyName" | Should Be $testPropertyValue
}
}
Context "Validate -LiteralPath" {
It "Verify New-Item and Remove-Item work with asterisk" {
try {
$tempPath = "HKCU:\_tmp"
$testPath = "$tempPath\*\sub"
$null = New-Item -Force $testPath
$testPath | Should Exist
Remove-Item -LiteralPath $testPath
$testPath | Should Not Exist
}
finally {
Remove-Item -Recurse $tempPath -ErrorAction SilentlyContinue
}
}
}
}
} finally {

View file

@ -292,11 +292,11 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
# not a .Net type so can't use BeOfType
$newItem.PSObject.TypeNames[0] | Should Be "Microsoft.WSMan.Management.WSManConfigContainerElement#ComputerLevel"
$newItem.Name | Should Be $expected
Remove-Item WSMan:\$name
Remove-Item WSMan:\$name -Recurse -Force
"WSMan:\$name" | Should Not Exist
}
finally {
Remove-Item WSMan:\$name -Force -ErrorAction SilentlyContinue
Remove-Item WSMan:\$name -Recurse -Force -ErrorAction SilentlyContinue
}
}
@ -314,11 +314,11 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
$property.Value | Should Be $listenerXml.Listener.$($property.Name)
}
}
Remove-Item -Path "WSMan:\localhost\Listener\$listenerName" -Force
Remove-Item -Path "WSMan:\localhost\Listener\$listenerName" -Recurse -Force
$newListener.PSPath | Should Not Exist
}
finally {
Remove-Item -Path "WSMan:\localhost\Listener\$listenerName" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "WSMan:\localhost\Listener\$listenerName" -Recurse -Force -ErrorAction SilentlyContinue
}
}
@ -332,11 +332,11 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
-RunAsCredential $creds
$expectedMissingProperties = @("InitializationParameters")
Test-Plugin -Plugin $plugin -expectedMissingProperties $expectedMissingProperties
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\ -Recurse -Force
"WSMan:\localhost\Plugin\TestPlugin2" | Should Not Exist
}
finally {
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\ -Force -ErrorAction SilentlyContinue
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\ -Recurse -Force -ErrorAction SilentlyContinue
}
}
@ -363,11 +363,11 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
try {
$plugin = New-Item -Path WSMan:\localhost\Plugin -File $testdrive\plugin.xml -Name TestPlugin2
Test-Plugin -Plugin $plugin
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\
Remove-Item WSMan:\localhost\Plugin\TestPlugin2\ -Recurse -Force
"WSMan:\localhost\Plugin\TestPlugin2\" | Should Not Exist
}
finally {
Remove-Item "WSMan:\localhost\Plugin\TestPlugin2\" -Force -ErrorAction SilentlyContinue
Remove-Item "WSMan:\localhost\Plugin\TestPlugin2\" -Recurse -Force -ErrorAction SilentlyContinue
}
}
@ -379,11 +379,11 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
$properties = Get-ChildItem $resource.PSPath
($properties | Where-Object { $_.Name -eq "ResourceUri" }).Value | Should Be "http://foo/"
($properties | Where-Object { $_.Name -eq "Capability" })[0].Value | Should Be "shell"
Remove-Item $resource.PSPath
Remove-Item $resource.PSPath -Recurse -Force
$resource.PSPath | Should Not Exist
}
finally {
Remove-Item $resource.PSPath -Force -ErrorAction SilentlyContinue
Remove-Item $resource.PSPath -Recurse -Force -ErrorAction SilentlyContinue
}
}
@ -394,7 +394,7 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
$parameterObj = Get-Item $parameter.PSPath
$parameterObj.Name | Should Be "foo"
$parameterObj.Value | Should Be "bar"
Remove-Item $parameter.PSPath
Remove-Item $parameter.PSPath -Force
$parameter.PSPath | Should Not Exist
}
finally {
@ -407,16 +407,16 @@ Describe "WSMan Config Provider" -Tag Feature,RequireAdminOnWindows {
$sddl = "O:NSG:BAD:P(A;;GA;;;BA)"
$resource = Get-ChildItem -Path WSMan:\localhost\Plugin\TestPlugin\Resources\ | Select-Object -First 1
# remove existing security resource since the folder name is just a hash of the resource uri
Get-ChildItem "$($resource.PSPath)\Security" | Remove-Item
Get-ChildItem "$($resource.PSPath)\Security" | Remove-Item -Recurse -Force
$security = New-Item "$($resource.PSPath)\Security" -SDDL $sddl -Force
$security.PSPath | Should Exist
$securityObj = Get-Item $security.PSPath
(Get-ChildItem $securityObj.PSPath | Where-Object { $_.Name -eq 'sddl' }).Value | Should Be $sddl
Remove-Item $security.PSPath
Remove-Item $security.PSPath -Recurse -Force
$security.PSPath | Should Not Exist
}
finally {
Remove-Item $security.PSPath -Force -ErrorAction SilentlyContinue
Remove-Item $security.PSPath -Recurse -Force -ErrorAction SilentlyContinue
}
}
}