2015-07-20 43 views
2

我对从被成功事件后调用另一个应用程序运行PowerShell脚本工作执行一系列命令:PowerShell的FTP上传不工作 - 创建零字节文件

  1. 首先,它呼啸而过指定的文件(ExampleFile
  2. 然后通过FTP删除了原始
  3. 上传的压缩文件到服务器

在FTP过程我的问题开始:一个s它存在于下面 - 在本地系统中正确创建ZIP文件,并在FTP服务器上创建一个ZIP文件,但它的文件大小为0字节。

从服务器,它看起来像上传挂起?

因此:第1-3行都正常工作,本地压缩文件的大小不为零。

Add-Type -A 'System.IO.Compression.FileSystem'; 
[IO.Compression.ZipFile]::CreateFromDirectory("ExampleFolder\ExampleFile", "ExampleFolder\ExampleFile_Datestamp.zip"); 
Remove-Item -Recurse -Force "ExampleFolder\ExampleFile"; 

$Username = "exampleusername"; 
$Password = "examplepassword"; 
$LocalFile = "C:\Users\example_path\ExampleFolder\ExampleFile.zip"; 
$RemoteFile = "ftp://exampleserver/examplepath2/ExampleFile_Datestamp.zip"; 

$FTPRequest = [System.Net.FtpWebRequest]::Create("$RemoteFile"); 
$FTPRequest = [System.Net.FtpWebRequest]$FTPRequest; 
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile; 
$FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password); 
$FTPRequest.UseBinary = $true; 
$FTPRequest.UsePassive = $true; 

$FileContent = gc -en byte $LocalFile; 
$FTPRequest.ContentLength = $FileContent.Length; 

$Run = $FTPRequest.GetRequestStream(); 
$Run.Write($FileContent, 0, $FileContent.Length); 
$Run.Close(); 
$Run.Dispose(); 

这让我非常难过,所以任何想法或想法都很感激。

powershell.exe -nologo -noprofile -command "Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory(\"ExampleFolder\ExampleFile\", \"ExampleFolder\ExampleFile_Datestamp.zip\"); Remove-Item -Recurse -Force \"ExampleFolder\ExampleFile\"; $Username = \"exampleusername\"; $Password = \"examplepassword\"; $LocalFile = \"C:\Users\example_path\ExampleFolder\ExampleFile.zip\"; $RemoteFile = \"ftp://exampleserver/examplepath2/ExampleFile_Datestamp.zip\"; $FTPRequest = [System.Net.FtpWebRequest]::Create(\"$RemoteFile\"); $FTPRequest = [System.Net.FtpWebRequest]$FTPRequest; $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile; $FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password); $FTPRequest.UseBinary = $true; $FTPRequest.UsePassive = $true; $FileContent = gc -en byte $LocalFile; $FTPRequest.ContentLength = $FileContent.Length; $Run = $FTPRequest.GetRequestStream(); $Run.Write($FileContent, 0, $FileContent.Length); $Run.Close(); $Run.Dispose(); 

回答

1

你缺少一个电话FtpWebRequest.GetResponse

... 

$Run = $FTPRequest.GetRequestStream(); 
$Run.Write($FileContent, 0, $FileContent.Length); 
$Run.Close(); 

$FTPResponse = $FTPRequest.GetResponse() 

Write-Out $FTPResponse.StatusCode 
Write-Out $FTPResponse.StatusDescription 

How to: Upload Files with FTP

+0

感谢您的回应!不幸的是,这些添加不成功。我希望我能提供一个错误或日志响应,但第三方通话的性质并没有给我太多。 ZIP文件看起来像它仍然被创建,但仍然有一个0文件大小。 此外,当我们在PowerShell中逐行运行我们的代码时,它可以正常工作:写入文件及其内容 - 只有当我们将它放入应用程序中时,才会写入内容。如果有帮助,第三部分是RETS连接器“成功”回调。 再次感谢您的帮助。 – SuperSephy

+0

尝试删除'.Dispose'调用,它不应该在那里(我已经在我的回复中)。 –

+0

我删除了$ Run.Dispose()调用,但没有明显的效果,不幸的是FTP仍然没有写入文件内容。我目前的思路可能是原始问题中存在的嵌套引号中的冲突/中断,上面的问题经过编辑后添加以反映这两种格式的完全公开和语法缓解。 – SuperSephy