Difference between revisions of "Using Powershell for ftp 2"

From MyWiki
Jump to: navigation, search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
http://stackoverflow.com/questions/5394090/is-it-possible-to-do-active-mode-ftp-using-ftpwebrequest<br>
 
  
 
<source lang="powershell">
 
<source lang="powershell">
 
// Get the object used to communicate with the server.
 
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
 
request.Method = WebRequestMethods.Ftp.UploadFile;
 
 
// This example assumes the FTP site uses anonymous logon.
 
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
 
request.UsePassive = false;      //////// ADDed this but not tested
 
 
// Copy the contents of the file to the request stream.
 
StreamReader sourceStream = new StreamReader("testfile.txt");
 
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
 
sourceStream.Close();
 
request.ContentLength = fileContents.Length;
 
 
Stream requestStream = request.GetRequestStream();
 
requestStream.Write(fileContents, 0, fileContents.Length);
 
requestStream.Close();
 
 
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
 
response.Close();
 
 
 
</source>
 
 
<source lang="powershell>
 
  
 
$Server = "ftp://ftp.example.com/"
 
$Server = "ftp://ftp.example.com/"
Line 86: Line 59:
  
 
}
 
}
 +
 +
</source>
 +
 +
<source lang="powershell">
 +
 +
 +
  
 
</source>
 
</source>

Latest revision as of 12:24, 6 August 2015

$Server = "ftp://ftp.example.com/"
$User = "anonymous@example.com"
$Pass = "anonymous@anonymous.com"
 
Function Get-FtpDirectory($Directory) {
 
    # Credentials
    $FTPRequest = [System.Net.FtpWebRequest]::Create("$($Server)$($Directory)")
    $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($User,$Pass)
    $FTPRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails
 
    # Don't want Binary, Keep Alive unecessary.
    $FTPRequest.UseBinary = $False
    $FTPRequest.KeepAlive = $False
 
    $FTPResponse = $FTPRequest.GetResponse()
    $ResponseStream = $FTPResponse.GetResponseStream()
 
    # Create a nice Array of the detailed directory listing
    $StreamReader = New-Object System.IO.Streamreader $ResponseStream
    $DirListing = (($StreamReader.ReadToEnd()) -split [Environment]::NewLine)
    $StreamReader.Close()
 
    # Remove first two elements ( . and .. ) and last element (\n)
    $DirListing = $DirListing[2..($DirListing.Length-2)] 
 
    # Close the FTP connection so only one is open at a time
    $FTPResponse.Close()
 
    # This array will hold the final result
    $FileTree = @()
 
    # Loop through the listings
    foreach ($CurLine in $DirListing) {
 
        # Split line into space separated array
        $LineTok = ($CurLine -split '\ +')
 
        # Get the filename (can even contain spaces)
        $CurFile = $LineTok[8..($LineTok.Length-1)]
 
        # Figure out if it's a directory. Super hax.
        $DirBool = $LineTok[0].StartsWith("d")
 
        # Determine what to do next (file or dir?)
        If ($DirBool) {
            # Recursively traverse sub-directories
            $FileTree += ,(Get-FtpDirectory "$($Directory)$($CurFile)/")
        } Else {
            # Add the output to the file tree
            $FileTree += ,"$($Directory)$($CurFile)"
        }
    }
 
    Return $FileTree
 
}