无法重命名powershell中的项目,因为它为null

时间:2016-11-17 22:19:40

标签: windows powershell

qqq.exe存在。

$ DB是正确的,我在其他时候使用它就好了。

$DB = Get-Content C:\Users\asd\Desktop\Farm\AccountList.txt
foreach ($x in $DB){
    Rename-Item C:\Users\asd\Desktop\Farm\$x\qqq.exe $x.exe
}

Rename-Item:无法将参数绑定到参数'NewName',因为它为null。 在C:\ Users \ asd \ Desktop \ Farm \ test2.ps1:3 char:57 +重命名 - 项目C:\ Users \ asd \ Desktop \ Farm \ $ x \ qqq.exe $ x.exe + ~~~~~~     + CategoryInfo:InvalidData :( :) [Rename-Item],ParameterBindingValidationException     + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RenameItemCommand

1 个答案:

答案 0 :(得分:1)

当powershell解析器看到以$开头的参数时,它会将其视为表达式,这意味着它将尝试将其作为代码进行评估。

由于字符串$x没有名为exe的属性,因此表达式会导致$nullRename-Item抛出您看到的错误。

如果您使用双引号字符串,解析器将在点后停止评估$x

Rename-Item C:\Users\asd\Desktop\Farm\$x\qqq.exe "$x.exe"

来自Get-Help about_Parsing

When processing a command, the Windows PowerShell parser operates
in expression mode or in argument mode: 

    - In expression mode, character string values must be contained in
      quotation marks. Numbers not enclosed in quotation marks are treated
      as numerical values (rather than as a series of characters). 

    - In argument mode, each value is treated as an expandable string 
      unless it begins with one of the following special characters: dollar
      sign ($), at sign (@), single quotation mark ('), double quotation
      mark ("), or an opening parenthesis (().