使用 PowerShell/WebClient 中的绝对路径从 FTP 服务器复制文件

Copying a file from an FTP server using an absolute path in PowerShell/WebClient

我正在尝试使用 PowerShell 脚本将文件从 HP-UX 复制到我的 Windows 机器。

请在下面找到我的脚本。

$File = "d:\copiedfile.txt"
$ftp = "ftp://my_Unix_Domain_name/tmp/sourceFile.txt"
"ftp url: $ftp"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Downloading $File..."
$webclient.DownloadFile($uri, $File)

我可以通过 FTP 连接,但是文件没有复制到我的目标目录。

我遇到一个错误:

The remote server return ed an error: (550) File unavailable (e.g., file not found, no access).

不确定,有什么问题。

我可以使用命令行下载文件 ftp:

ftp> get /tmp/text.sh
200 PORT command successful.
150 Opening ASCII mode data connection for /tmp/test.sh (71 bytes).
226 Transfer complete.
ftp: 76 bytes received in 0.00Seconds 76000.00Kbytes/sec.
ftp>

FTP(WebClientFtpWebRequest)的 .NET 实现不将主机名和文件路径之间的斜杠视为文件路径的一部分。

因此,如果您需要在 URL 中使用文件的绝对路径(如 /tmp/sourceFile.txt),则必须添加另一个斜线:

$ftp = "ftp://my_Unix_Domain_name//tmp/sourceFile.txt"