Writing to windows event log using Write-Eventlog
I am changing our powershell scripts to send log data to the windows event log system, and its sort of working.
I use the UpdateLog function (code listed at the bottom of post) to write to a text file, and wanted to edit the function to send the text to the event log as well.
The code writes to the console and to the log file properly, however the text sent to the event log does not expand the variables, in this example it writes ""Upload of {0} failed: {1} $e.FileName, $e.Error"" with out replacing any of the variables with the proper values ( {0}, {1}, $e.FileName, $e.Error ).
I am assuming that its the way I'm passing the data to the UpdateLog function, but not clear why.
Can someone help me figure out what I'm doing wrong?
Thanks
Sal
Example of function call in the application:
[code]
# *** Session.FileTransferred event handler
# Display status of file transfer
# -------------------------------------------
function FileTransferred
{
param($e)
if ($e.Error -eq $Null)
{
Write-Host ("Upload of {0} succeeded" -f $e.FileName)
UpdateLog $logFile "Upload of {0} succeeded $e.FileName"
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f $e.FileName, $e.Error)
UpdateLog $logFile "Upload of {0} failed: {1} $e.FileName, $e.Error"
}
[/code]
[code]
# *** Update log file
function UpdateLog
{
Param(
[Parameter(Mandatory=$True,Position=0)]
[string]$file,
[Parameter(Mandatory=$True,Position=1)]
[string]$msg)
$nowDateTime = Get-Date -Format g
add-content -Path $file -Value "$nowDateTime : $msg" -Force
Write-EventLog -LogName FTP-Process -Source WinSCP_Put -EntryType Information -EventId 1 -Message $msg
}
[/code]
I use the UpdateLog function (code listed at the bottom of post) to write to a text file, and wanted to edit the function to send the text to the event log as well.
The code writes to the console and to the log file properly, however the text sent to the event log does not expand the variables, in this example it writes ""Upload of {0} failed: {1} $e.FileName, $e.Error"" with out replacing any of the variables with the proper values ( {0}, {1}, $e.FileName, $e.Error ).
I am assuming that its the way I'm passing the data to the UpdateLog function, but not clear why.
Can someone help me figure out what I'm doing wrong?
Thanks
Sal
Example of function call in the application:
[code]
# *** Session.FileTransferred event handler
# Display status of file transfer
# -------------------------------------------
function FileTransferred
{
param($e)
if ($e.Error -eq $Null)
{
Write-Host ("Upload of {0} succeeded" -f $e.FileName)
UpdateLog $logFile "Upload of {0} succeeded $e.FileName"
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f $e.FileName, $e.Error)
UpdateLog $logFile "Upload of {0} failed: {1} $e.FileName, $e.Error"
}
[/code]
[code]
# *** Update log file
function UpdateLog
{
Param(
[Parameter(Mandatory=$True,Position=0)]
[string]$file,
[Parameter(Mandatory=$True,Position=1)]
[string]$msg)
$nowDateTime = Get-Date -Format g
add-content -Path $file -Value "$nowDateTime : $msg" -Force
Write-EventLog -LogName FTP-Process -Source WinSCP_Put -EntryType Information -EventId 1 -Message $msg
}
[/code]