Friday, 2 March 2012

Writting logs in Powershell Script

Hello All,

In our project we need to write lots of powershell scripts. So just finding the ways to maintain logs in powershell scripts.

There is a powershell command “Start-Transcript” which allows you to write into a log file.
Syntax:

Start-Transcript <Log File Path>

<Log File Path>: Log file path in which logs are maintained.

After this command we can use either write-output or write-host. Everything used with these commands is get logged into log file.
Whenever if we want to stop the maintain logs there is command “Stop-Transcript”. 

Example:

$logFilePath = “c:\logfile.log”  #Log File Path contains folder name and file name.
Start-Transcript $ logFilePath
write-output "Logging Started”
Stop-Transcript

In this way logs are maintained for powershell script.

Thanks!

Br,
Prasham


No comments: