Having spent some hours figuring out how to script an FTP transfer, I thought I’d describe my kludge. Maybe someone can suggest a more elegant way. I’m trying to I’m connect to a FTP server on my Linux box to upload a file.
There are at least three approaches that can be taken:
1. Directly interact with .NET objects
2. Import a Powershell module for FTP
3. Use Powershell to manipulate a command line FTP program, such as the Putty Secure FTP program PSFTP.
I started with the second option recommended on TechNet. Looks great, and I thought that it was semi-official (being from Technet). I was unable to get a connection and I think it may be related to the fact that module apparently doesn’t support SFTP version 2. There are a couple other quirks with the module… including the fact that the user name and password are passed to the command line as an object.
By the way, both option 2 and 3 have the same name, PSFTP.
Option 2 = Powershell FTP
Option 3 = Putty Secure FTP
So, I’m on to option 3. This looks a little more promising. One gotcha, however, is that calling Putty PSFTP from the Powershell ise, makes the connection but doesn’t return to show the PSFTP prompt. Here’s the command (so far)
PS> .psftp email@example.com -p 22 -pw mypassword -v -2
This command shows that:
The psftp program is located in the current directory.
firstname.lastname@example.org – is the login account used for logging into the target machine
192.168.224.185 – is the IP address of the target machine
-p 22 – is port 22, used for Secure FTP
-pw is the password
-v is verbose (upon execution it returns all the steps of the login
-2 is SSL version 2.
Running this from the command line in the ISE gives the following:
.psftp : Looking up host “192.168.214.184”
At line:1 char:1
+ .psftp email@example.com -P 22 -pw mypassword -v -2
+ CategoryInfo : NotSpecified: (Looking up host “192.168.224.184”:String) , RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Connecting to 192.168.224.184 port 22
We claim version: SSH-2.0-PuTTY_Release_0.63
Server version: SSH-2.0-OpenSSH_3.8.1p1
Using SSH protocol version 2
Doing Diffie-Hellman group exchange
Doing Diffie-Hellman key exchange with hash SHA-1
Host key fingerprint is:
ssh-rsa 1024 ce:ec:0d:c2:90:ab:5e:87:12:bf:ba:f9:78:77:89:fb
Initialised AES-256 SDCTR client->server encryption
Initialised HMAC-SHA1 client->server MAC algorithm
Initialised AES-256 SDCTR server->client encryption
Initialised HMAC-SHA1 server->client MAC algorithm
Using username “myaccount”.
Attempting keyboard-interactive authentication
Opening session as main channel
Opened main channel
Started a shell/command
Connected to 192.168.214.103
Remote working directory is /ftproot
If you run this from the ise, it returns the PS command as shown.
If you run this from a regular powershell command session, it will keep you in the PSFTP session, and you can run use the usual FTP commands….like CD, etc.
In either case, the way Putty FTP can execute scripted commands is that the script has to live in a separate batch file that is called from the command line. There doesn’t appear to be a way to pass commands from Powershell to a running Putty SFTP session. (hmm.. really?)
1. I might be able to configure the FTP server running on the Linux box to accept the connection from the PowerShell FTP module. I haven’t investigated that possibility but presumably it would be less secure in terms of encryption.
2. The script above has the name and password in clear text. Not a best practice.
3. Various versions of FTP are described on this page.