Reputation: 116
I have a script in PowerShell that calls LogParser. Early in the script I define the path to the executable and tests its path:
#Define path to log parser executable
$logParser = '\\hostname\logparser22\LogParser.exe'
if (! $(Test-Path $logParser) )
{
Write-Host -ForegroundColor Red "Could not access: $logParser"
return
}
Then later down the script I call LogParser:
$sessionData = & $logParser "SELECT * FROM $logPath where data LIKE `'Contoso\\$user`'" -i:csv -nSkipLines:4 -headers:on -stats:off -o:csv
This works file for awhile during the PowerShell session, but if run enough times it stops working eventually. Doing a little debugging once I've entered a broken shell, the below does not even produce the normal help that comes back when calling LogParser without parameters:
& $LogParser
However if I open a new PowerShell session running the SAME exact command, it works and calls LogParser and I get the standard response from it when not passing any parameters.
What I've come down to is & is broken somehow. Has anyone seen this and know of a fix\workaround?
Upvotes: 1
Views: 2174
Reputation: 763
I had this exact problem. As mentioned in the bug report, the workaround in Powershell V2 is to put [GC]::Collect()
in your loop that writes to the console.
The &<command>
is the preferred way of calling out to an external .exe
. There is an excellent post about how to set up your external calls with arguments.
Upvotes: 1
Reputation: 6828
This might be related to this defect in how PowerShell handles large volumes of console output, which could easily happen with the LogParser. This is supposedly fixed in PowerShell 3.0.
Upvotes: 0
Reputation: 72680
Perhaps you can try using an other way to start you external process with a Cmdlet :
$logParser = '\\hostname\logparser22\LogParser.exe'
$allArgs = ("SELECT * FROM $logPath where data LIKE `'Contoso\\$user`'", "-i:csv", "-nSkipLines:4", "-headers:on", "-stats:off -o:csv")
$ps = Start-Process -FilePath $logParser -ArgumentList $allargs -Wait -Passthru -NoNewWindow -RedirectStandardOutput $tempoutputfile -RedirectStandardError $temperrorfile;
$ps.WaitForExit() # block till exe finish
$ps.ExitCode;
You should have more explanations on the error.
Upvotes: 1