Last active
December 27, 2019 04:07
-
-
Save rob89m/02b6935646cfa7b9cd32a3a9ab5e5505 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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