Simple powershell automated FTP upload script

[This post was originally posted on my personal blog, http://simplystallings.com]

At work, we manually patch our web servers. Part of the process requires ftping the updates to each server. This process is tedious considering the number of web servers and number of steps required. I decided this past patch Tuesday that I would invest a little time into automating the process using powershell.

I located a simple ftp upload script located @ http://poshcode.org/1134. With a few modifications I had my script:

$file = "somefilename"
$filePath = "C:\" + $file
for ($i=0;$i-lt$servers.length;$i++)
{
"ftp url: $ftp"
#FTP URL syntax
$ftp = $servers[$i]+$file
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Uploading $File..."
$webclient.UploadFile($uri, $filePath)
}

I added an array that contains the ftp URL syntax for each web server then I iterate over the array using a for loop.

A few notes about the script. If you plan to put passwords in the script then you should encrypt the file. Alternatively, you could have the script prompt for credentials. The script will automatically overwrite a file if the same file name exists in the remote destination. Do not use passwords with an @ character because the powershell parser will not be able to determine where the host name begins.

Happy ftping.

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *