Search code examples
powershellloggingverbositypowershell-module

Capture Verbose out from Powershell function


I am attempting to call a Powershell function from another Powershell function. The function I am calling has a lot of verbose output and I need to capture them to a file. Since I am calling said function multiple times, I need the verbose outputs appended to the end of the file.

I attempted the following

Invoke-TeardownWorkflow -WorkflowID $i -PostTeardownAction=@{$IPList[$i]="Reconnect-PrimaryNetwork"} 9 >>C:\TeardownLog.log

However, the code didn't work as expected and kept repeating

A positional parameter cannot be found that accepts argument '9'.

The function is source-controlled (so, I can't touch the function.). Also these functions are all part of PSM1 files. The Teardown.log file has some entries prior to the execution of the code mentioned above.


Solution

  • You can use 4>

    Invoke-TeardownWorkflow -WorkflowID $i -PostTeardownAction=@{$IPList[$i]="Reconnect-PrimaryNetwork"} 4>C:\TeardownLog.log
    

    More about redirection here, here is an extract:

    4>        Sends verbose output to    Import-Module * -Verbose 4> Verbose.txt
              the specified file.
    
    4>>       Appends verbose output     Import-Module * -Verbose 4>> Save-Verbose.txt
              to the contents of the 
              specified file.