3

If I have a powershell script say called caller.ps1 which looks like this

.\Lib\library.ps1

$output = SomeLibraryFunction

where library.ps1 looks like the following

function SomeLibraryFunction()
{
   Write-Output "Some meaningful function logging text"
   Write-Output "return value"
}

What I'd like to achieve is a way in which the library function can return it's value but also add some logging messages that allow the caller to process those internal messages as they see fit. The best I can think of is to write both to the pipeline and then the caller will have an array with the actual return value plus the internal messages which may be of use to a logger the calling script has.

Am I going about this problem the right way? Is there a better way to achieve this?

1
  • Perhaps Tee-Object would be of use Commented Aug 28, 2014 at 15:54

3 Answers 3

5

It's usually not a good idea to mix logging messages with actual output. Consumers of your function then have to do a lot of filtering to find the object they really want.

Your function should write logging messages to the verbose output stream. If the caller wants to see those messages, it can by specifying the -Verbose switch to your function.

function SomeLibraryFunction()
{
    [CmdletBinding()]
    param(
    )

    Write-Verbose "Some meaningful function logging text"
    Write-Output "return value"
}

In PowerShell 3+, consumers can redirect your verbose messages to the output stream or a file:

# Show verbose logging messages on the console
SomeLibraryFunction -Verbose 

# Send verbose logging messages to a file
SomeLibraryFunction -Verbose 4> verbose.txt

# redirect verbose message to the output stream
SomeLibraryFunction -Verbose 4>&1 

Other options include:

  • Writing to a well-known file
  • Writing to the Event Log
  • Use Start-Transcript to create a log of the session.
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, I knew of Write-Verbose but I didn't know I could filter it out of the pipeline like that from the other Write-Output value. Cheers
2

Something like Tee-Object might be helpful to you

function SomeLibraryFunction()
{
    $filepath = "C:\temp\log.txt"
    write-output "Some meaningful function logging text" |  Tee-Object -FilePath $filepath -Append
    write-output "return value" |  Tee-Object -FilePath $filepath -Append
}

For more information on Tee-Object look here

You could use an If statement based on a variable like $logging=$true but i could see that getting messy.

Another Approach

If you are looking for more of an optional solution then maybe you could use something like this Start-Transcript and Stop-Transcript which creates a record of all or part of a Windows PowerShell session in a text file.

function SomeLibraryFunction()
{
    write-output "Some meaningful function logging text"
    write-output "return value"
}


$logging = $True
If ($logging){
    Start-Transcript -Path C:\temp\log.txt -Append
}

SomeLibraryFunction

If ($logging){
    Stop-Transcript
}

This would just show that you could toggle the Start and Stop. You could even set the switch with a paramater passed to the script if you chose.

NOTE The output might be more that you are looking for but at least give it a try. Also, this will not work in the Powershell ISE as you will get an error Start-Transcript : This host does not support transcription.

2 Comments

I wanted the library function to not have to be coupled to any logging implementation. I wanted the caller to be able to decide what it does with the output of the function.
Made an update using another approach. If i once again misinterpeted your need let me know.
1

Another way to do this would be to return a compound object that includes the results and the log information. This is then easy to pick apart.

function MyFunc
{

    # initialize
    $result = $null
    $log = @()

    # write to the log
    $log += "This will be written to the log"
    $log += "So will this"

    # record the result
    $result = $true

    # return result and log combined into object
    return New-Object -TypeName PSObject -Property @{ result = $result; log = $log -join "`r`n" }

}

# Call the function and get the results
$MyFuncResult = MyFunc

# Display the returned result value
Write-Host ( "MyFunc Result = {0}" -f $MyFuncResult.Result )

# Display the log
write-host ( "MyFunc Log = {0}" -f $MyFuncResult.Log )

Alternatively, if you want to avoid the object, pass in a log variable by reference. The function can write to the log variable and the changes will be visible in the calling scope. To do this, you need to add the [ref] prefix to the function definition AND the function call. When you write to the variable in the function you need to refer to the .value property.

function MyFunc2 ([ref]$log)
{

    # initialize
    $result = $null

    # write to the log
    $log.value += "`r`nThis will be written to the log"
    $log.value += "`r`nSo will this"

    # record the result
    $result = $true

    # return result and log combined into object
    return $result

}

# Call the function and get the results
$log = "before MyFunc2"
$MyFuncResult = MyFunc2([ref]$log)
$log += "`nafter MyFunc2"

# Display the returned result value
write-host ( "MyFunc2 result = {0}" -f $MyFuncResult )

# Display the log
write-host ( "MyFunc2 Log = {0}" -f $Log )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.