How to redirect Powershell output from a script run by TaskScheduler and override default width of 80 characters

rated by 0 users
This post has 7 Replies | 4 Followers

Top 150 Contributor
Posts 22
Shantaram Posted: 12-22-2010 5:05 AM

I have a powershell script which is invoked by the task scheduler. The task's definition also redirects the output using >> to a log file. The PowerShell script invokes a C# console program which generates output which is longer than 80 characters in width. However, the resultant output in the log file is wrapping all the output from the C# program to 80- characters. Note that only the output from the C# program is wrapped. All other output from the script (generated by Write-Host) is not wrapped. Also note that this wrapping only occurs when the script is run by the task scheduler.

Here is the script calling chain:

  • The task definition invokes a .cmd script (RunWidthTest.cmd)
  • RunWidthTest.cmd: powershell -File .\RunReports.ps1 >> C:\Log\output.log

  • RunReports.ps1 (snippet):

    Write-Host "======================================================== Running reports for date file for $valueDate"

    .\ReportRunner.exe --ValueDate $valueDate | Out-Default

Note that piping the outout to Out-Default is required to avoid another powershell error that occurs due to the fact that an executable's output is attempting to write to the same stream as the powershell script without powershell being aware of it (Write-Host : The OS handle's position is not what FileStream expected. Do not use a handle simultaneously in one FileStream and in Win32 code or another FileStream. This may cause data loss.)

So in this case here, the string output by the Write-Host line is not wrapped, however, any output generated by ReportRunner.exe is wrapped to 80 characters and it only occurs when run with the task scheduler! The same behaviour occurs if you change the point at which the redirection occurs, ie if you redirect in the definition the task (the task's command line instead of inside the .cmd script).

Any clues as to why this would occur and how to override this width restriction.

Thanks.

 

Not Ranked
Posts 5

I'm wondering if it would make a difference if you pipe to format-list before outputting to the log file.  It sounds like what happens when the output is from format-table.

Scott

Top 10 Contributor
Posts 631
Microsoft MVP
Top Contributor

When you putput text from powershell, the console width is assumed. Since task scheduler runs its own hidden console, the width of that console is used to truncate the text.

You can work by this issue by setting the width yourself. Try using a combination of Format-Table -auto / Format-List -auto and Out-File -width like this:

get-process | Format-Table -Auto | Out-File c:\test.txt -Width 10000

The format-cmdlet with -Auto makes sure the file uses only as much width as is truly needed. The -Width parameter sets the maximum width available.

If you must redirect the output to something else, replace Out-File with Out-String -Stream -Width 10000 and then redirect the result to whereever you want.

Note that multiline strings will still be truncated after the first line.

Not Ranked
Posts 2

How lucky I felt when I found this site & thread.

I have too a problem with truncating lines.

I'm trying to log to a file and it works when run in PS command line but not when I schedule it.

Have tried format-list and this -width but still lines get truncated to 80 chars.

Top 10 Contributor
Posts 631
Microsoft MVP
Top Contributor

so when you append this to your command:

| Format-Table -Auto | Out-File c:\test.txt -Width 10000

and you schedule it, output will be truncated still?

Not Ranked
Posts 2

Doh,

I had wrong version in my scheduled task. So yes, it works now.

Thanks for this -width tip.

Top 10 Contributor
Posts 631
Microsoft MVP
Top Contributor

Wink great!

Not Ranked
Posts 1

THANK YOU SO MUCH! I have been trying to find the answer to this issue for a week now. I didn't realize that task scheduler ran its own hidden console.

My code looks like this:

#Export Dataset Table to a text file       
$DataSet.Tables[0] | Format-Table -Auto | Out-File $rcvrFileName -Width 10000

This worked like a charm.

Eva Yes

Page 1 of 1 (8 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.