Upload files to FTP with Powershell


it took Long, but now, I decided to cut one more VBScript off, and start implementing a Powershell Script to upload the CRL to my CRL Distribtion Point with FTPS, so I took the Script from my collegue (http://www.thomasmaurer.ch/2010/11/powershell-ftp-upload-and-download/), and made some “improvements”:

#Declare the folder

#ftp server
$ftpserver = "ftp://yourftpserver/folder"
$user = "username"
$pass = "corresponding password"

foreach($item in (Get-ChildItem $Dir -Include "*.crl","*.crt","*.cer")){
"Uploading $item..."
#connect to ftp server
$ftp = [System.Net.FtpWebRequest]::Create($ftpserver+$item.Name)
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential($user,$pass)
$ftp.UseBinary = $true
$ftp.EnableSsl = $true
#$ftp.UsePassive = $false
# read in the file to upload as a byte array
$content = [System.IO.File]::ReadAllBytes($item.FullName)
$ftp.ContentLength = $content.Length
# get the request stream, and write the bytes into it
$rs = $ftp.GetRequestStream()
$rs.Write($content, 0, $content.Length)
# be sure to clean up after ourselves
$ftp = $null

Hope this helps 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.