Tuesday, October 29, 2019

Powershell - FTP download a file

This Powershell code will connect to an FTP site and download a file and store it in a location.

Method #1 LONG method with a bunch of error checking, mostly code i've found in other locations.
(method 2 below is far simpler).  I've included both for comparison and help with this

function Get-FTPfiles ($url,$credentials) 
        {
        write-host Connecting to FTP....
        $FTPrequest = [Net.WebRequest]::Create($url)
        $FTPrequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
        
        if ($credentials) 
            { 
            $FTPrequest.Credentials = $credentials 
            }
            
        $FTPresponse = $FTPrequest.GetResponse()
        
            if(!$FTPresponse)
            {   
            #if the response comes back as a 'null' it could mean that the FTP server might be down
            #the file wasn't found, something errored.  when this flag goes high (1) it cause the "UNTIL" statement to activate and break
            #out of this function loop
            $FTPState = 1
            break
            }
        
        $FTPfiles = New-Object IO.StreamReader $FTPresponse.GetResponseStream() 
                    
        while(-not $FTPfiles.EndOfStream) 
            {
            $FTPfiles.ReadLine()   
            }
    
        $FTPfiles.Close()
        $FTPresponse.Close()      
        }

$FTPserver = "ftp://123.123.123.123/" 
$FTPuser = 'FTPuserName' 
$FTPpassword = 'FTPpassword'
$FTPsourcefolder = '\'  #FTP subfolder if required
$FTPtargetfolder = "C:\downloads\" #local PC destination
$FTPfilename ="file to get"
$FTPState = 0

$credentials = new-object System.Net.NetworkCredential($FTPuser, $FTPpassword)
    
$folderPath= $FTPserver + "/" + $FTPsourcefolder + "/"
    
#the Do-Until loop below basically works in conjuction with the "get-FTPfiles" function.  In order to break out of the function,
#it runs 'until' the variable is not equal to 0, which causes the function to break out and continue with the program.
    do
        {
        $files = Get-FTPfiles -url $folderPath -credentials $credentials    
        $webclient = New-Object System.Net.WebClient 
        $webclient.Credentials = New-Object System.Net.NetworkCredential($FTPuser,$FTPpassword) 
   
        $FTPsourceFullPath=$folderPath + $FTPfilename  
        $ftpDestinationLocation = $FTPtargetfolder + $FTPfilename 
        $webclient.DownloadFile($FTPsourceFullPath, $FTPtargetfolder+$FTPfilename)
        $FTPState = 1
        }

    until ($FTPState -ne 0)
        {
        write-host FTP process flagged as completed
        }

$FileExists = Test-Path $ftpDestinationLocation

    If ($FileExists -eq $True) 
        {
        write-host File Downloaded Successfully
        
        }

METHOD 2
Script for DOWNLOADING from FTP

$FTPserverIP = "123.123.123.123"
$FTPuser = 'username'
$FTPpassword = 'password'
$FTPtargetfolder = "C:\download\"
$FTPfilename ="testfile.csv"

$client = New-Object System.Net.WebClient
$client.Credentials = New-Object System.Net.NetworkCredential("$FTPuser", "$FTPpassword")

$client.DownloadFile("ftp://$FTPserverIP/$FTPfilename", "$FTPtargetfolder\$FTPfilename")


Script for UPLOADING to FTP

$FTPserverIP = "123.123.123.123"
$FTPuser = 'username'
$FTPpassword = 'password'
$FTPtargetfolder = "C:\download\"
$FTPfilename ="testfile.csv"

$client = New-Object System.Net.WebClient
$client.Credentials = New-Object System.Net.NetworkCredential("$FTPuser", "$FTPpassword")
$client.UploadFile("ftp://$FTPserverIP/$FTPfilename", "$FTPtargetfolder\$FTPfilename")

No comments:

Post a Comment