Issue with Script execution and upload of a file to FTP site
Hi
We are running a script below to upload a CSV file from a root directory to an FTP site, the root has a new child folder created each day ( folder name is date stamp ) and the CSV name is date stamp in the child folder. When we run the below script i can see the variable populated with the latest CSV file, but the upload to FTP side of the script does not upload the file and we dont get an error, it just doesn't upload. I can manually upload using Winscp so it isnt F/W, permissions etc, can you help please?? our script is below, Thanks in Advance.
-------------------------------
try
{
# Load WinSCP .NET assembly
#Add-Type -Path "WinSCPnet.dll"
Add-Type -Path "e:\Program Files\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "ftp.********.com"
PortNumber = 21
UserName = "********"
Password = "*********"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$localPath = "F:\upload\filestoupload\"
$remotePath = "/in/"
# Select the most recent file.
# The !$_.PsIsContainer test excludes subdirectories.
# With PowerShell 3.0, you can replace this with -File switch of Get-ChildItem.
$latest =
Get-ChildItem -Path $localPath -recurse -Filter *.csv |
Where-Object {!$_.PsIsContainer} |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}
# Upload the selected file
$session.PutFiles($session.EscapeFileMask($localPath + $latest.Name), $remotePath).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
# exit
# Execute the script using a command like:
# "C:\Program Files\WinSCP\WinSCP.exe" /log="C:\writable\path\to\log\WinSCP.log" /ini=nul /script="C:\path\to\script\script.txt"
We are running a script below to upload a CSV file from a root directory to an FTP site, the root has a new child folder created each day ( folder name is date stamp ) and the CSV name is date stamp in the child folder. When we run the below script i can see the variable populated with the latest CSV file, but the upload to FTP side of the script does not upload the file and we dont get an error, it just doesn't upload. I can manually upload using Winscp so it isnt F/W, permissions etc, can you help please?? our script is below, Thanks in Advance.
-------------------------------
try
{
# Load WinSCP .NET assembly
#Add-Type -Path "WinSCPnet.dll"
Add-Type -Path "e:\Program Files\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "ftp.********.com"
PortNumber = 21
UserName = "********"
Password = "*********"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$localPath = "F:\upload\filestoupload\"
$remotePath = "/in/"
# Select the most recent file.
# The !$_.PsIsContainer test excludes subdirectories.
# With PowerShell 3.0, you can replace this with -File switch of Get-ChildItem.
$latest =
Get-ChildItem -Path $localPath -recurse -Filter *.csv |
Where-Object {!$_.PsIsContainer} |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}
# Upload the selected file
$session.PutFiles($session.EscapeFileMask($localPath + $latest.Name), $remotePath).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
# exit
# Execute the script using a command like:
# "C:\Program Files\WinSCP\WinSCP.exe" /log="C:\writable\path\to\log\WinSCP.log" /ini=nul /script="C:\path\to\script\script.txt"