我使用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