![通过我们的防火墙进行身份验证,使用 powershell 脚本下载文件](https://linux22.com/image/1597236/%E9%80%9A%E8%BF%87%E6%88%91%E4%BB%AC%E7%9A%84%E9%98%B2%E7%81%AB%E5%A2%99%E8%BF%9B%E8%A1%8C%E8%BA%AB%E4%BB%BD%E9%AA%8C%E8%AF%81%EF%BC%8C%E4%BD%BF%E7%94%A8%20powershell%20%E8%84%9A%E6%9C%AC%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6.png)
我需要一个 powershell 脚本来从网站下载文件。我们通常需要先打开浏览器并验证网站身份。我怎样才能通过非预期的脚本做到这一点?谢谢
我已在线尝试了多个版本的脚本,但没有成功。
$source = "http://chocolatey.org/install.ps1"
$dest = "C:\temp\chocolatey_install.ps1"
$WebClient = New-Object System.Net.WebClient
$WebProxy = New-Object System.Net.WebProxy("http://chocolatey.org:8080",$true)
#$WebProxy.Credentials = New-Object Net.NetworkCredential("user","password","domain.local")
$WebProxy.Credentials = New-Object Net.NetworkCredential("$user","$password","")
$WebClient.Proxy = $WebProxy
$WebClient.DownloadFile($source,$dest)
答案1
也许您应该自己发送授权标头,就像这个(未经测试的)代码:
$uri = "{url here}"
$username = "{username here}"
$password = "{password here}"
$b = [System.Text.Encoding]::UTF8.GetBytes($username + ":" + $password)
$p = [System.Convert]::ToBase64String($b)
$creds = "Basic " + $p
Invoke-WebRequest -Uri $uri -Method Get -Headers @{"Accept" = "*/*"; "Authorization" = $creds}