Red Gate forums :: View topic - Website via FTP
Return to RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation

Deployment Manager PowerShell Scripts forum

Website via FTP

Search in Deployment Manager PowerShell Scripts forum
Post new topic   Reply to topic
Jump to:  
Author Message

Joined: 20 Apr 2011
Posts: 55

PostPosted: Tue Jun 25, 2013 6:08 pm    Post subject: Website via FTP Reply with quote

This script deploys a website (or perhaps a folder of any other files) via FTP . It doesn't delete files that are not in the package, as I have virtual directories also in the FTP root.

It's being used for this website:

My package contains the published website files, and the following deploy.ps1.

I put the web app and videos for the web app in two separate packages with the same script, to reduce the size of the package that changes most frequently.


#$domain = ""
#$ftpPassword = "********"

$ftpUrl = "ftp://ftp.$domain/wwwroot"
$ftpUserName = $domain

Function FtpUpload ([System.Uri]$uri, [string]$username, [string]$password, [string]$filePath)
    $ftp = [System.Net.FtpWebRequest]::Create($uri)
    $ftp = [System.Net.FtpWebRequest]$ftp
    $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $ftp.Credentials = new-object System.Net.NetworkCredential($username,$password)
    $ftp.UseBinary = $true
    $ftp.UsePassive = $true
    $content = [System.IO.File]::ReadAllBytes($filePath)
    $ftp.ContentLength = $content.Length
    $rs = $ftp.GetRequestStream()
    $rs.Write($content, 0, $content.Length)

Function FtpCreateFolder ([System.Uri]$uri, [string]$username, [string]$password)
    $ftp = [System.Net.FtpWebRequest]::Create($uri)
    $ftp = [System.Net.FtpWebRequest]$ftp
    $ftp.Method = [System.Net.WebRequestMethods+Ftp]::MakeDirectory
    $ftp.Credentials = new-object System.Net.NetworkCredential($username,$password)
        $rs = $ftp.GetResponse()
    catch [System.Exception]
    if( $rs -ne $null )

$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path
cd $myDir
$files = Get-ChildItem -recurse

foreach($file in $files)
    $directory = $null
    if ($file.DirectoryName -ne $null)
        $directory = $file.DirectoryName.Replace($myDir,"")
    $directory += "/";
    $uri = New-Object System.Uri($ftpUrl + $directory + $file.Name)

    if( $file.Extension -eq ".nupkg" )
    # if a folder
    if( $file.DirectoryName -eq $null)
        "Create folder, URI: $uri"
        FtpCreateFolder $uri $ftpUserName $ftpPassword
        "Upload file, URI: $uri, File: " + $file.FullName
        FtpUpload $uri $ftpUserName $ftpPassword $file.FullName

I hope this helps,

Justin Caldicott
Product Manager - Deployment Manager
Red Gate
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group