【问题标题】:translate CMD FTP request to PowerShell FTP request将 CMD FTP 请求转换为 PowerShell FTP 请求
【发布时间】:2013-11-14 16:52:58
【问题描述】:

我有一个完整的 FTP 请求保存到 .CMD 文件。

这是我的 CMD 脚本:

@echo off
setlocal
set uname=exUsername
    set passw=exPassword
    set hostname=exHostname
    set filespec=exSpec
echo %uname%>                     test.ftp
echo %passw%>>                    test.ftp
echo pwd>>                        test.ftp
echo cd exDir>>                   test.ftp
echo binary>>                     test.ftp
echo get %filespec%>>             test.ftp
echo bye>>                        test.ftp
ftp -s:test.ftp %hostname%
if errorlevel 1 pause
endlocal

我想把上面的代码翻译成一个 PowerShell 脚本。

这是我的 PowerShell 脚本:

$uname = "exUsername"
$passw = "exPassword"
$hostname = "exHostname"
$filespec = "exSpec"
$dir = "exDir"

$uri = "ftp://$uname`:$passw@$hostname/$dir/$filespec"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($uri, "C:\")

到目前为止,我的 PowerShell 脚本不起作用。这是我的错误:

使用“2”参数调用“DownloadFile”的异常:“异常 在 We bClient 请求期间发生。”在 line:20 char:17 + $wc.DownloadFile

我最初以为我指定了错误的 FTP 地址,所以我在 Windows 资源管理器中输入了 $uri 值并进入服务器,但注意到没有(可见)文件夹可供访问。

但是,完全相同的 $uri 文件路径正在通过 CMD 脚本为我获取请求的文件。

关于我哪里出错了有什么想法吗?

编辑 - 应@KeithHill 的要求,我重新编写了脚本以使用ftpWebRequest 而不是WebClient。代码如下,但我收到以下错误:

使用“0”参数调用“GetResponse”的异常:“远程 服务器返回错误:(431) 431 未请求安全机制 此时可用。 ." 在 line:23 char:39 + $ftpresponse = $ftprequest.GetResponse

代码如下:

$uname = "exUsername"
$passw = "exPassword"
$hostname = "exHostname"
$filespec = "exSpec"
$dir = "exDir"

$uri = "exUri"


# Create an FTPWebRequest object to handle the connection to the FTP server
$ftprequest = [System.Net.FtpWebRequest]::Create($uri)

# Set the request's network credentials for an authenticated connection
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($uname,$passw)

# Set FTPWebRequest method to ListDirectory
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$ftprequest.EnableSsl = $True
$ftprequest.UseBinary = $True
$ftprequest.UsePassive = $True
$ftprequest.KeepAlive = $False

$ftpresponse = $ftprequest.GetResponse()

Write-Out $ftpresponse.StatusCode
Write-Out $ftpresponse.StatusDescription

【问题讨论】:

标签: shell powershell ftp cmd etl


【解决方案1】:

对于DownloadFile,第二个参数必须是像"c:\$filespec" 这样的文件名,而不仅仅是驱动器说明符。

【讨论】:

  • 我把它改成了$wc.DownloadFile($uri, "C:\$filespec"),但我得到了和我的帖子一样的错误。
  • 在您运行该命令并收到错误消息后,立即执行$error[0] | fl * -force 以查看是否可以获得有关异常的更多详细信息。
  • 顺便说一句,您可以尝试使用 FtpWebRequest 而不是 WebClient。
  • 其他详细信息:System.Management.Automation.MethodInvocationException: Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (501) Syntax error in parameters or arguments. 我会调查 FtpWebRequest,但如果您发现$uri 字符串有任何明显错误,请告诉我。
  • 请使用FtpWebRequest查看我的代码编辑。我也遇到了一个错误。我注释掉了Write-Out 行,并得到了以下错误(也存在于编辑中)Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (431) 431 Requ ested security mechanism not available at this time.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 2021-06-30
  • 2018-04-16
  • 1970-01-01
  • 2021-02-06
相关资源
最近更新 更多