I made a small PowerShell wrapping script for the WinSCP module that suites my needs. Like in the example for synchronizing local and remote folders mine uses a file transferred event to log session events.
$fileTransferedEvent = {Receive-FileTransferredEvent $_}
$session.add_FileTransferred($fileTransferedEvent)
The example disposes the session when it is done. My logic keeps the session open until I choose to be done with it so I can use the same session in other cmdlet call.
This creates an issue if a other cmdlets (or even calling the same one again) also use add_FileTransferred(). The event gets added again which triggers X times the output where X is how many times the method is called.
I found a partner method called remove_FileTransferred() but I do not know how to use it
Name MemberType Definition
---- ---------- ----------
add_FileTransferred Method void add_FileTransferred(WinSCP.FileTransferredEventHandler value)
remove_FileTransferred Method void remove_FileTransferred(WinSCP.FileTransferredEventHandler value)
They both accept the same values so I thought maybe I could pass it a definition of the the handler but that does not seem to work i.e. $session.remove_FileTransferred($fileTransferedEvent) did not do the job. Nor does it error either.
How can I remove file transferred event handlers on Winscp.Session objects?