Using Powershell for ftp 3

From MyWiki
Jump to: navigation, search
$Username = "FTPUSER"
$Password = "testPassw0rd"
 
#hash with source and destination path
$list =@{ "ftp://myftpserver.com/files/file1.zip"="C:\temp\dest1.zip";"ftp://myftpserver.com/files/file2.zip"="C:\temp\dest1.zip"}
 
$list | % {
 
	$LocalFile = $_.value
	$RemoteFile = $_.name
 
	# 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)
 
	$LocalFileFile.close()
}