Created
April 15, 2020 11:32
-
-
Save Jonatantwn/c6e40989061721057c52ef2814653bb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
$localPath = "C:\Users\Dern\Desktop\WINSCP PS DOWNLOAD\", | |
$remotePath = ("jonatantest/","jonatantest2/"), | |
$fileName = "wp-config-sample.php" | |
) | |
try | |
{ | |
# Load WinSCP .NET assembly | |
Add-Type -Path "C:\Users\Dern\Desktop\WINSCP PS\WinSCPnet.dll" | |
# Setup session options | |
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{ | |
Protocol = [WinSCP.Protocol]::ftp | |
HostName = "xxx" | |
UserName = "xxxx" | |
Password = "xxxx" | |
} | |
$session = New-Object WinSCP.Session | |
try | |
{ | |
# Connect | |
$session.Open($sessionOptions) | |
# Download files | |
$transferOptions = New-Object WinSCP.TransferOptions | |
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary | |
# Format timestamp | |
$stamp = $(Get-Random) | |
# Download the file and throw on any error | |
$transferResult = | |
$session.GetFiles( | |
($remotePath + $fileName), | |
($localPath + $fileName + "." + $stamp)).Check() | |
# Print results | |
foreach ($transfer in $transferResult.Transfers) | |
{ | |
Write-Host "Download of $($transfer.FileName) succeeded" | |
} | |
} | |
finally | |
{ | |
# Disconnect, clean up | |
$session.Dispose() | |
} | |
exit 0 | |
} | |
catch | |
{ | |
Write-Host "Error: $($_.Exception.Message)" | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment