Exception Powershell WebClient下载文件异常路径中包含非法字符
我试图从FTP站点下载zip文件,通过检索目录列表来查找文件名 下载部分:Exception Powershell WebClient下载文件异常路径中包含非法字符,exception,powershell,webclient,illegal-characters,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/d
$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 )
}
如果我使用注释掉的字符串并正确定义它下载的字符串。但如果我按图所示运行它,我会收到路径中非法字符的异常错误。有趣的是,写得冗长和不写之间有区别
运行时的输出,如图所示:
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
文件下载得很好。当然,一旦我发布了问题,我就明白了。My$数组包含
`n
和`r
字符。我需要找到并替换它们
$array=$array -replace "`n",""
$array=$array -replace "`r",""