I’m using Windows 2003 Server x86, Powershell 1.0 and have following head-breaking problem. When I’m trying to execute script below, I’m getting an error:
Error "=" operator: System error. (Fehler beim "="-Operator: Systemfehler.)
+ $data = <<<< Get-Content $log | % {
Here is this script:
$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;
}
}
}
}
However, if I execute it without $data = assignment, it works perfectly and returns me expected values. I’ve also tried following statements
$data = (...)
$data = $(...)
and declaring a function with expression but without success.
Could you please give me a hint why it does happen?
UPDATE: I’ve tried to remove spaces before and after assigment sign and got similar error, but now powershell didn’t like $data=G string
Error "=" operator: System error (Fehler beim "="-Operator: Systemfehler.)
At C:\kosmo\scripts\ps\test.ps1:60 Symbol:7
+ $data=G <<<< et-Content $log | % {
Thanks all to your hints! Pity, but nothing suggested solved my problem indeed.
Most helpful was hint from latkin about break behavior. After some reading I found out, that in Foreach-Object cmdlet, which is aliased by %, break or continue statements stop whole script
Output shows only
before getting result. From there I’ve made a conclusion that there is no way to do what I want in Powershell 1.0! So I found a way to install Powershell 2.0 on my good old 2003 server (tip how to uninstall Powershell 1.0 before; btw, my Windows-update had name KB926140-v5).Ok, error has gone. But I still have no result I want to have?! At the end of my journey I found very useful post and have written script below
And got an answer:
Explanation
If I’d used
foreachstatement, I could use break with estimated behavior, but I’m working with large log files and foreach-statement reads whole file into memory, while ForEach-Object cmdlet processes lines one by one. I’m not sure about Get-Content cmdlet, maybe it also reads whole file to return it, but I’ve written down my own cmdlet Get-Content-Reversed to read lines from the end of file and that’s why I’m pretty sure it doesn’t