Ive tried connecting to an ftp with the following powershell script:
#FTP Server Information - SET VARIABLES
$ftp = "ftp://XXX.com/"
$user = 'UserName'
$pass = 'Password'
$folder = 'FTP_Folder'
$target = "C:\Folder\Folder1\"
#SET CREDENTIALS
$credentials = new-object System.Net.NetworkCredential($user, $pass)
function Get-FtpDir ($url,$credentials) {
$request = [Net.WebRequest]::Create($url)
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
if ($credentials) { $request.Credentials = $credentials }
$response = $request.GetResponse()
$reader = New-Object IO.StreamReader $response.GetResponseStream()
while(-not $reader.EndOfStream) {
$reader.ReadLine()
}
#$reader.ReadToEnd()
$reader.Close()
$response.Close()
}
#SET FOLDER PATH
$folderPath= $ftp + "/" + $folder + "/"
$files = Get-FTPDir -url $folderPath -credentials $credentials
$files
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
$counter = 0
foreach ($file in ($files | where {$_ -like "*.txt"})){
$source=$folderPath + $file
$destination = $target + $file
$webclient.DownloadFile($source, $target+$file)
#PRINT FILE NAME AND COUNTER
$counter++
$counter
$source
}
But i keep getting 530 error (authentication error) when connecting. I know the username and password is working cause i've tested it in other ftp clients. So, i think it might be a problem because the webapp demands a certain protocol, which isnt used in this script.
Ive been doing some research and i found something called Posh-SSH which might work. But is there a way to modify my script instead? When im connecting with winscp i use FTP protocol with TLS/SSL implicit encryption to port 990.
UPDATE: WORKS I made the following work:
#FTP Server Information - SET VARIABLES
$ftp = "ftp://waws-prod-xxx.ftp.azurewebsites.windows.net"
$user = 'xxxxx\[email protected]'
$pass = '$FRnqxxpxxxxxxx'
$folder = 'site/wwwroot/wwwroot/images/uploaded'
$target = "C:\Folder\Folder1\"
#SET CREDENTIALS
$credentials = new-object System.Net.NetworkCredential($user, $pass)
function Get-FtpDir ($url,$credentials) {
$request = [Net.WebRequest]::Create($url)
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
if ($credentials) { $request.Credentials = $credentials }
$response = $request.GetResponse()
$reader = New-Object IO.StreamReader $response.GetResponseStream()
while(-not $reader.EndOfStream) {
$reader.ReadLine()
}
#$reader.ReadToEnd()
$reader.Close()
$response.Close()
}
#SET FOLDER PATH
$folderPath= $ftp + "/" + $folder + "/"
$files = Get-FTPDir -url $folderPath -credentials $credentials
Write-Host($files)
$files
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
$counter = 0
foreach ($file in ($files)){
$source=$folderPath + $file
$destination = $target + $file
$webclient.DownloadFile($source, $target+$file)
#PRINT FILE NAME AND COUNTER
$counter++
$counter
$source
}
I ended up adding a new credential to the web app, and changing sftp in hostname to ftp, and now it works. Using the credentials from webpublish file works too.
I also made WinSCP work and im able to download the full folder with children.
#FTP Server Information - SET VARIABLES
$ftp = "waws-prod-xxx-xxx.ftp.azurewebsites.windows.net"
$user = 'xxxxxx\[email protected]'
$pass = '$xxxxxxxxx'
$folder = 'site/wwwroot/wwwroot/images/uploaded/*'
$target = "C:\Folder\Folder1\*"
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = $ftp
UserName = $user
Password = $pass
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Download files
$session.GetFiles($folder, $target).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}