Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help in downloading files from FTPS server via PowerShell with Implicit and having specific host fingerprint.

I have written a code like below

$user = 'Username'
$pass = 'password'
$folder = 'UST/Download'
$target = "D:\TCS_test"
$ftp = "ftps2-temp.tcsglobal.its.fincorp.net/TCS/Download"

#SET CREDENTIALS
$credentials = new-object System.Net.NetworkCredential($user, $pass)

function Get-FtpDir ($url,$credentials) {
$request = [Net.WebRequest]::Create("ftps2-temp.tcsglobal.its.fincorp.net/TCS/Download")
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
$request.EnableSsl = $True
$request.UseBinary = $False
$request.UsePassive = $True
$request.KeepAlive = $False
if ($credentials) { $request.Credentials = $credentials }
$response = $request.GetResponse()
$reader = New-Object IO.StreamReader $response.GetResponseStream()
$reader.ReadToEnd()
$reader.Close()
$response.Close()
}


$folderPath= $ftp
Write-Output $folderPath
$Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
$files = ($Allfiles -split "`r`n")

$files

$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
$counter = 0
foreach ($file in ($files | where {$_ -like "*.*"})){

Write-Output "file name"
Write-Output $file

$source=$folderPath + $file
$destination = $target + $file

Write-Output "source"
Write-Output $source

Write-Output "destination"
Write-Output $destination

$webclient.DownloadFile($source, $target+$file)

#PRINT FILE NAME AND COUNTER
$counter++
$counter
$source
}

What I have tried:

I am getting the below Error. I am not sure where to use
Implicit HostCertificateFingerprint. I have specific given fingerprint.

Exception calling "GetResponse" with "0" argument(s): "The remote name could not be resolved: 'ftps2-temp.tcsglobal.its.fincorp.net'"
At line:19 char:9
+         $response = $request.GetResponse()
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException
 
You cannot call a method on a null-valued expression.
At line:20 char:9
+         $reader = New-Object IO.StreamReader $response.GetResponseStr ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:21 char:9
+         $reader.ReadToEnd()
+         ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:22 char:9
+         $reader.Close()
+         ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:23 char:9
+         $response.Close()
+         ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900