Powershell script logging

Posted on 2011-10-27
Last Modified: 2013-11-10

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 ( 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 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

Question by:bndit
    LVL 16

    Accepted Solution

    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
    LVL 16

    Assisted Solution

    by:Bryan Butler
    $? 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:
    LVL 2

    Assisted Solution

    I found that the "try  catch" construct works best for me.
    LVL 16

    Expert Comment

    by:Dale Harris
    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
    LVL 2

    Author Closing Comment

    Good ideas.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now