Below code can be used to perform FTP upload and Download using Powershell:
param([Parameter(Mandatory=$true,Position=0)] [string]$SourceFilePath, [Parameter(Mandatory=$true,Position=1)] [string]$TargetFilepath,[Parameter(Mandatory=$true,Position=2)][ValidateSet("Upload","Download")]$OperationType)
try
{
$Username = "temp-account"
$Password = "tempPassword"
if($OperationType -eq "Upload")
{
$serverUri = "ftp://ftp2.myweb.com" + $TargetFilepath
}
else
{
$serverUri = "ftp://ftp2.myweb.com" + $SourceFilePath
}
[System.Net.FtpWebRequest] $FTPrequest = [System.Net.FtpWebRequest]::Create($serverUri)
$FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
$FTPRequest.Timeout = 10000 #Time out in Milliseconds
$FTPRequest.UsePassive = $true
$FTPRequest.UseBinary = $false
$FTPRequest.KeepAlive = $false #This is important property and terminates connection after
# completion of operation
if($OperationType -eq "Upload")
{
$SourceFile = $SourceFilePath
$FTPrequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
Write-host "Uploading File..."
#Create a request Stream
[System.IO.Stream] $requestStream = $FTPRequest.GetRequestStream()
if($requestStream -ne $null)
{
#Read File to be uploaded
$FileContents = Get-Content -path $SourceFile -encoding byte
#Write File
$requestStream.write($FileContents, 0, $FileContents.Length)
$requestStream.Close()
$requestStream.dispose()
#Validating Status of Upload
[System.Net.FtpWebResponse] $response = $FTPrequest.GetResponse();
Write-host "File Upload Status $($response.StatusDescription)"
$response.Close()
$response.dispose()
}
else
{
[System.Net.FtpWebResponse] $response = $FTPrequest.GetResponse();
Write-host "File Upload Status $($response.StatusDescription)"
}
}
elseif($OperationType -eq "Download")
{
$TargetFile = $TargetFilepath
Write-host "Downloading File..."
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile;
#Get the response object
[System.Net.FtpWebResponse] $FTPResponse = $FTPRequest.GetResponse()
if($FTPResponse -ne $null)
{
[System.IO.Stream] $responseStream = $FTPResponse.GetResponseStream()
System.IO.FileStream]$FileStream = New-Object System.IO.FileStream($TargetFile,[System.IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$FileStream.Write($ReadBuffer,0,$ReadLength)
} while ($ReadLength -ne 0)
Write-host "File Download Status $($FTPResponse.StatusDescription)"
$FileStream.Close()
$FileStream.dispose()
$responseStream.Close()
$responseStream.dispose()
}
}
}
catch [Exception]
{
if($requestStream -ne $null)
{
$requestStream.Close()
$requestStream.dispose()
}
if($responseStream -ne $null)
{
$responseStream.Close()
$responseStream.dispose()
}
if($FileStream -ne $null)
{
$FileStream.Close()
$FileStream.dispose()
}
Write-host "Exception Type: $($_.Exception.GetType().FullName);`n
Exception Code: $($_.Exception.ErrorCode);Exception Message: $($_.Exception.Message) "
}
#Usage
#Upload
FTPUploadDownload.ps1 "D:\Temp\FileNametobeUploaded.txt" "/FTPFolder/Targetfilename.txt" Upload
#Download
FTPUploadDownload.ps1 "/FTPFolder/Sourcename.txt" "D:\Temp\DownloadedFileName.txt" Download
param([Parameter(Mandatory=$true,Position=0)] [string]$SourceFilePath, [Parameter(Mandatory=$true,Position=1)] [string]$TargetFilepath,[Parameter(Mandatory=$true,Position=2)][ValidateSet("Upload","Download")]$OperationType)
try
{
$Username = "temp-account"
$Password = "tempPassword"
if($OperationType -eq "Upload")
{
$serverUri = "ftp://ftp2.myweb.com" + $TargetFilepath
}
else
{
$serverUri = "ftp://ftp2.myweb.com" + $SourceFilePath
}
[System.Net.FtpWebRequest] $FTPrequest = [System.Net.FtpWebRequest]::Create($serverUri)
$FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
$FTPRequest.Timeout = 10000 #Time out in Milliseconds
$FTPRequest.UsePassive = $true
$FTPRequest.UseBinary = $false
$FTPRequest.KeepAlive = $false #This is important property and terminates connection after
# completion of operation
if($OperationType -eq "Upload")
{
$SourceFile = $SourceFilePath
$FTPrequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
Write-host "Uploading File..."
#Create a request Stream
[System.IO.Stream] $requestStream = $FTPRequest.GetRequestStream()
if($requestStream -ne $null)
{
#Read File to be uploaded
$FileContents = Get-Content -path $SourceFile -encoding byte
#Write File
$requestStream.write($FileContents, 0, $FileContents.Length)
$requestStream.Close()
$requestStream.dispose()
#Validating Status of Upload
[System.Net.FtpWebResponse] $response = $FTPrequest.GetResponse();
Write-host "File Upload Status $($response.StatusDescription)"
$response.Close()
$response.dispose()
}
else
{
[System.Net.FtpWebResponse] $response = $FTPrequest.GetResponse();
Write-host "File Upload Status $($response.StatusDescription)"
}
}
elseif($OperationType -eq "Download")
{
$TargetFile = $TargetFilepath
Write-host "Downloading File..."
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile;
#Get the response object
[System.Net.FtpWebResponse] $FTPResponse = $FTPRequest.GetResponse()
if($FTPResponse -ne $null)
{
[System.IO.Stream] $responseStream = $FTPResponse.GetResponseStream()
System.IO.FileStream]$FileStream = New-Object System.IO.FileStream($TargetFile,[System.IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$FileStream.Write($ReadBuffer,0,$ReadLength)
} while ($ReadLength -ne 0)
Write-host "File Download Status $($FTPResponse.StatusDescription)"
$FileStream.Close()
$FileStream.dispose()
$responseStream.Close()
$responseStream.dispose()
}
}
}
catch [Exception]
{
if($requestStream -ne $null)
{
$requestStream.Close()
$requestStream.dispose()
}
if($responseStream -ne $null)
{
$responseStream.Close()
$responseStream.dispose()
}
if($FileStream -ne $null)
{
$FileStream.Close()
$FileStream.dispose()
}
Write-host "Exception Type: $($_.Exception.GetType().FullName);`n
Exception Code: $($_.Exception.ErrorCode);Exception Message: $($_.Exception.Message) "
}
#Usage
#Upload
FTPUploadDownload.ps1 "D:\Temp\FileNametobeUploaded.txt" "/FTPFolder/Targetfilename.txt" Upload
#Download
FTPUploadDownload.ps1 "/FTPFolder/Sourcename.txt" "D:\Temp\DownloadedFileName.txt" Download