bndit
asked on
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
so...question is...how do I log this error to the log file?? Do I need to use the Try/Catch/Finally construct??
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
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
}
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good ideas.
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