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

Author Message
[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?


Example of function call in the application:
# *** Session.FileTransferred event handler
# Display status of file transfer
# -------------------------------------------

function FileTransferred

if ($e.Error -eq $Null)
Write-Host ("Upload of {0} succeeded" -f $e.FileName)
UpdateLog $logFile "Upload of {0} succeeded $e.FileName"
Write-Host ("Upload of {0} failed: {1}" -f $e.FileName, $e.Error)
UpdateLog $logFile "Upload of {0} failed: {1} $e.FileName, $e.Error"

# *** Update log file
function UpdateLog


$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

[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 27106
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)

You can post new topics in this forum


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!


About donations

$9   $19   $49   $99

About donations


WinSCP Privacy Policy

WinSCP License