PowerShell FTP上传不起作用 – 创build零字节文件

我正在处理从成功事件后调用的另一个应用程序运行的PowerShell脚本,并执行一系列命令:

  1. 首先它将一个指定的文件( ExampleFile
  2. 然后删除原来的
  3. 通过FTP将压缩file upload到服务器

我的问题在FTP进程中开始:因为它存在于下面 – 在本地系统中正确地创build了ZIP文件,并且在FTP服务器上创build了一个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(); 

您错过了对FtpWebRequest.GetResponse的调用:

 ... $Run = $FTPRequest.GetRequestStream(); $Run.Write($FileContent, 0, $FileContent.Length); $Run.Close(); $FTPResponse = $FTPRequest.GetResponse() Write-Out $FTPResponse.StatusCode Write-Out $FTPResponse.StatusDescription 

请参阅如何:使用FTP上传文件 。