简体   繁体   中英

WinSCP Script to Loop through Files

I am using the following slightly modified script (from https://winscp.net/eng/docs/script_local_move_after_successful_upload ) to upload files to an SFTP site on AWS...and in a PRD env I will have to have this run through approx 0.5M small files...

param (
    $localPath = "C:\FTP\*.DAT",
    $remotePath = "/",
    $backupPath = "C:\FTP\Complete\"
    )

try
{
    # Load WinSCP .NET assembly
    #Add-Type -Path "WinSCPnet.dll"
    $ScriptPath = $(Split-Path -Parent $MyInvocation.MyCommand.Definition)
    [Reflection.Assembly]::UnsafeLoadFrom( $(Join-Path $ScriptPath "WinSCPnet.dll") ) | Out-Null
    
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "somewhere.com"
    UserName = "user"
    SshHostKeyFingerprint = "ssh-rsa 2048 XXXXXXXI"
    SshPrivateKeyPath = "C:\blahblah.ppk"
    }
 
    $session = New-Object WinSCP.Session
    $transferOptions = New-Object WinSCP.TransferOptions
    # Look to ignore any file property change errors
    $transferOptions.FilePermissions = $Null # This is default
    $transferOptions.PreserveTimestamp = $False
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
        
        # Upload files, collect results
        $transferResult = $session.PutFiles($localPath, $remotePath)
 
        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($Null -eq $transfer.Error)
            {
                Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup"
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
            }
            else
            {
                Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

The scripts works great, but unfortunately only loads one file before disconnecting. The script fails if there is an error loading any of the files, so it is working as expected. But the error I am getting, is " Upload of file 'somefile.DAT' was successful, but error occurred while setting the permissions and/or timestamp.

If the problem persists, turn off setting permissions or preserving timestamp. Alternatively you can turn on 'Ignore permission errors' option. The server does not support the operation. Error code: 8 Error message from server (US-ASCII): SETSTAT unsupported"

I think I have the following settings possibly configured incorrectly, but I'm not sure what I am doing wrong here....thoughts?

    $transferOptions.FilePermissions = $Null # This is default
    $transferOptions.PreserveTimestamp = $False

I've actually managed to get this to work by modifying the session and transfer options..

 $session.Open($sessionOptions)
    $transferOptions = New-Object WinSCP.TransferOptions
    # Look to ignore any file property change errors
    $transferOptions.PreserveTimeStamp = $false
    $transferOptions.FilePermissions = $Null
    $transferOptions.AddRawSettings("IgnorePermErrors",  "1")
    
    # Upload files, collect results
    $transferResult = $session.PutFiles($localPath, $remotePath, $False, $transferOptions)


   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM