Monday, October 12, 2009

Powershell | FTP download/upload client

# UPLOAD file through ftp

$filename = "test.zip"
$localfile="c:\"+$filename

$ftphost="ftp://ftpserver"
$ftp_user="username"
$ftp_pass="password"

$remotefile="/folder/$filename"
$URI = $ftphost + $remotefile
$credentials=New-Object System.Net.NetworkCredential($ftp_user,$ftp_pass)
$ftp=[System.Net.FtpWebRequest]::Create($URI)
$ftp.Credentials=$credentials
$ftp.UsePassive=0 #active mode, 1 for passive mode
$ftp.UseBinary=1
$ftp.KeepAlive=0
$ftp.Method="STOR" #upload method

$read = [System.IO.File]::ReadAllBytes($localfile) #read local file contents

$responseStream=$ftp.GetRequestStream()
$responseStream.Write($read, 0, $read.Length) #upload file
$responseStream.Close()
$responseStream.Dispose()

System.Net.FtpWebRequest
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest_methods.aspx

How to upload a file, the PowerShell way, Anders Hesselbom,
http://www.winsoft.se/2009/10/how-to-upload-a-file-the-powershell-way

Simple ftp client for powershell (download file)
http://rcovelo.blogspot.com/2008/10/powershell-very-simple-ftp-client.html

No comments:

Post a Comment