Skip to content

Instantly share code, notes, and snippets.

@Jonatantwn
Last active April 15, 2020 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jonatantwn/e1b166aea7c0b3a0897f896f80cb5349 to your computer and use it in GitHub Desktop.
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
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
}
@Jonatantwn
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment