Reputation: 43
I am writing a PowerShell script that has to invoke a legacy Perl Script and display the output of this long running Perl script in real time to PowerShell as I would normally view in a Windows command prompt or interactive Putty SSH session. My requirement is to write the output of the Perl script to a log file in real time and display the results to the PowerShell in real time.
My problem is that once the Perl script finishes running, all of the results are then returned to PowerShell or my log files.
Here is a simplified sample of my PowerShell code.
#Test.ps1
$env:Path = "C:\Dev\perl583\bin;" + $env:Path
$WORKING_PATH="C:\Dev\scripts"
perl $WORKING_PATH\LegacyScript.pl *>> C:\Dev\results\realtime_output.txt
exit $LASTEXITCODE
Here is a sample Perl Script that simulates a long running operation invoked by the PowerShell script:
#LegacyScript.pl
for( $a = 1; $a < 6; $a = $a + 1 ){
print "New value of a: $a\n";
sleep(10)
}
Upvotes: 0
Views: 1075
Reputation: 43
After a day of researching I think I found the answer to my own question courtesy of PerlMonks.org . It is perl in the way it buffers the output (line vs. block). Once I set "$| = 1;" in my Perl script, I got the expected results from both the PowerShell itself and PowerShell's file redirection. Here is the site for those that may run into this problem integrating legacy scripts as in my case.
http://www.perlmonks.org/?node_id=305801 "By setting $| as Anonymous Monk indicated, you force the output to be flushed after each write." - Direct Quote from this Website that solved my problem.
Upvotes: 3