2016-11-17 65 views
0

qqq.exe存在。无法重命名powershell中的项目,因为它为空

$ 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 + Rename-Item C:\ Users \ asd \ Desktop \ Farm \ $ x \ qqq.exe $ x.exe +〜 ~~~~~ + CategoryInfo:InvalidData:(:) [重命名-项目],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RenameItemCommand

+1

' $ x.exe' - >'“$ x.exe”' –

+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 ((). 
+0

“$($ x).exe”将更适合将来使用:-) – filimonic

相关问题