3

I have set of powershell scripts which gets invoke from command prompt. Currently script returns the success or failure code.

Sometime some of the powershell commands in the scripts throws error. I want to capture all the output (logs/message/error). I found that I can use the command like

.\test.ps1 2>&1 | foreach-object {$_.ToString()} | Out-File e:\log.txt

Although this approch logs the error message however following are the problems

  1. It just logs the error message not the line number which comes when I run the same script from the powershell console.

  2. Now there is no output message from powershell. I do not want to hide the output from console & want powershell logging as additional thing.

  3. I have to modify the script invocation command. Is there any standlone tool which can run & capture the output coming on the powershell window.

2 Answers 2

2

I was going to mention Start-Transcript as a great way to do it.

You can also make use of Get-History to find out what commands you entered.

Sign up to request clarification or add additional context in comments.

Comments

2

Just found Start-Transcript and Stop-Transcript commands. We can use it to capture all the output content (error/message/info etc) to file.

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.