Running from powershell

Jan 2, 2012 at 2:11 PM

Hello,

Question, when i run a ftps -g from cmd it works, when i run the same command from within powershell i get the error:

ERROR: The specified network name is no longer available.

The result is a local file with 0 bytes.

I used the following command:

.\ftps.exe -h $hostname -U $username -P $passw -ssl all -sslInvalidServerCertHandling Accept -g Orders\Order1.t ..\Orders\Order1.t

The FTP server is a IIS 7.5

Can't you run ftps from within powershell? I use the -l and -rn commands and they seem to work fine.

Regards,

Erwin van Dijk.

Coordinator
Jan 2, 2012 at 5:45 PM

Erwin,

there's no reason why it shouldn't work in PowerShell. You can even use the .Net library directly instead of the ftps.exe app for higher control.

Did u try to quote the parameters with "..."?

 

Best,

Alessandro Pilotti

[ MVP ASP.Net / IIS ]

 

Jan 3, 2012 at 4:28 AM

Alessandro,

Thx for the reply. I tried to quote it but the result is the same. I would love to use .Net library but i more off a script kiddy in Powershell :-).

Can you give me short example on how to connect to a FTP-S from powershell using the .NET lib? The rest i will find out myself.

Thx for your time.

Regards,

Erwin van Dijk

Coordinator
Jan 4, 2012 at 9:50 PM
Edited Jan 4, 2012 at 9:53 PM

Erwin,

here's an example with some basic commands.

Let me know if it's enough for you!

Best,

Alessandro

# Load the assembly (from the directory in which it is contained) 
[System.Reflection.Assembly]::LoadFrom("AlexPilotti.FTPS.Client.dll")
$c = new-object AlexPilotti.FTPS.Client.FTPSClient

$cred = new-object System.Net.NetworkCredential("yourUsername", "yourPassword")
$mode = [AlexPilotti.FTPS.Client.ESSLSupportMode]::CredentialsRequired -bor [AlexPilotti.FTPS.Client.ESSLSupportMode]::DataChannelRequested

# Open the connection
$c.Connect("ftp.yourserver.com", $cred, $mode)
# Get a directory list
$c.GetDirectoryList("your/dir/path") | select-Object Name
# Get a file
$c.GetFile("/path/to/remotefile.jpg", "c:\local\path\")
$c.Close()

Jan 5, 2012 at 8:37 PM

Alessandro,

Thx for giving me a sample.

Last question (i hope) how do i get -sslInvalidServerCertHandling Accept working in youre example? I did the dirty trick:

$c.Connect("ftp.yourserver.com", $cred, $mode , {$true})

But then when i do a GetDirectoryList i get the error:

Exception calling "GetDirectoryListUnparsed" with "1" argument(s): "X509 certificate changes between validation requests. This is not allowed"

So proberly the trick was to dirty :-)

Again thx for your time.

Regards,

Erwin

Coordinator
Jan 6, 2012 at 4:36 PM

Erwin,

your "dirty", trick is perfect, that's exactly what the -sslInvalidServerCertHandling does :-)

During my tests it works without problems. What is the value of $mode?

 

Best,

Alessandro

Jan 7, 2012 at 10:35 AM

Alessandro,

The value of $mode = [AlexPilotti.FTPS.Client.ESSLSupportMode]::All.

See the test example below. I am using a IIS 7.5 FTP-S server with a server certificate from an Windows internal Enterprise PKI. Also i host multiple host name on this server. So that’s why i use the username format username@hostname. Where hostname is the hostname in the site bindings in IIS. If i test it with a working certificate all goes well. But i want to be able to handle certificate errors.

For the record, i am very happy with your work. I currently finished my scripts for a customer of mine and the work like a charm. As long as i don‘t get certificate errors :-)

add-type -Path ".\AlexPilotti.FTPS.Client.dll"
$ftps = New-Object AlexPilotti.FTPS.Client.FTPSClient
$username = "username@hostname"
$passw = "somethingiwonttellyou"
$password = ConvertTo-SecureString -AsPlainText $passw -Force
$cred = New-Object system.net.NetworkCredential ($username, $passw)
$SSLMode = [AlexPilotti.FTPS.Client.ESSLSupportMode]::All
$connect = $ftps.Connect("ftp.someserver.com", $cred, $SSLMode, {$true} )
write-host $connect
Write-Host ("Welcome message: {0}" -f ($ftps.WelcomeMessage))
Write-Host ("Banner Message: {0}" -f ($ftps.BannerMessage))
Write-Host ("Local Certiface:")
$ftps.LocalCertificate | Format-List
Write-Host ("Remote Certificate:")
$ftps.RemoteCertificate | Format-List
Write-Host ("SSL Info:")
$ftps.SslInfo | Format-List
Write-Host ("SSL Support mode: {0}" -f ($ftps.SslSupportCurrentMode))
Write-Host ("SSL Request mode: {0}" -f ($ftps.SslSupportRequestedMode))
Write-Host ("Text encoding: {0}" -f ($ftps.TextEncoding))
Write-Host ("Transfer mode: {0}"-f ($ftps.TransferMode))

$ftps.GetDirectoryList("Orders")
$ftps.GetFile("Orders\Order1.t", "..\Orders\Order1.t")
$ftps.GetFile("Orders\Order6.t", "..\Orders\Order6.t")
$ftps.Close()
$ftps.Dispose()

The output errors are:
Exception calling "GetDirectoryList" with "1" argument(s): "X509 certificate changes between validation requests. This is not allowed"
At ***********************\test1-FTPS.ps1:23 char:23
+ $ftps.GetDirectoryList <<<< ("Orders")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "GetFile" with "2" argument(s): "Cannot issue a new command while waiting for a previous one to complete"
At ************************\test1-FTPS.ps1:24 char:14
+ $ftps.GetFile <<<< ("Orders\Order1.t", "..\Orders\Order1.t")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Exception calling "GetFile" with "2" argument(s): "Cannot issue a new command while waiting for a previous one to complete"
At *************************\test1-FTPS.ps1:25 char:14
+ $ftps.GetFile <<<< ("Orders\Order6.t", "..\Orders\Order6.t")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
Removed the pathinfo :-)

Coordinator
Jan 7, 2012 at 4:09 PM

Erwin,

I updated the sources to allow X509 certificate changes, this should solve your problem. Can you please do a test?

Best,

Alessandro 

Jan 8, 2012 at 9:48 AM

Alessandro,

Did a test and it works as expected, so i am very happy. Scripts are now production ready for me.

Thx again for your help and time, much appreciated.

Regards,

Erwin