Created
January 22, 2020 19:00
-
-
Save alexandair/6314d53e7f3d64dea7aff8c0a646bf08 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
$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