Reputation: 71
I wrote a script in powershell and I would like to have it write all activities and errors to a log file. I am a powershell newbie so I need anyone's input.
I created a function
function logWrite
{
param ([string]$logstring)
add-content $logfile -value $logstring
}
Instead of using Write-host i use the logWrite but I am getting errors:
Unexpected token 'starting script' in expression or statement. at d:\scripts\tmain.ps1
Appreciate everyone's feedback in advance.
Upvotes: 0
Views: 14560
Reputation: 68341
You can also use the Start-Transcript cmdlet in your script, which will copy all of the console input and output (including Write-Host) to a file.
Upvotes: 2
Reputation: 202032
The easiest way is to redirect output at the point you invoke the script e.g.:
C:\PS> .\myscript.ps1 *> myscript.log
The *>
will redirect all streams to the log file including output, error, warning, verbose and debug. The only output it won't capture is Write-Host output since that is written directly to the host by definition.
Upvotes: 1