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
}
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
}