Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

jemacc

Check Remote SFtP files

Thank for your assistance. The addition work
I have also made some changes but could not get it to Work correctly. I wanted to check the remote files, if the files exit and not upload the files from $localpath and display message to check the remote location but if the file does not exist upload the files and then move the files from the $localpath to the $backupath

try

{
   # Load WinSCP .NET assembly
    [Reflection.Assembly]::LoadFrom("\\C:\Program Files\WinSCP\WinSCPnet.dll") | Out-Null
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "192.20.xxx.xx"
    $sessionOptions.PortNumber = 22   
    $sessionOptions.UserName = "SAMPLE"
    $sessionOptions.Password = "Password$1@"
    $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:x:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
     
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
       
        $localPath = "C:\temp\SEND\*.*"
        $remotePath = "/home/DEV/"
        $backupPath = "C:\temp\Backup\"+$(Get-Date -f "yyyyMMddHHmmss")
        New-Item -ItemType directory -Path $backupPath
 
        if ($session.FileExists($remotePath))
        {
            Write-Host ("File {0} File exists, Check why the file has not move from remote locatiom" -f $remotePath)
           
            exit 0
        }
        else
        {
            Write-Host ("File {0} does not exist" -f $remotePath)
            $transferResult = $session.PutFiles($localPath, $remotePath)
            Move-Item $transfer.FileName $backupPath
           
            exit 1
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 2
}
martin

Re: Add datestamp to files or create folder with date stamp

If I understand your question right, all you are missing is appending the timestamp to a $backupPath, and creating that directory, correct?
Well, it's more of PowerShell question than WinSCP.

Anyway, use this:
$backupPath = "C:\temp\Backup\" + $(Get-Date -f "yyyyMMddHHmmss")

New-Item -ItemType directory -Path $backupPath


References:
https://winscp.net/eng/docs/script_download_timestamped_filename
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item
jemacc

Add datestamp to files or create folder with date stamp

Hi the a standard script retrieved from this forum wich work as design. I will like to make some modification but have failed in making it work.
Steps
I connect to and sftp site and upload all files from directory "C:\temp\send\", then I move the files to backup location "C:\temp\Backup\"

This scripts is scheduled to run every day during a specific time or it can also be run manually. What I will like to do is for each day that the scripts run to to create subfolder under "C:\temp\Backup\" with the current day "C:\temp\Backup\20141004" and the files that have uploaded to the sftp site.

option 2
when moving the files from "C:\temp\send\" to "C:\temp\Backup\"
is to add time stamp to all the files, for example, the filenames gytcxs.txt= 20141004_gytcxs.txt
tsystt.trn=20141004_tsystt.trn
yhftdt.bat=20141004_yhftdt.bat

This is the powershell script

try

{
    # Load WinSCP .NET assembly
    [Reflection.Assembly]::LoadFrom("\\C:\Program Files\WinSCP\WinSCPnet.dll") | Out-Null
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "192.20.xxx.xx"
    $sessionOptions.PortNumber = 22   
    $sessionOptions.UserName = "sample"
    $sessionOptions.Password = "Password$1@"
    $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:x:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $localPath = "C:\temp\send\*.*"
        $remotePath = "/home/DEV/"
        $backupPath = "C:\temp\Backup\"
 
        # Upload files, collect results
        $transferResult = $session.PutFiles($localPath, $remotePath)
 
        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($transfer.Error -eq $Null)
            {
                Write-Host ("Upload of {0} succeeded, moving to backup location" -f
                 $transfer.FileName)
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
            }
            else
            {
                Write-Host ("Upload of {0} failed: {1}" -f
                    $transfer.FileName, $transfer.Error.Message)
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}


Any help will be greatly appreciated