In PowerShell, how to write error, warning, write-host-output to a single file?

In PowerShell, how to write an error, warning, write-write output to a single file?

When I execute the external command / write -warning / write-error / write-host, I need all the information that needs to be written to the file.

when I redirect the write error to the log file, it will not show the same content as on the console. He will have other information that I do not need.

Is it possible to prefix the error with ERROR :, warning WARN: in the log file?

+4
source share
3 answers

An easy way to do this is to use cmd.exe, for example:

cmd /c powershell.exe -file <path_to_script> > script.log 
+4
source

Maybe Start-Transcript and Stop-Transcript will do what you want. (PowerShell 2.0)

+2
source

Take a look at PowerShell 2.0: Oisin's customizable and flexible Logger Script module . It should be possible to prefix the message with your custom string ("ERROR", "WARN", ...)

+2
source

Source: https://habr.com/ru/post/1303789/


All Articles