【问题标题】:Cannot Bind Argument to Parameter 'NewName' because it is an empty string无法将参数绑定到参数“NewName”,因为它是一个空字符串
【发布时间】:2014-09-15 00:48:01
【问题描述】:
Get-ChildItem -Name *.txt | Rename-Item -NewName { $_.name -replace '\.txt','.log' }

我的当前路径中有 3 个文本文件,我正在使用在最后一个示例中找到的这段代码...

get-help rename-item -full

(Powershell 2.0 版)。无论出于何种原因,我一直收到以下错误:

Rename-Item : Cannot bind argument to parameter 'NewName' because it is an empty string.
At line:1 char:40
+ Get-ChildItem -Name *.txt | Rename-Item <<<<  -NewName { $_.name -replace 
'\.txt','.log' }
+ CategoryInfo          : InvalidData: (testfile3.txt:PSObject) [Rename-Item], 
ParameterBindingValidationException
+ FullyQualifiedErrorId : 
ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.Rena
meItemCommand

显然,我将 .txt 更改为 .log 的格式不是空字符串,这与 Microsoft 的最后一个 cmdlet rename-item 示例中的代码完全相同。

【问题讨论】:

    标签: powershell powershell-2.0 rename-item-cmdlet


    【解决方案1】:

    要么不使用-Name 参数,因为它只输出包含完整路径的字符串,要么不引用Name 属性:

    Get-ChildItem -Name *.txt | Rename-Item -NewName { $_ -replace '\.txt','.log' }
    

    【讨论】:

    • 谢谢,仍然是一个问题,我如何确定 Name 属性仅将文件输出为字符串?
    • 您可以查看帮助 man Get-ChildItem -Parameter Name 或通过 Get-Member 例如管道输出Get-ChildItem -Name *.txt | Get-Member.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多