Outputting all output from a scheduled psm1 to a file?

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Duffney Duffney 1 year, 5 months ago.

  • Author
    Posts
  • #30875
    Profile photo of Poseyj
    Poseyj
    Participant

    I have a psm1 which is called from a scheduled task that happens around midnight. At this point I'm still developing it and I just want to read everything that's happening(getting backups, zipping things, moving things etc..)

    Is there a way I can basically do what this pseudo code is saying?

    Task scheduler calls test.bat which contains:

    PowerShell.exe -Command "& 'TestScript'" >2&1 | "C:\backup\whats_going_on.txt"

  • #30882
    Profile photo of Don Jones
    Don Jones
    Keymaster

    First, you can do almost exactly that pseudo-code. But it depends on how the script is outputting its text. Anything done with Write-Host, no, you can't redirect or capture. Write-AnhythingElse, yes. Write-Verbose, for example, provided you've enabled verbose output.

  • #30888
    Profile photo of Poseyj
    Poseyj
    Participant

    Makes sense, thanks Don

  • #30923
    Profile photo of Andy
    Andy
    Participant

    Close with the redirection.. but not quite.
    PowerShell.exe c:\scripts\test.ps1 > "C:\backup\whats_going_on.txt" 2>&1

    Also, check out Start-Transcript – it's pretty handy.

  • #31002
    Profile photo of Duffney
    Duffney
    Participant

    If you have any issues with formatting try | out-string, this has come in handy for me a few times.

You must be logged in to reply to this topic.