通过我们的防火墙进行身份验证,使用 powershell 脚本下载文件

通过我们的防火墙进行身份验证,使用 powershell 脚本下载文件

我需要一个 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} 

相关内容