PowerShell OutputDataReceived not displaying much info

Advertisement

ddowdle
Joined:
Posts:
4
Location:
Texas

PowerShell OutputDataReceived not displaying much info

Most of the code is from example code. The code works as far as syncing files from server.
I would like to see what is going on on the console I am running this from. The $logPath has the info I would like to print on screen. OutputDataReceived displays text at startup, and then whenever the server times out and attempts reconnect, but no other time.
In the debug log, I can see every file/folder attempted to sync and I can see the actual sync occurring on the server logs.
param (
    $localPath = $PSScriptRoot,
    $remotePath = '/offers/',
    $logPath = $PSCommandPath.replace('ps1', 'log')
)
Add-Type -Path ([IO.Path]::Combine($PSScriptRoot, 'WinSCPnet.dll'))
#####################################################################################################################
###  Session.OutputDataReceived event handler
#####################################################################################################################
function OutputDataReceived {
    param($e)
    Write-Host $e
}
#####################################################################################################################
###  Session.FileTransferred event handler
#####################################################################################################################
function FileTransferred {
    param($e)
     write-host $e
 }
# Main script
 
try {
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol                      = [WinSCP.Protocol]::Ftp
        HostName                      = 'site'
        UserName                      = 'user'
        Password                      = 'pwd'
        FtpSecure                     = [WinSCP.FtpSecure]::Explicit
        TlsHostCertificateFingerprint = 'fingerprint'
    }
 
    $session = New-Object WinSCP.Session
    try {
        # event handlers
        # Will continuously report progress of synchronization
        $session.add_FileTransferred( { FileTransferred($_) } )
        $session.add_OutputDataReceived( { OutputDataReceived($_) } )
 
        $session.DebugLogLevel = -1
        $session.SessionLogPath = $logPath
 
        # Connect
        $session.Open($sessionOptions)
        $synchronizationResult = $session.SynchronizeDirectories(
            [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False, $False)
 
        # Throw on any error
        $synchronizationResult.Check()
    }
    finally {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch {
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,506
Location:
Prague, Czechia

Re: PowerShell OutputDataReceived not displaying much info

I've tried your code and it prints all synchronized files for me.
winscp> synchronize remote   -nopermissions -preservetime -transfer="binary" -criteria="time"  -- "c:\synctest" "/synctest"
Comparing...
Local 'c:\synctest' => Remote '/synctest'
Synchronizing...
Local 'c:\synctest' => Remote '/synctest'
c:\synctest\aaa.txt |            3 B |    0,0 KB/s | binary | 100%
c:\synctest\bbb.txt |            3 B |    0,2 KB/s | binary | 100%
c:\synctest\ccc.txt |            3 B |    0,4 KB/s | binary | 100%
Though in general, the right API to check for changes is the $synchronizationResult (SynchronizationResult class).

Reply with quote

ddowdle
Joined:
Posts:
4
Location:
Texas

My code is a synchronize local yours is a synchronize remote.
For files that are actively being "synced"(downloaded from remote) – my code does actually produce this same(ish) output
I am not trying to check for changes, I am attempting to see some sort of idea of progress, like what directory and/or file is being compared
If I run
synchronize local   -nopermissions -preservetime -transfer="binary" -criteria="time"  -- "I:\alldata\inetpub\wwwroot\offers" "/offers/"
within an actual WinSCP console window I get something like the following
"Local 'I:\alldata\inetpub\wwwroot\offers\ALAE' <= Remote '/offers/ALAE'"
over and over per directory.
If I create debug log file at the lowest log level(-1), I also get this list. I suspect that because the actual console is writing the progress over and over on the same line that OutputDataReceived is not being fired or recognized correctly.

Reply with quote

martin
Site Admin
martin avatar

Yes, indeed the ephemeral progress output is not fed to the OutputDataReceived. That progress information is not exposed anyhow in the .NET assembly API.

Reply with quote

Advertisement

martin
Site Admin
martin avatar

Unfortunately, as I wrote, "That [comparison] progress information is not exposed anyhow in the .NET assembly API."

Reply with quote

ddowdle
Joined:
Posts:
4
Location:
Texas

What about without the .NET API? If I run
WinSCP.com /script=script.txt 1> x.x 2>&1
The console does not show the "Local '\offers\wip' <= Remote '/offers/wip'" anymore but neither does x.x.

Reply with quote

martin
Site Admin
martin avatar

This is basically what .NET assembly does internally. In other words, it's not limitation of the .NET assembly. It's how WinSCP console works.

Reply with quote

Advertisement

You can post new topics in this forum