• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 930
  • Last Modified:

Powershell script logging


I'd like to log what my scripts *actually* do as opposed to echoing message strings....I came across a piece of code that seems promising (http://jdhitsolutions.com/blog/2011/03/powershell-automatic-logging/). In this code, the author only displays message strings as opposed to success vs failure command completion....I'm assuming that I need to use some type of error handling and based on that display the output...but I'm not sure how to modify this code to accomplish what I want.

here's what happens:

if I run: TryMe -computername localhost -log c:\utils\sample.txt -verbose

10/27/2011 14:15:15 Starting Command
10/27/2011 14:15:15 Connecting to SERVER2
10/27/2011 14:15:16 INTEL  - 6040000
10/27/2011 14:15:16 finished

This output is nice, but not the output of the actual commands...I'd want to check success/failure on the command (gwmi win32_bios -ComputerName $computername) and write to the log based on that...

if I run: TryMe -computername 'SERVER10' -log c:\utils\sample.txt -verbose

10/27/2011 14:04:01 Starting Command
10/27/2011 14:04:01 Connecting to SERVER10
10/27/2011 14:04:09 finished

 Error when contacting server
so...question is...how do I log this error to the log file??  Do I need to use the Try/Catch/Finally construct??
# dot source Write-Log function

Function TryMe {
    if ($log)
    Write-log "Starting Command"
    Write-log "Connecting to $computername"
    $b=gwmi win32_bios -ComputerName $computername
    Write-log $b.version
    Write-Log "finished" $log

Open in new window

Function Write-Log {

    #Pass on the message to Write-Verbose if -Verbose was detected
    Write-Verbose $Message
    #only write to the log file if the $LoggingPreference variable is set to Continue
    if ($LoggingPreference -eq "Continue")
        #if a $loggingFilePreference variable is found in the scope
        #hierarchy then use that value for the file, otherwise use the default
        if ($loggingFilePreference)
        Write-Output "$(Get-Date) $Message" | Out-File -FilePath $LogFile -Append

} #end function

Open in new window

  • 2
  • 2
3 Solutions
Dale HarrisProfessional Services EngineerCommented:
Call me crazy, but does "Start-Transcript" and "Stop-Transcript" help you?

That way, you could output everything to the screen you wanted to see, and it would log any errors.

Then you could parse the errors after the transcript had been written.

Does that help at all?

The other way is we could do the whole try/catch/finally like you're saying.
The Variable to use if I remember correctly is $?


if ($?){Do something if we have an error}

Dale Harris
Bryan ButlerCommented:
$? tells if the last command was successful.  Start-transcript as Dale mentions is probably the way to go.  If you want more info, maybe one of these links can help;

Good link about error handling:

And logging:
bnditAuthor Commented:
I found that the "try  catch" construct works best for me.
Dale HarrisProfessional Services EngineerCommented:
Sounds great!  Good luck!

This question has been open for a while.  It's best if we close it out and get it out of the way for future questions to be asked regarding the same issue.

Best Regards,

Dale Harris
bnditAuthor Commented:
Good ideas.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now