[Powershell] FileTransferProgress

Advertisement

burnmaster
Joined:
Posts:
2
Location:
Germany

[Powershell] FileTransferProgress

Hi,

i'm trying to automate my Customer File Transfers.
Everything works fine except the File Transfer Event.
I used the Example on your Page, but it doesn't work, it looks like the event didn't get fired on Transfer.

I'm using WinSCP Version v5.2.4 (beta) and the equivalent .net assembly / COM library (winscp524automation).

Could someone please take a look at my Script?

This is my Script:

# Config #
[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP_beta\WinSCPnet.dll")
$script:startdir = Split-Path $MyInvocation.MyCommand.Path
[xml]$XML = get-content "$($script:startdir)\SFTP.conf"

# Script #
function LOG($customer, $logMessage) {
   $logFile = "$($script:startdir)\Logs\$(Get-Date -format "MMyyyy")_$($customer)-SFTP.log"
   Add-content $logFile -value "[$(Get-Date -format "dd.MM.yyyy HH:mm:ss")] $logMessage"
    Write-Host "[$(Get-Date -format "dd.MM.yyyy HH:mm:ss")] $logMessage"
}

$script:lastFileName = $Null

function FileTransferProgress
{
    Param($e)
 
    # Print transfer progress
    Write-Host "TEST"
    Write-Host -NoNewline ("`r{0} ({1:P0})" -f $e.FileName, $e.FileProgress)
 
    # Remember a name of the last file reported
    $script:lastFileName = $e.FileName
}

foreach ($customer in $XML.SFTP.Customer) {
   
    $ftpOptions = New-Object WinSCP.SessionOptions
   $ftpOptions.Protocol = [WinSCP.Protocol]::Sftp
   if (!$customer.PORT) {$ftpOptions.PortNumber = "22"} else {$ftpOptions.PortNumber = $customer.PORT}
   $ftpOptions.HostName = $customer.IP
   $ftpOptions.UserName = $customer.USER
   $ftpOptions.Password = $customer.PASS
   $ftpOptions.SshHostKeyFingerprint = $customer.FINGERPRINT
   #$ftpOptions.Timeout = New-TimeSpan -Seconds 10
    $ftp = New-Object WinSCP.Session
   try {
        ## TransferProgress Event ##
        Register-ObjectEvent -inputObject $ftp -eventName FileTransferProgress -Action {FileTransferProgress($event.sourceEventArgs)}
      
        $ftp.Open($ftpOptions)
      LOG $customer.name "Connecting to $($customer.name) -> $($ftpOptions.HostName):$($ftpOptions.PortNumber)"
      
      if ($ftp.FileExists($customer.REMOTEDIR)) {
            LOG $customer.name "$($customer.REMOTEDIR) exists -- Starting download($($customer.MASK))"
            ## List Files
            foreach ($file in $ftp.ListDirectory($customer.REMOTEDIR).Files | Where-Object {$_.name -like $customer.MASK}) {
               LOG $customer.name "$($file.Name) ($($file.Length))"
            }
            ##
            $ftp.GetFiles("$($customer.REMOTEDIR)/$($customer.MASK)", $customer.LOCALDIR).Check()
            LOG $customer.name "File Transfer finished Successfully"
      } else {
            LOG $Customer.name "Remote Direcotry $($customer.REMOTEDIR) doesn't exist -- Closing Connection"
      }
    }
    catch [Exception] {
      LOG $customer.name "ERROR: $($_.Exception.Message)"
    }
    finally {
        $ftp.Dispose()
    }
}

My SFTP.Conf
<SFTP>
   <CUSTOMER name="Test">
      <IP>192.168.0.1</IP>
      <PORT>22</PORT>
      <USER>Testuser</USER>
      <PASS>Testpass</PASS>
      <REMOTEDIR>/TEST</REMOTEDIR>
      <LOCALDIR>C:\Testcustomer\Data\</LOCALDIR>
      <MASK>*.pdf</MASK>
      <FINGERPRINT>ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx</FINGERPRINT>
   </CUSTOMER>
</SFTP>

The Console Result:
GAC    Version        Location
---    -------        --------
False  v2.0.50727     C:\Program Files (x86)\WinSCP_beta\WinSCPnet.dll

Module        : __DynamicModule_5452ae7c-33be-48a9-9620-8d47cb93c7c6
StatusMessage :
HasMoreData   : False
Location      :
Command       : FileTransferProgress($event.sourceEventArgs)
JobStateInfo  : NotStarted
Finished      : System.Threading.ManualResetEvent
InstanceId    : d1ee00b2-233f-4894-99e6-11f92eedc11c
Id            : 3
Name          : 2a30f000-90a4-4d1d-be8c-cc3a645a8edf
ChildJobs     : {}
Output        : {}
Error         : {}
Progress      : {}
Verbose       : {}
Debug         : {}
Warning       : {}
State         : NotStarted

[19.09.2013 08:46:22] Connecting to Test -> 192.168.0.1:22
[19.09.2013 08:46:22] /TEST exists -- Starting download(*.pdf)
[19.09.2013 08:46:25] test.pdf (26055)
[19.09.2013 08:46:25] File Transfer finished Successfully

Thanks,
burn

Reply with quote

burnmaster
Joined:
Posts:
2
Location:
Germany

After searching your Website, I found another example to define event handling functions.

i just removed that line:
Register-ObjectEvent -inputObject $ftp -eventName FileTransferProgress -Action {FileTransferProgress($event.sourceEventArgs)}

and used this one instead:
$ftp.add_FileTransferProgress( { FileTransferProgress($_) } )

Now it's working.
Thans anyway.
burn

Btw. you can close this Thread, if you want.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
27,459
Location:
Prague, Czechia

I have too realized that Register-ObjectEvent have problems. I have updated the examples to use the .add_Event method.
_________________
Martin Prikryl

Reply with quote

Advertisement

You can post new topics in this forum