在下面的代码中,当$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/