%5B
替换它。不可能更改密码。
cd v:
$username = "*********"
$password = "*********"
$usrpass = $username + ":" + $password
$webclient = New-Object -TypeName System.Net.WebClient
function ftp-test
{
if (Test-Path v:\*.204)
{
$files = Get-ChildItem v:\ -name -Include *.204 | where { ! $_.PSIsContainer } #gets list of only the .204 files
foreach ($file in $files)
{
$ftp = "ftp://$usrpass@ftp.example.com/IN/$file"
Write-Host $ftp
$uri = New-Object -TypeName System.Uri -ArgumentList $ftp
$webclient.UploadFile($uri, $file)
}
}
}
ftp-test
当我运行上面的代码时,我得到了
Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:13 char:34
+ $webclient.UploadFile <<<< ($uri, $file)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
我不确定问题是什么。搜索带来了代理问题,但我没有代理需要通过。
我可以使用ftp.exe
手动上传文件,但如果可能的话,我宁愿在PowerShell中执行所有操作,而不是生成使用ftp.exe
的脚本。
答案 0 :(得分:1)
你必须URL-encode特殊字符。 请注意,经过编码的斜杠(/
)为%2F
,而不是%5B
(即[
)。
$usrpass = $username + ":" + [System.Uri]::EscapeDataString($password)
或使用WebClient.Credentials
property,您无需逃避任何事情:
$webclient.Credentials = New-Object System.Net.NetworkCredential($username, $password)
...
$ftp = "ftp://ftp.example.com/IN/$file"