Powershell FTP 上传简历

发布于 2024-12-04 19:35:19 字数 1557 浏览 1 评论 0原文

我有一个相当有趣的问题。

我创建了一个脚本来将一些文件压缩并上传(带有简历)到 FTP 帐户。在我的机器本地,它工作正常,但在生产环境中,我收到此错误:

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

使用“3”参数调用“Put”时出现异常:“找不到文件'C:\ ByAndrew \ PowerShell \ Transactions \ System.IO。文件流'。”

在行:1 字符:9

  • $ftp.Put( <<<< $fileStream,$file.name,$true);

这是脚本:

$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

<<<<<<<<<<<<<<<<<<<< 我不明白这个错误。 “找不到文件 'C:\ByAndrew\PowerShell\Transactions\System.IO.FileStream'”是怎么回事。 我的意思是“路径\对象” 以前有人遇到过这种行为吗?有什么建议吗?

此致, 安德鲁


更新:

今天我检查了生产环境中引用的程序集版本: mscoree.dll [2.0.50727.1433] - .net 20 sp1 本地我有 [2.0.50727.3053] - .net 20 sp2 为了排除这个问题,我查找了一台带有 .net 20 sp1 的机器。重现错误->将.net更新到2.0 sp2并检查它。同样的问题。这就是为什么我说..我不认为“依赖”是错误的。

问候, 安德鲁

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

  • $ftp.Put( <<<< $fileStream,$file.name,$true);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹淡然 2024-12-11 19:35:19

为什么调用 $file.OpenRead() 然后在下一行创建一个新的 FileStream 对象?此外,新 FileStream 的任何重载都不将 FileInfo 对象作为构造函数的第一个参数。我可以看到您使用 $file.FullName 作为第一个参数,尽管我会在讨论生产/开发差异之前将这些行视为可疑...

如果您不需要指示 .NET 如何打开的所有特殊属性文件,我只会使用 .OpenRead() 方法 - 它已经返回一个 FileStream 对象。如果您需要这些选项,请删除 OpenRead() 行。

另外,最好在调用 .Close() 之后包含 $fileStream.Dispose() 调用,甚至代替它,因为 Dispose 也会关闭流。这样做的好处是还可以释放关联的句柄。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文