Topic "Writing to windows event log using Write-Eventlog"

Author Message
salp
[View user's profile]

Joined: 2016-03-25
Posts: 1
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]
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
You have to use the same syntax as you are using in Write-Host

UpdateLog $logFile ("Upload of {0} failed: {1}" -f $e.FileName, $e.Error)
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License