Powershell: ftp directory listing (script help)

I am trying to make a ftp directory list view. While the part is being viewed, but I can not manipulate the data that I return. Here is the script I used;

[System.Net.FtpWebRequest]$ftp = [System.Net.WebRequest]::Create("ftp://ftp.microsoft.com/ResKit/y2kfix/alpha/") $ftp.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory #Details $response = $ftp.getresponse() $stream = $response.getresponsestream() $buffer = new-object System.Byte[] 1024 $encoding = new-object System.Text.AsciiEncoding $outputBuffer = "" $foundMore = $false ## Read all the data available from the stream, writing it to the ## output buffer when done. do { ## Allow data to buffer for a bit start-sleep -m 1000 ## Read what data is available $foundmore = $false $stream.ReadTimeout = 1000 do { try { $read = $stream.Read($buffer, 0, 1024) if($read -gt 0) { $foundmore = $true $outputBuffer += ($encoding.GetString($buffer, 0, $read)) } } catch { $foundMore = $false; $read = 0 } } while($read -gt 0) } while($foundmore) $outputBuffer 

Here is the answer I am returning for this script;

 PS C:\Users\Toshiba> C:\Apps\@PowerShell\FTPListDirectory.ps1 forfiles.exe logtime.exe timeserv w32time 

From there, how can I work on the data that is being returned. file information (name, creation time, last update, etc.) and other materials.

My goal here is to view all the ftp data in the directory, and then I can upload all the files to the directory.

Is there any chance?

+6
source share
1 answer

You must get all the data first.

Change the method to

 $ftp.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails 

Without data, you only get file names.

To upload a file, I usually use the webclient class

 $webclient = New-Object System.Net.WebClient $webclient.credentials = New-Object System.Net.NetworkCredential("anonymous"," anonymous@test.com ") $webclient.downloadfile("ftp://ftp.host.com/file.txt", "c:\temp\file.txt") 

Downloadfile method of webclient class

+8
source

Source: https://habr.com/ru/post/894991/


All Articles