2016-11-17 92 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

相關問題