我尝试从https网址下载文件时遇到以下错误。我正在使用Power Shell 3.0
Shell脚本
$Username = 'user'
$Password = 'password'
$Url = "url"
$Path = "path"
$WebClinet = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
$WebClient.DownloadFile( $Url, $Path )
错误:
异常调用" DownloadFile"用" 2"参数:" WebClient请求期间发生异常。" 在行:7 char:1 + $ WebClient.DownloadFile($ Url,$ Path)
有什么建议吗?
答案 0 :(得分:0)
在try catch块中包装现有的powershell代码并写出异常详细信息,以便了解导致问题的实际错误。
Try
{
$Username = 'user'
$Password = 'password'
$Url = "url"
$Path = "path"
$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
$WebClient.DownloadFile( $Url, $Path )
}
Catch [Exception]
{
Write-Host $_.Exception | format-list -force
}
你的代码中也有拼写错误。检查变量名称。
答案 1 :(得分:0)
在脚本中添加了以下命令,现在工作正常
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12