Powershell example for downloading latest file

Advertisement

freff
Joined:
Posts:
2
Location:
Uk

Powershell example for downloading latest file

Hi,

I am new to scripting and opted for the powershell example purely because I have powershell on my laptop.

In the example that is supplied on the website I am having issue with the code and suspect that it is related to me not the example.

here is the example that you have supplied my first thought it was going to be straight forward, however had an issue registering winscpnet.dll then an issue with the script Directoryinfo was not a cmd, I guess that this line needed a $ before the command.

after inserting a $ the script now moves onto the next issue

Can't get attributes of file '/usr/xxx/xxx.cvs'

Can some one please point me in the right direction I have ensured that I have two files in the folder and while they have the same date stamp they do have different time stamps.

any help would be great

regards

Steve



try
{
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCPnet.dll") | Out-Null

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "XXXXXXXXX"
$sessionOptions.UserName = "XXXXXX"
$sessionOptions.Password = "XXXXXXX"
$sessionOptions.SshHostKeyFingerprint = "ssh-dss 1024 xx:xx:"

$session = New-Object WinSCP.Session

try
{
# Connect
$session.Open($sessionOptions)

$localPath = "c:\downloaded\"
$remotePath = "/usr/xxxx"

# Gel list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)

# Select the most recent file
$latest =
$directoryInfo.Files | ****************Added $
Where-Object { -Not $_.IsDirectory } |
Sort-Object LastWriteTime |
#Select-Object -Last 1

# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}

# Download the selected file
$session.GetFiles($session.EscapeFileMask($remotePath + $latest.Name), $localPath).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
28,802
Location:
Prague, Czechia

Re: Powershell example for downloading latest file

First, thanks for pointing out the problem with missing $.

Regarding the error: What is /usr/xxx/xxx.cvs? Is it a path you set $remotePath to? The purpose of the script is to find the latest file in a directory. So the $remotePath should be set to a path to the directory, not a specific path.

Posting a complete error message, or even better a log file, would help.
_________________
Martin Prikryl

Reply with quote

freff
Joined:
Posts:
2
Location:
Uk

Re: Powershell example for downloading latest file

martin wrote:

First, thanks for pointing out the problem with missing $.

Regarding the error: What is /usr/xxx/xxx.cvs? Is it a path you set $remotePath to? The purpose of the script is to find the latest file in a directory. So the $remotePath should be set to a path to the directory, not a specific path.

Posting a complete error message, or even better a log file, would help.

Hi Prikryl,


When I run the Powershell script it does not create a log file

After looking at the error I can see that on the remote path I am missing the / at the end I have added the / for the path and it appears to be working now.

Many thanks for your help

Regards

steve

Reply with quote

Advertisement

You can post new topics in this forum