P
P
Pascaller2016-03-14 17:06:24
PowerShell
Pascaller, 2016-03-14 17:06:24

Why does ReadToEnd() throw an error (PowerShell)?

There is a simple PowerShell script that should give a list of files in an ftp directory.

$Username = "111"
$Password = "111"
$folderPath="ftp://10.10.10.10/test/"
$credentials=New-Object System.Net.NetworkCredential($Username,$Password) 
$request = [System.Net.WebRequest]::Create($folderPath)
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails
$request.Credentials = $credentials
$response = $request.GetResponse()
$stream = $response.getresponsestream()
$reader = New-Object IO.StreamReader $response.GetResponseStream() 

$files = $reader.ReadToEnd()
print $files

$reader.Close()
$response.Close()

print $files gives a list of files, but at first it gives the error "Invalid key - """ or "Cannot find file ". At the same time, if you change the code to $reader.ReadToEnd() without assigning the output to a variable, then normal output without errors is displayed on the console. Is it possible to somehow ignore this error, and why might it occur? (I suspect this is due to the directory "./")
It looks like this:
Недопустимый ключ - ""-rw-rw-r--    1 512      100      172494727 Aug 22  2015 1.psd
-rw-rw-r--    1 512      100      29271702 May 26  2015 1220TЕ1024_5.psd
drwxrwxr-x   16 506      100          4096 Dec 09 13:49 111
-rw-rw-r--    1 512      100        139776 Jul 22  2014 2222.xls
...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question