This script is an attempt to automate a lengthy error-prone copying and configuration process that we do each week. If we do the process manually it can take anywhere from five to twenty minutes, and it tends to have various points of failure. The piece below is actually just one portion of the process. The steps include:
1. Get the name of a new folder to be created on the server
2. Get the name of the file to be copied into the new folder
3. Using the two new names, build a text file which contains commands that will be fed into PSFTP
4. Call PSFTP and run the commands in the text file.
This PowerShell script uses Putty FTP to log into an FTP server, create a new folder, and copy a file to that folder from the local host. Note the the steps for making the folder and copying the file are contained in a Putty script called gwkprocess.scr. This secondary script is is used as input to the Putty program after Putty makes the connection. Those steps are typical FTP steps:
CD / topdirectory
MKDIR /new directory
<# Powershell Scripted FTP
Send a file to the eMail server via FTP.
Uses the Putty Secure FTP program PSFTP
# Note that the login credentials are in clear text!
# Enter the new folder name here.
$NewFolder = “20141101ks”
# The Picture file to be copied is located in
# and should be named, with the usual naming convention
$PicFile = “20141101ks-image.png”
#Note line wraps.
#Build the Putty Script file
“cd /home/web/html/store/images/fy2014/Kids-Shop”| Out-File -FilePath C:UsersLarryPowershellgwkprocess.scr -Encoding ascii
“mkdir $Newfolder” | Out-File -FilePath C:UsersLarryPowershellgwkprocess.scr -Encoding ascii -Append
“put $PicFile” | Out-File -FilePath C:UsersLarryPowershellgwkprocess.scr -Encoding ascii -Append
“ls” | Out-File -FilePath C:UsersLarryPowershellgwkprocess.scr -Encoding ascii -Append
# Call the putty program
.psftp email@example.com -P 22 -pw mypassword -v -2 -b gwkprocess.scr
This starts PSFTP in the Powershell window, makes the connection and then executes the gwkprocess.scr steps. It then closes the connection. If there is a problem, PSFTP will print a failure message, but clearly there is room for more error checking on the front end.
The presumption is that the secondary script gets rebuilt with new file and folder names each time the script is run. Obviously, there are some refinements to be included, like interactive data entry of the file and folder names.