Friday, 31 July 2015

FTP/FTPS ( FTP over SSL ) Share using Windows PowerShell

[Net.ServicePointManager]::ServerCertificateValidationCallback={$true}
$Dir = "Location of Directory where Files to Upload"
foreach($item in (dir $dir))
{
    write-output "————————————–"
    $fileName = $item.FullName
    write-output $fileName
    $ftp = [System.Net.FtpWebRequest]::Create("ftp://some.ftpserver.com/"+$item.Name)
    $ftp = [System.Net.FtpWebRequest]$ftp
    $ftp.UsePassive = $true
    $ftp.UseBinary = $true
    $ftp.EnableSsl = $true
    $ftp.Credentials = new-object System.Net.NetworkCredential("username","password")
    $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $rs = $ftp.GetRequestStream()
   
    $reader = New-Object System.IO.FileStream ($fileName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read)
    [byte[]]$buffer = new-object byte[] 4096
    [int]$count = 0
    do
    {
        $count = $reader.Read($buffer, 0, $buffer.Length)
        $rs.Write($buffer,0,$count)
    } while ($count -gt 0)
    $reader.Close()
    $rs.Close()
    write-output "+transfer completed"
    }


Change the settings according to your Env. which are bolded

For Upload files with Plain FPT with SSL Disabled, change the following settings in the above script:

$ftp.EnableSsl = $false

No comments:

Post a Comment