Skip to content

Instantly share code, notes, and snippets.

@alexandair
Created January 22, 2020 19:00
Show Gist options
  • Save alexandair/6314d53e7f3d64dea7aff8c0a646bf08 to your computer and use it in GitHub Desktop.
Save alexandair/6314d53e7f3d64dea7aff8c0a646bf08 to your computer and use it in GitHub Desktop.
$sourceuri = "ftp://ftp.example.com/myfolder/myfile.xml"
$filePath = "C:\temp\myfile.xml"
$username = "user"
$password = "password"
# Create a FTPWebRequest object to handle the connection to the ftp server
$ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)
$credentials = New-Object System.Net.NetworkCredential($username,$password)
# set the request's network credentials for an authenticated connection
$ftprequest.Credentials = $credentials
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftprequest.UseBinary = 1
$ftprequest.KeepAlive = 0
# read in the file to upload as a byte array
$content = gc -en byte $filePath
$ftprequest.ContentLength = $content.Length
# get the request stream, and write the bytes into it
$rs = $ftprequest.GetRequestStream()
$rs.Write($content, 0, $content.Length)
# be sure to clean up after ourselves
$rs.Close()
$rs.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment