2016-11-17 40 views
0

qqq.exeが存在します。nullのため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:引数がnullであるため、パラメータ 'NewName'に引数をバインドできません。 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という名前のプロパティがないため、式の結果は$nullとなり、Rename-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

関連する問題