Need help to Log output on screen and to Text file

Welcome Forums General PowerShell Q&A Need help to Log output on screen and to Text file

This topic contains 1 reply, has 2 voices, and was last updated by

 
Member
4 years, 9 months ago.

  • Author
    Posts
  • #20354

    Participant
    Topics: 7
    Replies: 8
    Points: 0
    Rank: Member

    Hi

    Have a function which I call during my script and use to write output to the screen, I would also like to out put the same information to a log file.

    I have tried to pipe the output to a file using out-file but I dont see the log file created nor an error. I see the output on screen but not a log file

    Function Logit(){
    $TextBlock1 = $args[0]
    $TextBlock2 = $args[1]
    $TextBlock3 = $args[2]
    $Stamp = Get-Date
    Write-Output "[$Stamp] [$Section – $TextBlock1]" | Out-file -Append -FilePath 'C:\Script\Log.txt'
    }

  • #20355

    Member
    Topics: 9
    Replies: 2322
    Points: 0
    Rank: Member

    You can use the Start-Transcript command for this, but it has some limitations that are being addressed in PowerShell v5 (only works in the powershell.exe host, only one transcript can be active at a time.) To get around these limitations, I wrote the PowerShellLogging module, which you can download from https://gallery.technet.microsoft.com/Enhanced-Script-Logging-27615f85 .

The topic ‘Need help to Log output on screen and to Text file’ is closed to new replies.