我使用Windows 2003 Server x86,Powershell 1.0,並有以下突破性問題。當我試圖下面執行腳本,我得到一個錯誤:PowerShell:如何分配結果,從foreach返回到變量
Error "=" operator: System error. (Fehler beim "="-Operator: Systemfehler.)
+ $data = <<<< Get-Content $log | % {
下面是這個腳本:
$log = "C:\log file.txt"
$linePrefix = "PROCESS THIS LINE"
$trimStart = 25 #chars to ignore in the beginning
$started = 0
$data = Get-Content $log | % {
if ($_.length -ge $trimstart) {
$linedata = $_.substring($trimstart);
$timesline = $linedata.startswith($lineprefix);
if ($started -eq 0 -and $timesline) {
$started = 1;
}
if ($started) {
if ($timesline) {
$line = $linedata.substring($lineprefix.length).trimstart();
if ($line.contains(": ")) {
write-output $line
}
} else {
break;
}
}
}
}
不過,如果我沒有$data =
分配執行它,它完美的作品和返回我的預期值。我也試過以下語句
$data = (...)
$data = $(...)
並用表達式聲明函數但沒有成功。
請給我一個提示,爲什麼它會發生?
更新:我已經試過分配新建分配FY跡象之前和之後刪除空格,得到了類似的錯誤,但現在PowerShell的不喜歡$data=G
串
Error "=" operator: System error (Fehler beim "="-Operator: Systemfehler.)
At C:\kosmo\scripts\ps\test.ps1:60 Symbol:7
+ $data=G <<<< et-Content $log | % {
你有沒有嘗試過在一行中執行它? – 2013-02-12 15:53:04
yeap。我爲它準備了一行代碼:'$ data = Get-Content $ log | %{if($ _。length -ge $ trimstart){$ linedata = $ _。substring($ trimstart); $ timesline = $ linedata.startswith($ lineprefix);如果($開始-eq 0 - 和$ timesline){$開始= 1; }; ($ start){if($ timesline){$ line = $ linedata.substring($ lineprefix.length).trimstart(); if($ line.contains(「:」)){write-output $ line}} else {break; }}}}'。仍然沒有 – kosmo 2013-02-12 15:54:55
試過$ data =&{...}? – mjolinor 2013-02-12 15:55:48