Firstly, the errors I'm interested in redirecting are NOT PowerShell errors, but those generated by a Python script that is called within my PowerShell script. I have read multiple post on here (and other StackExchange sites, Windows PowerShell scripting guy article, and various other locations. This has lead me to develop the script as it is below, but the output is somewhat odd.

Stop-Transcript | out-null
$ErrorActionPreference = "Continue"

$SnapsToTest = Get-ChildItem C:\Snaps\ -Directory
$ErrorFile = "C:\Tools\Scripts\Errors.txt"
$SnapDir = "C:\Logs"
$LogDir = "\var\log\"
$LogFile = "support.log"

Start-Transcript -path $ErrorFile
Set-Location -Path $SnapDir

$SnapsToTest | ForEach-Object {
        Write-Host $_.Name
        python C:\Tools\Scripts\script1.py $_.Name
        $LogToTest = $_.Name+$LogDir+$LogFile
        python C:\Tools\Scripts\script2.py $LogToTest


Now, if I run script using .\myscript.ps1 > output.text I almost get what I'm after, but not quite.

I end up with two files, output.text and errors.txt.

The Output Text shows all the output generated by the Python script. The Errors Text file shows SOME of the output written to the screen, but not if the Python script crashes an writes out its stack trace, however I DO see this in the terminal, and this I do not understand.

For example, the output I see in the error text file is:

Windows PowerShell transcript start
Start time: 20160429160228
Username: PC\me
RunAs User: PC\me
Machine: PC(Microsoft Windows NT 10.0.14328.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 16844
PSVersion: 5.1.14328.1000
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14328.1000
CLRVersion: 4.0.30319.42000
BuildVersion: 10.0.14328.1000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3

However, what I see in the console is:

C:\Tools\Scripts> .\myscript.ps1 | Out-File output.txt
Traceback (most recent call last):
  File "C:\Tools\Scripts\script1.py", line 446, in <module>
  File "C:\Tools\Scripts\script1.py", line 436, in main
    dba = DBAnalyser(rootdir)
  File "C:\Tools\Scripts\script1.py", line 54, in __init__
    self.osstatus = self._read_osstatus(osstatus_file)
  File "C:\Tools\Scripts\script1.py", line 170, in _read_osstatus
    ret[hostname]["meminfo"] = { "memtotal": memtotal, "memfree": memfree}
UnboundLocalError: local variable 'memtotal' referenced before assignment

I can't see how to get the above error I see in the PowerShell console to show in the Errors.txt file

This is running in Windows 10 Pro 64 bit.

  • you could wrap all code inside a scriptblock and redirect your output like this: &{ # your whole code in here } 2>&1 | out-file C:\somepath\error.txt however, I'm not sure if there's a better solution for this.
    – SimonS
    Apr 30 '16 at 12:04
  • I also posted this to the Scripting Guy forums, and although I had read about the various redirections within PowerShell, it didn't seem to make much sense to me (and to be honest it still doesn't), but instead I invoked the script by using .\myscript.ps1 > output.txt 2>errors2.txt, and I kinda end up with what I want. This gives 3 files, the errors2.txt only contain the Python errors, but not the error.txt file created by Start-Transcript give both the output of Write-Host and the Python script error. The only small glitch is I no longer see the Python errors in the console
    – Swinster
    May 2 '16 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.