Andrei Iulian BARBU
Andrei Iulian BARBU

Reputation: 1

Powershell FTP Upload with resume

I'm having a rather interesting problem.

I've created a script to zip and upload(with resume) some files onto an FTP account. Locally in my machine it works fine but in a production environment I get this error:

PS> $ftp.Put($fileStream,$file.name,$true);

Exception calling "Put" with "3" argument(s): "Could not find file 'C:\ByAndrew\PowerShell\Transactions\System.IO.FileStream'."

At line:1 char:9

Here is the script:

$xml = New-Object XML

$xml.Load(".\settings.xml")

[void][Reflection.Assembly]::LoadFrom("D:\Work\Projects\ProjectIndyFTP\Bin\Indy.Sockets.dll")

$ftp = new-object Indy.Sockets.FTP

$ftp.Host = $xml.list.ftp.server

$ftp.Port = $xml.list.ftp.port

$ftp.Username = $xml.list.ftp.user

$ftp.Password = $xml.list.ftp.pass

$ftp.ConnectTimeout = 600

$ftp.Connect()

$file = Get-Item "D:\Work\Projects\ProjectIndyFTP\TestFolder\TestFiles\testfile5M.bin"
#$fileStream = $file.OpenRead()
$fileStream = New-Object System.IO.FileStream($file,[System.IO.FileMode]::Open,[System.IO.FileAccess]::Read,[System.IO.FileShare]::ReadWrite)
$ftp.Put($fileStream,$file.name,$true);
$fileStream.Close()
$ftp.Quit()
exit

<<<<<<<<<<<<<<<<<<<< I don't understand the error. What's with "Could not find file 'C:\ByAndrew\PowerShell\Transactions\System.IO.FileStream'." I mean 'path\object' Has anyone encounter this behavior before? Any tips?

Best regards, Andrew


Updates:

Today I've check the referenced assembly version from the production environment: mscoree.dll [2.0.50727.1433] - .net 20 sp1 Locally I had [2.0.50727.3053] - .net 20 sp2 In order to exclude this as an issue I looked up a machine with .net 20 sp1. reproduced the error -> updated .net to 2.0 sp2 and checked it. Same problem. That's why I said.. I don't think the "dependency" is faulty.

Regards, Andrew

Upvotes: 0

Views: 1261

Answers (1)

Goyuix
Goyuix

Reputation: 24340

Why do you call $file.OpenRead() and then in the next line create a new FileStream object? Also, none of the overloads for a new FileStream take a FileInfo object as the first argument to the constructor. I could see you using $file.FullName as the first argument though I would look at those lines as suspect before even getting to the production/dev differences...

If you don't need all the special attributes instructing .NET how to open the file, I would just use the .OpenRead() method - it already returns a FileStream object. If you need those options, then delete the OpenRead() line.

Also, it would be a good idea to include a $fileStream.Dispose() call after your call .Close() - or even in place of it as Dispose will close the stream as well. This has the benefit of releasing the associated handle as well.

Upvotes: 1

Related Questions