我试图从具有给定网址的服务器获取CSV文件。具有此文件的服务器具有NTLM授权,每次我尝试使用下面的代码时,我得到:
"远程服务器返回错误:(401)未经授权。"
$source = "http://sql-001.contoso.com/ReportServer/Pages/ReportViewer.aspx?test:Format=CSV&rs:Command=Render"
$destination = "C:\test.csv"
$wc = new-object System.Net.WebClient
$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential("user","p@ssw0rd")
$credCache.Add($source, "Basic", $creds)
$wc.Credentials = $credCache
$wc.DownloadFile($source, $destination)
答案 0 :(得分:2)
如果您想使用NTLM
,那就是您应该指定的内容而不是Basic
:
$credCache.Add($source, "NTLM", $creds)
您可能还需要指定域名:
$creds = new-object System.Net.NetworkCredential("user", "p@ssw0rd", "MYDOMAIN")