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

Powershell script logging

Hello,

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
output:

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
output:

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 {
    [cmdletbinding()]
    Param([string]$computername=$env:computername,
    [string]$Log
    )
    if ($log)
    {
     $loggingPreference="Continue"
     $loggingFilePreference=$log
    }
    Write-log "Starting Command"
    Write-log "Connecting to $computername"
    $b=gwmi win32_bios -ComputerName $computername
    $b
    Write-log $b.version
    Write-Log "finished" $log
}

Open in new window

Function Write-Log {
    [cmdletbinding()]

    Param(
    [Parameter(Position=0)]
    [ValidateNotNullOrEmpty()]
    [string]$Message,
    [Parameter(Position=1)]
    [string]$Path="$env:temp\PowerShellLog.txt"
    )
   
    #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
        #$path
        if ($loggingFilePreference)
        {
            $LogFile=$loggingFilePreference
        }
        else
        {
            $LogFile=$Path
        }
       
        Write-Output "$(Get-Date) $Message" | Out-File -FilePath $LogFile -Append
    }

} #end function

Open in new window

0
bndit
Asked:
bndit
  • 2
  • 2
3 Solutions
 
Dale HarrisCommented:
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 $?

so

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

Dale Harris
0
 
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:
http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/12/powershell-error-handling-and-why-you-should-care.aspx

And logging:
http://jdhitsolutions.com/blog/2011/03/powershell-automatic-logging/
0
 
bnditAuthor Commented:
I found that the "try  catch" construct works best for me.
0
 
Dale HarrisCommented:
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
0
 
bnditAuthor Commented:
Good ideas.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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