Skip to content

Instantly share code, notes, and snippets.

@rob89m
Last active December 27, 2019 04:07
Show Gist options
  • Save rob89m/02b6935646cfa7b9cd32a3a9ab5e5505 to your computer and use it in GitHub Desktop.
Save rob89m/02b6935646cfa7b9cd32a3a9ab5e5505 to your computer and use it in GitHub Desktop.
# Config
$Username = "Robert"
$Password = "rob"
$LocalFile = "C:\Temp\Test2.exe"
$RemoteFile = "ftp://ftphost/test.exe"
# Create a FTPWebRequest
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse()
# Get a download stream from the server response
$ResponseStream = $FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFileFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
# Config
$Username = "Robert"
$Password = "rob"
$LocalFile = "C:\Temp\Test.exe"
$RemoteFile = "ftp://ftphost/test.exe"
# Create FTP Rquest Object
$FTPRequest = [System.Net.FtpWebRequest]::Create("$RemoteFile")
$FTPRequest = [System.Net.FtpWebRequest]$FTPRequest
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
$FTPRequest.UseBinary = $true
$FTPRequest.UsePassive = $true
# Read the File for Upload
$FileContent = Get-Content -en byte $LocalFile
$FTPRequest.ContentLength = $FileContent.Length
# Get Stream Request by bytes
$Run = $FTPRequest.GetRequestStream()
$Run.Write($FileContent, 0, $FileContent.Length)
# Cleanup
$Run.Close()
$Run.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment