Powershell WebClient下载文件异常路径中的非法字符

时间:2014-03-07 17:32:16

标签: exception powershell webclient illegal-characters

我试图从FTP站点下载zip文件,基于检索目录列表来查找文件名。

下载部分:

$folderPath='ftp://11.111.11.11/'
$target = "C:\Scripts\ps\ftpdl\"

Foreach ($file in ($array | where {$_ -like "data.zip"})) {

$Source = $folderPath+$file
$Path = $target+$file

#$Source = "ftp://11.111.11.11/data.zip"
#$Path = "C:\Scripts\ps\ftpdl\data.zip"

$source
Write-Verbose -Message $Source -verbose
$path
Write-Verbose -message $Path -verbose

$U = "User"
$P = "Pass"
$WebClient2 = New-Object System.Net.WebClient
$WebClient2.Credentials = New-Object System.Net.Networkcredential($U, $P)
$WebClient2.DownloadFile( $source, $path )  
}

如果我使用注释掉并定义它正确下载的字符串。但是,如果我按照所示运行它,我会收到路径中的异常错误非法字符。有趣的是,write-verbose与not之间存在差异。

如图所示运行时输出:

ftp://11.111.11.11/data.zip
data.zip
C:\Scripts\ps\ftpdl\data.zip
data.zip
Exception calling "DownloadFile" with "2" .........

使用硬编码路径运行时输出&源

ftp://11.111.11.11/data.zip
VERBOSE: ftp://11.111.11.11/data.zip
C:\Scripts\ps\ftpdl\data.zip
VERBOSE: C:\Scripts\ps\ftpdl\data.zip    

该文件下载得非常好。

1 个答案:

答案 0 :(得分:2)

嗯,当然,一旦我发布了问题,我就知道了。我的$数组包含`n`r个字符。我需要找到并替换它们。

$array=$array -replace "`n",""
$array=$array -replace "`r",""