mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix Rename-Item -Path with wildcard char (#7398)
This commit is contained in:
@@ -3519,22 +3519,87 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
#region Command code
|
||||
|
||||
private Collection<PathInfo> GetResolvedPaths(string path)
|
||||
{
|
||||
Collection<PathInfo> results = null;
|
||||
try
|
||||
{
|
||||
results = SessionState.Path.GetResolvedPSPathFromPSPath(path, CmdletProviderContext);
|
||||
}
|
||||
catch (PSNotSupportedException notSupported)
|
||||
{
|
||||
WriteError(
|
||||
new ErrorRecord(
|
||||
notSupported.ErrorRecord,
|
||||
notSupported));
|
||||
}
|
||||
catch (DriveNotFoundException driveNotFound)
|
||||
{
|
||||
WriteError(
|
||||
new ErrorRecord(
|
||||
driveNotFound.ErrorRecord,
|
||||
driveNotFound));
|
||||
}
|
||||
catch (ProviderNotFoundException providerNotFound)
|
||||
{
|
||||
WriteError(
|
||||
new ErrorRecord(
|
||||
providerNotFound.ErrorRecord,
|
||||
providerNotFound));
|
||||
}
|
||||
catch (ItemNotFoundException pathNotFound)
|
||||
{
|
||||
WriteError(
|
||||
new ErrorRecord(
|
||||
pathNotFound.ErrorRecord,
|
||||
pathNotFound));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves the specified item to the specified destination
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (SuppressWildcardExpansion)
|
||||
{
|
||||
RenameItem(Path, literalPath: true);
|
||||
return;
|
||||
}
|
||||
|
||||
Collection<PathInfo> resolvedPaths = GetResolvedPaths(Path);
|
||||
|
||||
if (resolvedPaths == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (resolvedPaths.Count == 1)
|
||||
{
|
||||
RenameItem(resolvedPaths[0].Path, literalPath: true);
|
||||
}
|
||||
else
|
||||
{
|
||||
RenameItem(WildcardPattern.Unescape(Path), literalPath: true);
|
||||
}
|
||||
}
|
||||
|
||||
private void RenameItem(string path, bool literalPath = false)
|
||||
{
|
||||
CmdletProviderContext currentContext = CmdletProviderContext;
|
||||
currentContext.SuppressWildcardExpansion = literalPath;
|
||||
|
||||
try
|
||||
{
|
||||
if (!InvokeProvider.Item.Exists(Path, currentContext))
|
||||
if (!InvokeProvider.Item.Exists(path, currentContext))
|
||||
{
|
||||
PSInvalidOperationException invalidOperation =
|
||||
(PSInvalidOperationException)
|
||||
PSTraceSource.NewInvalidOperationException(
|
||||
NavigationResources.RenameItemDoesntExist,
|
||||
Path);
|
||||
path);
|
||||
|
||||
WriteError(
|
||||
new ErrorRecord(
|
||||
@@ -3580,7 +3645,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
bool isCurrentLocationOrAncestor = false;
|
||||
try
|
||||
{
|
||||
isCurrentLocationOrAncestor = SessionState.Path.IsCurrentLocationOrAncestor(_path, currentContext);
|
||||
isCurrentLocationOrAncestor = SessionState.Path.IsCurrentLocationOrAncestor(path, currentContext);
|
||||
}
|
||||
catch (PSNotSupportedException notSupported)
|
||||
{
|
||||
@@ -3621,7 +3686,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
(PSInvalidOperationException)
|
||||
PSTraceSource.NewInvalidOperationException(
|
||||
NavigationResources.RenamedItemInUse,
|
||||
Path);
|
||||
path);
|
||||
|
||||
WriteError(
|
||||
new ErrorRecord(
|
||||
@@ -3635,12 +3700,12 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
currentContext.PassThru = PassThru;
|
||||
|
||||
tracer.WriteLine("Rename {0} to {1}", Path, NewName);
|
||||
tracer.WriteLine("Rename {0} to {1}", path, NewName);
|
||||
|
||||
try
|
||||
{
|
||||
// Now do the rename
|
||||
InvokeProvider.Item.Rename(Path, NewName, currentContext);
|
||||
InvokeProvider.Item.Rename(path, NewName, currentContext);
|
||||
}
|
||||
catch (PSNotSupportedException notSupported)
|
||||
{
|
||||
@@ -3674,7 +3739,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
pathNotFound));
|
||||
return;
|
||||
}
|
||||
} // ProcessRecord
|
||||
}
|
||||
#endregion Command code
|
||||
|
||||
} // RenameItemCommand
|
||||
|
||||
@@ -433,7 +433,13 @@ namespace System.Management.Automation
|
||||
|
||||
s_pathResolutionTracer.WriteLine("Path is DRIVE-QUALIFIED");
|
||||
|
||||
string relativePath = GetDriveRootRelativePathFromPSPath(path, context, true, out drive, out providerInstance);
|
||||
string relativePath =
|
||||
GetDriveRootRelativePathFromPSPath(
|
||||
path,
|
||||
context,
|
||||
!context.SuppressWildcardExpansion,
|
||||
out drive,
|
||||
out providerInstance);
|
||||
|
||||
Dbg.Diagnostics.Assert(
|
||||
drive != null,
|
||||
|
||||
Reference in New Issue
Block a user