2

I have the following script in Powershell ISE.

cd E:\Data
@"
xxxx.zip
yyyy.zip
"@ -split "`n" | % { echo "'$_'"; test-path -path "$_" -EA Stop }

However it always raises error.

'xxxx.ZIP'
False
Illegal characters in path.
At line:175 char:27
+ % { echo "'$_'"; test-path <<<<  -path "$_" -EA Stop }
    + CategoryInfo          : InvalidArgument: (E:\Data\xxxx.ZIP:String) [Test-Path], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.TestPathCommand

However, I can run Test-Path -path xxxx.zip or just hard code 'xxxx.zip' in the script and it runs fine. What's the problem of piped string?

Update

If I change the last script to % { echo "'$_'"; "test-path -path $_ -EA Stop" } and copy/paste the output ("test-path -path xxxx.ZIP -EA Stop") to the command line. It works.

Update

It seems it works in powershell console. An ISE bug?

4

3 に答える 3

4

ISEでは、次のように、キャリッジリターンとそれに続くPowerShellの改行を使用してhere-stringを分割する必要があります。

cd E:\Data
@"
xxxx.zip
yyyy.zip
"@ -split "`r`n" | % { echo "'$_'"; test-path -path "$_" -EA Stop }

この機能を使用する場合:

function asciiToHex($a)
{
$b = $a.ToCharArray();
Foreach ($element in $b) {$c = $c + "%#x" + [System.String]::Format("{0:X}",
[System.Convert]::ToUInt32($element)) + ";"}
$c
}

iseのhere-stringを変換するには、次のようにします。

asciitohex $t
%#x78;%#x78;%#x78;%#x78;%#x2E;%#x7A;%#x69;%#x70;%#xD;%#xA;%#x79;%#x79;%#x79;%#x79;%#x2E;%#x7A;%#x69;%#x70;

ただし、PowerShellコンソールでは

asciitohex $t
%#x78;%#x78;%#x78;%#x78;%#x2E;%#x7A;%#x69;%#x70;%#xA;%#x79;%#x79;%#x79;%#x79;%#x2E;%#x7A;%#x69;%#x70;
于 2012-01-06T21:43:11.893 に答える
3

。を使用した正規表現を使用して、ISEとコンソールの両方で機能する例-split

cd C:\
@"
xxxx.zip
yyyy.zip
"@ -split "`r`n|`n" | % { echo "'$_'"; test-path -path "$_" -EA Stop }
于 2012-01-06T23:50:51.843 に答える
2

これがまさにあなたが実行しているスクリプトであると確信していますか?問題を再現できません。

NTCs>  @"
>> xxxx.zip
>> yyyy.zip
>> "@ -split "`n"|%{echo "'$_'";test-path -path "$_" -ea stop}
>>
'xxxx.zip'
False
'yyyy.zip'
False

更新 ISEおよびコンソールで機能するように、戻り文字に疑問符(0または1回出現)を付けます。

  @"
 xxxx.zip
 yyyy.zip
 "@ -split "`r?`n"|%{echo "'$_'";test-path -path "$_" -ea stop}
于 2012-01-06T20:42:51.690 に答える