Script didn't save the downloaded files correct

Advertisement

piti87
Joined:
Posts:
1

Script didn't save the downloaded files correct

I have the following script (PowerShell)

param (
    $localPathImport = "C:\application\Data\12345\Import\SAP\PROJEKTE\",
    $remotePath = "/test/download/*"
)
 
try
{
    # WinSCP .NET assembly laden
    Add-Type -Path "WinSCPnet.dll"
 
    # Session Einstellungen
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "host.net"
        UserName = "user_ftp"
        SshHostKeyFingerprint = "xxx"
        SshPrivateKeyPath = "C:\application\AddOns\File_upload\ssh\private_key_cust01_new.ppk"
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Verbinden
        $session.Open($sessionOptions)
 
        # Download
        $transferResult = $session.GetFiles($remotePath, $localPath)
                
        foreach ($transfer in $transferResult.Transfers)
        {
            # Download erfolgt oder Error?
            if ($transfer.Error -eq $Null)
            {
                $zeit = Get-Date >> C:\application\AddOns\File_upload\download_sap_projektstamm_log.txt
                Write-output "Download von $($transfer.FileName) erfolgt!" >> C:\application\AddOns\File_upload\download_sap_projektstamm_log.txt
            }
            else
            {
                $zeit = Get-Date >> C:\application\AddOns\File_upload\download_sap_projektstamm_error.txt
                Write-output "Download von $($transfer.FileName) fehlgeschlagen: $($transfer.Error.Message)" >> C:\application\AddOns\File_upload\download_sap_projektstamm_error.txt
            }
        }
                
        # löschen aus Verzeichnis                
        $session.RemoveFiles($remotePath)
    }
    finally
    {
        # Verbindung trennen
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    $zeit = Get-Date >> C:\application\AddOns\File_upload\download_sap_projektstamm_error.txt
    Write-output "Error: $($_.Exception.Message)" >> C:\application\AddOns\File_upload\download_sap_projektstamm_error.txt
    exit 1
}

The script does not save the files in the $localPathImport.
The three files on the FTP server are:
project0000000000001319
project0000000000001320
project0000000000001321
It makes a file named %22 in the directory, where is the winscp.exe located. And in this file %22, the content of the three files are combined.

Any ideas?

Reply with quote

Advertisement

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

Re: Script didn't save the downloaded files correct

You never use $localPathImport in your code. Maybe you wanted to use it, where you actually use undefined $localPath.

Also, what do you expect the $zeit = Get-Date >> ... to do? Imo, the $zeit = Get-Date does not output anything. Possibly you meant to do Get-Date >> ... only.

If you need further help, please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

Advertisement

You can post new topics in this forum