PowerShell Logging.

Tagged: 

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Robert Pearman Robert Pearman 3 years, 9 months ago.

  • Author
    Posts
  • #10274
    Profile photo of Robert Pearman
    Robert Pearman
    Participant

    Hi Guys, newbie here on the forums.

    I looked for an answer to my question first, so this is more of a follow up to this post:

    I use PowerShell quite a lot, but I am no means experienced with scripting/coding.

    I have built, what I would class as crude PowerShell applications. A series of functions and a menu system built into a PS1 file.

    What I have not been able to yet master is logging.

    Just this morning I have found the ‘start-transcript’ command, which appears to be exactly what I want.

    However, inside my application I suppress the errors so the user has a more elegant experience.

    My thinking here is that a transcript, is essentially going to capture whatever is output to the console, and if i have
    $ErrorAction = "SilentlyContinue" the errors wont be there to be transcripted.

    What I would like to do is run a transcript but also capture the errors, whilst not displaying the errors out to the console.

    Is that possible? Hopefully that makes sense?

    Many thanks in advance if you are able to help out!

  • #10275
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    I don't think you can.

    using the transcript cmdlets toggles the transcript functionality pretty much on or off
    -Erroraction silentlycontinue will pretty much suppress your errors and you won't see anything

    I think you'll have to log information separately – either using write-eventlog to add it to an event log – I'd recommend creating your own
    or use add-content to write the data to a text file

    I think you'll end up with something like

    try {
    do-something -erroraction stop
    log success
    }
    catch
    {
    log error
    }

  • #10276
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    If you have some control over how your script is called, you could also use a redirection operator to send the Error stream to a file. It would have to be a separate file from the rest of your logging, though (to avoid "file in use by another process" errors):

    .\SomeScript.ps1 2>> .\SomeScriptErrorLog.txt

  • #10278
    Profile photo of Robert Pearman
    Robert Pearman
    Participant

    Ok so that sounds doable.

    I have a shortcut on the desktop of a server that launches Powershell.exe -command xxxx

    What do i need to do within the script to get the errors output to a seperate file?

    Rob.

  • #10279
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You could add that same redirection operator to the shortcut. PowerShell.exe -command xxxx 2>> .\SomeErrorLog.txt

  • #10280
    Profile photo of Robert Pearman
    Robert Pearman
    Participant

    2>> .\SomeErrorLog.txt

    What exactly does this tell Powershell to do? (i understand the file name bit ;o) )

  • #10281
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    It redirects the Error stream to a file (appending to the file if it already exists). "Get-Help about_Redirection" has the details how those operators all work.

  • #10285
    Profile photo of Robert Pearman
    Robert Pearman
    Participant

    OK i will check it out, many thanks.

You must be logged in to reply to this topic.