PowerShell 提示路径,但路径有效

标签 powershell rename-item-cmdlet

在下面的代码中,当$client = XRS1

if (Test-Path C:\dbbackups\cm_$client-*.full.bak){
Rename-Item -path C:\dbbackups\cm_$client-*.bak -newname cm_$client.bak
Write-Host "New file found, renamed to: cm_$client.bak"

Test-Path语句可以找到C:\dbbackups\cm_xrs1-2013414.full.full.bak,但是Rename-Item中的-path找不到。

我得到的错误是

Rename-Item : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again.
At C:\Users\Aaron\Documents\0000 - PowerShell DB Update Utility\UpdateCMDatabases.ps1:167 char:1
+ Rename-Item -path C:\dbbackups\cm_$client-*.bak -newname cm_$client.bak
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Rename-Item], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand

最佳答案

对于那些只需要一行命令的人,请注意这是一个仅限 powershell 的错误,并且该命令在良好的旧命令提示符下工作得很好。

powershell命令的完整形式是

ren -Path [something with a wildcard] -NewName [something else]

错误与 Path 参数的值有关。它接受路径中的通配符,但必须解析为单个文件 [1] .要在 powershell 中使用通配符,您需要将它们一一传送到重命名项目命令。这是一个将 txt 文件重命名为日志的示例 [2] :

get-childItem *.txt | rename-item -newname { $_.name -replace '\.txt','.log' }

关于PowerShell 提示路径,但路径有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25007739/

相关文章:

Powershell Rename-Item 一次性替换多个命名参数

powershell - 使用 PowerShell 将前导零添加到文件名字符串

.net - 从托管 powershell 中的脚本或命令检索参数

powershell - Powershell,在解析和切换目录名称字符串后重命名目录

powershell - 输出文本文件以识别PowerShell中的操作文件

string - 如何用内部@""@封装脚本,用外部@""@

sharepoint - 使用 powershell 将用户添加到 SharePoint 2010 组

powershell - 如何使用PowerShell按日期对文件进行分组?

powershell - 使用凭据将文件复制到远程服务器的 PowerShell 命令出错