Struggling with script and batch file to move file from local server to remote directory
I am fairly new to this so please respond as if I know nothing and speak in layman's terms.
I am using WinSCP to move a file every 15 minutes from a local server to a remote (web server) directory, from where a database is updated with the contents of that file.
That bit works OK, using the Keeping Remote Directory Up To Date option.
However, no matter what I try, using Task Scheduler on a Windows server, scripts and batch files, I cannot get it to work automatically and consistently.
It might work for a day but not the next, and at the moment I have to remember to start the WinSCP process manually every day.
Can someone suggest a basic batch file which calls on a script which will do the job, or is that too vague a question?
The file to be transferred is called Freestock.csv and this is the batch file I was advised to use:
which in turn called upon a Powershell script as per below:
I am using WinSCP to move a file every 15 minutes from a local server to a remote (web server) directory, from where a database is updated with the contents of that file.
That bit works OK, using the Keeping Remote Directory Up To Date option.
However, no matter what I try, using Task Scheduler on a Windows server, scripts and batch files, I cannot get it to work automatically and consistently.
It might work for a day but not the next, and at the moment I have to remember to start the WinSCP process manually every day.
Can someone suggest a basic batch file which calls on a script which will do the job, or is that too vague a question?
The file to be transferred is called Freestock.csv and this is the batch file I was advised to use:
cd /D %CD% Powershell.exe -executionpolicy remotesigned -File WinSCP.ps1
param ( $localPath = "D:\Freestock", $remotePath = "/upd/stock" ) try { # Load WinSCP .NET assembly Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Ftp HostName = "******" UserName = "******" Password = "******" } $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) # Synchronize files to local directory, collect results $synchronizationResult = $session.SynchronizeDirectories( [WinSCP.SynchronizationMode]::Remote, $localPath, $remotePath, $False) # Deliberately not calling $synchronizationResult.Check # as that would abort our script on any error. # We will find any error in the loop below # (note that $synchronizationResult.Downloads is the only operation # collection of SynchronizationResult that can contain any items, # as we are not removing nor uploading anything) # Iterate over every download foreach ($Upload in $synchronizationResult.Uploads) { # Success or error? Write-Host "==== Upload of $($Upload.FileName) ===" if ($Upload.Error -eq $Null) { Write-Host "Download of $($Upload.FileName) succeeded, removing from source" # Download succeeded, remove file from source $filename = [WinSCP.RemotePath]::EscapeFileMask($Upload.FileName) Remove-Item($filename) if( -not $? ) { Write-Host "Removing of file $($Upload.FileName) failed" } else { Write-Host "Removing of file $($Upload.FileName) succeeded" } } else { Write-Host ( "Upload of $($Upload.FileName) failed: $($Upload.Error.Message)") } } } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch { Write-Host "Error: $($_.Exception.Message)" exit 1 } #Get all files for processing from Speedy file #Set location to script location $scriptPath = $MyInvocation.MyCommand.Path $rootDir = Split-Path $scriptPath Set-Location $rootDir $outDir = $rootDir $strOutputFilename = $outDir + "/run-log.txt" $date = Get-Date $strOutput = "Last run time = " + $date Write-Host "Date = " $date Write-Host "Output Filename = " + $strOutputFilename $strOutput = $strOutput | Out-File -FilePath $strOutputFilename -Encoding utf8 -Append Write-Host "The scheduled task test has successfully run." #Read-Host "Press 'Enter' to exit." #Use Read-Host to keep Powershell console open (debugging) #Read-Host "Please stop here."