Search: 
NetCmdlets - FTP/File Transfer

File Transfer

There are several NetCmdlets that can be used for file transfer such as get/send-ftp, get/send-tftp, get-http, get/send-webdav, and more. Probably the most commonly used file transfer method is still FTP. The get-ftp and send-ftp NetCmdlets are used to download and upload files from an FTP server. The NetCmdlets get/set-ftp cmdlets include advanced features that give you the most flexibility, such as ssl and ssh support, multiple authentication modes, transfer resume, proxy/firewall support, etc.

Below is a basic example of using get-ftp to download a file from ftp.microsoft.com:

PS C:\>get-ftp -server ftp.microsoft.com -user anonymous 
      -password lance@ -localfile C:\readme.txt -remotefile /deskapps/readme.txt
And now here is another example using set-ftp to upload that same file to another ftp server on my local network:
PS C:\> set-ftp -server 10.0.1.1 -user test -password kotplot 
      -localfile C:\readme.txt -remotefile readme.txt

 

Listing Files

By default, if you only specify the server and authentication details (plain user/password, ssh user/password, ssh cert/password, or PSCredential), the cmdlet will simply establish a plain FTP connection and perform a file directory listing and return the results.  You can turn on ssl or ssh functionality using the optional -ssl or -ssh parameters, as shown here:

  • Plain FTP connection:

    PS C:\> get-ftp -server myserver -user lancer -password mypass
  • SSL connection:

    PS C:\> get-ftp -server myserver -user lancer -password mypass -ssl explicit

    "Explicit SSL" is when a tcp connection is established, and an FTP command is sent (prior to authentication) explicitly telling the server to enter SSL mode.  As such, this is done on the same port as regular FTP connections (usually port 21).  "Implicit SSL" is when SSL is assumed from the start by both parties, for this reason a separate port is typically used which you can specify via the -port parameter (usually port 990).

    An optional SSLAccept parameter may be specified to tell the cmdlet to automatically accept the SSL certificate presented by the server during the SSL handshake process.  If this parameter is not specified, the cmdlet will prompt the user to accept the server certificate.  If the SSLAccept parameter is used, it must be set to the base64 encoded public key of the trusted certificate, ie: get-ftp -server monkey -cred $testcred -ssl explicit -sslaccept $mycert
  • SSH connection:

    PS C:\> get-ftp -server myserver -user lancer -password mypass -ssh

    In addition to SSH password authentication as shown in the above example, public key authentication is also supported through the use of a set of -cert* parameters used to point to a local certificate.

    An optional SSHAccept parameter may be specified to tell the cmdlet to automatically accept the host key presented by the server during the SSH handshake process.  If this parameter is not specified, the cmdlet will prompt the user to accept the server host key.  If the SSHAccept parameter is used, it gets set to the hex encoded fingerprint of the host key, ie: get-ftp -server monkey -cred $testcred -ssh -sshaccept 59:52:C8:DB:C8:3A:FE:CF:9D:02:E3:31:3A:2C:11:E4

Examples:

Find the files on a remote server that are larger than 10 megs:
PS C:\> get-ftp -server myserver -user lancer -password mypass | 
	where { $_.FileSize -gt 10MB }

Delete all files on a remote FTP server that are greater than 10 megs:

send-ftp -server myserver -user lancer -password mypass -delete filetodelete.txt

Calculate the number of bytes in the files of a remote server directory:

PS C:\> get-ftp -server myserver -user lancer -password mypass -path / | 
	measure-object -property FileSize -sum

Transferring Files

The get-ftp component allows you to download files from a remote FTP server simply by specifying the remotefile parameter in addition to the connection parameters.  If a remotefile is specified, the cmdlet will begin downloading the file to the client.  If a localfile parameter is specified, the data will be written to that location, otherwise the cmdlet will attempt to write the localfile in the current directory using the same file name as the remote file.

Likewise, the send-ftp cmdlet allows you to upload files to a remote FTP server simply by specifying the localfile parameter.  To set a custom name for the remotefile, simply specify the remotefile parameter as well.

Examples:

Find all the local .txt files and upload them to a remote FTP server:
PS C:\> get-childitem *.txt | select Name | set-ftp -server myserver 
	-user lancer
-password mypassword -localfile $_ -remotefile $_

Recursively upload the contents of a local directory:

param( [string] $dir = "C:\Testing\FTPTest\" )
$files = (get-childitem $dir -r)
foreach ($file in $files) {
  $remfilename = $file.FullName.Replace($dir, "")
  $remfilename = $remfilename.Replace("\", "/")
  if ($file.Attributes -eq "Directory") {
    send-ftp -server MYSERVER -user TEST -password TEST -create $remfilename
  }
  else {
    send-ftp -server MYSERVER -user TEST -password TEST -localfile 
	$file.FullName -remotefile
    $remfilename
  }
  Write-Host $remfilename
}

NetCmdlets

We appreciate your feedback.  If you have any questions, comments, or suggestions about this article please contact our support team.

| About | Privacy Policy | Terms of Use |
© Copyright 2008 /n software inc.