在以下代码中,$ 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但重命名项中的-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
答案 0 :(得分:6)
对于那些只需要一行命令的用户,请注意这是一个仅限PowerShell的错误,并且这个命令在旧的命令提示符下运行正常。
powershell命令的完整形式是
ren -Path [something with a wildcard] -NewName [something else]
错误与Path参数的值有关。它接受路径中的通配符,但必须解析为单个文件[1]。要在PowerShell中使用通配符,您需要将它们逐个管道到重命名项目命令。以下是将txt文件重命名为log [2]的示例:
get-childItem *.txt | rename-item -newname { $_.name -replace '\.txt','.log' }
答案 1 :(得分:1)
如果Rename-Item不喜欢通配符,则不要给它一个
Convert-Path C:\dbbackups\cm_$client-*.full.bak | % {
if (Test-Path $_) {
Rename-Item $_ cm_$client.bak
}
}