0

I have a script that is being called a certain way, i cannot change it. I want to redirect the error messages from the entire script to a file. How does this work?

I did this, which works, except the errors are complete gone:

$ErrorActionPreference="SilentlyContinue"
Start-Transcript -path C:\output.txt -append

write-host "test"

some nonsence that creates as error

Stop-Transcript

Thanks

3
  • Do you have to use Start-Transcript? What is the certain way your script is being called? Is it a ps1 being called from the command line? Commented Oct 22, 2014 at 10:14
  • Does the script call native commands? Start-Transcript will not actually record errors generated by native commands, even though they are shown in the commandline window. You can work around this by piping the call to out-host. For bug reference see here: connect.microsoft.com/PowerShell/feedback/details/315875/… Commented Oct 22, 2014 at 10:27
  • Hello. Im not sure what you mean by native commands. The script is being launched from a bat file, but to several machines at the same time. Would this not result in one log file with all machines logs in it? Commented Oct 22, 2014 at 11:06

4 Answers 4

2

Would this work for you?

try{
    # your code goes here
}
catch{
    $exception = $_.Exception.Message
    Out-File -FilePath 'c:\myscript.log' -Append -InputObject $exception
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hello. This seems to work, but is it correct the script ends if ONE error message is captured? It should just continue. Is it possible to also log all commands tids this file? Like the transcript does?
Yes. That is correct. The moment an exception occurs the script will skip to the catch. But you could wrap all commands in separate try-catch pairs. It will not capture all commands like transcript does. Perhaps you need to combine the two.
1

Since Powershell 5.1, there are "Redirectable output streams" with bash-like similar syntax, e.g.

Write-Output "good" > success.txt
Write-Error "failed" 2> error.txt
Write-Warning "attention" 3> warning.txt

You can also redirect error to success stream.

./script.ps1 2>&1 > out.log 

Alternatively, to redirect all streams when running a script or command, use

.\script.ps1 *> $null # suppress all output

See about_Redirection for more details.

Comments

0

I needed something like this before, but i could change how the file is being called etc.

If its launched from commandline as mentioned, by arco444, you can use the below to pipe everything to one file:

PowerShell.exe -Command "& {c:\myscript.ps1}" > c:\myscript.log

Comments

0

Beware using Start-Transcript. You might get the following error:

Start-Transcript : This host does not support transcription.
At D:\Apps\TranscriptTest\TranscriptTest.ps1:3 char:1
+ Start-Transcript -Path Log.txt -Append
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [Start-Transcript], PSNotSupportedException
    + FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.StartTranscriptCommand

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.