Last active
April 15, 2020 16:18
-
-
Save Jonatantwn/e1b166aea7c0b3a0897f896f80cb5349 to your computer and use it in GitHub Desktop.
A method to download specific files from different folders, using powershell and WinSCP
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 = "xxx" | |
Password = "xxx" | |
} | |
$session = New-Object WinSCP.Session | |
try | |
{ | |
# Connect | |
$session.Open($sessionOptions) | |
foreach ($rp in $remotePath) { | |
# 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( | |
($rp + $fileName), | |
($localPath + $fileName + $stamp + "." + "php")).Check() | |
} | |
# Print results | |
foreach ($rp in $remotePath) | |
{ | |
Write-Host "Download of $($rp + $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
Following this, you can scan the folder you have downloaded the files to, in order to find a specific line in that file.
I needed to find the database name, so i used this:
Get-Content "C:\Users\Dern\Desktop\WINSCP PS DOWNLOAD*" | select-string 'DB_NAME' | foreach {Write-Output $_}