1

The issue I'm facing is logging the output of a function being called inside the powershell workflow into a logfile.

Tried using Out-File but it is not supposed to be used for logging a specific function call.

Any help on this would be great. Thanks in advance.

Attached is the sample script regarding the issue:

function test1{
    param (
    [String] $FileName)
    Write-Host "$FileName test1"
}

WorkFlow Sample { 
    $Files = Get-ChildItem 'C:\Users\Downloads\testDir' 
    Foreach -parallel ( $File in $Files ) {
        test1($File) #This function output should be logged
    }  
}

Sample
1

2 Answers 2

0

about_redirection on Microsoft docs has quite a bit of information.

One of the problems you got is that you're trying to redirect Write-Host. Depending on your PowerShell version you might have more or less options available. Prior to version 5 you had to use something like *> or Tee-Object where applicable. With version 5, there are some new options available:

Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information stream. This enables the capture or suppression of data written using Write-Host while preserving backwards compatibility.

The $InformationPreference preference variable and InformationAction common parameter do not affect Write-Host messages. The exception to this rule is -InformationAction Ignore, which effectively suppresses Write-Host output. (see "Example 5")

An easy option would be to use Write-Output instead of Write-Host. You won't be able to color it (easily) but in your current example you don't need to. In addition, depending on your use case, you could look into using Start-Transcript which might also be available.

Sign up to request clarification or add additional context in comments.

Comments

0

To create a file you can do this: ($outcome being the variable containing the outcome of that function)

$outcome | Set-Content 'file.txt'`

or

$outcome > 'file.txt'

If there already is a file and you want to append:

$outcome | Add-Content 'file.txt'

1 Comment

Thanks Marthyn, this works for Write-Output and return statements in the function but in my case I even have Write-Host statements in the function which are not logged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.