Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Powershell to upload files on ftp

$
0
0

Hello,

i found this script for uploading files on ftp servers but it does not work, or at least for me.

The problem is that besides files are not uploaded on ftp server, the files in folder from which i am uploading are set to size 0.

I got the following error:Exception calling "GetResponse" with "0" argument(s): "The remote server returned
 an error: (550) File unavailable (e.g., file not found, no access)."
At line:14 char:43
+     $FTPResponse = $FTPRequest.GetResponse <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

 

Here is the script:

$Username = "user"
$Password = "password"
$UploadFolder = "D:\ftptest"
$RemoteFile = "ftp://ftp.test.com/ftptest";

ForEach ($File in (Get-ChildItem $UploadFolder))
{   # Create a FTPWebRequest
    $FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
    $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
    $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $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 ($File.FullName,[IO.FileMode]::Create)
    [byte[]]$ReadBuffer = New-Object byte[] 1024
    # Loop through the download
    While ($ReadLength -ne 0)
    {   $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
        $LocalFileFile.Write($ReadBuffer,0,$ReadLength)
    }
}

Any help would be nice :)


Viewing all articles
Browse latest Browse all 6937

Trending Articles