我正在尝试从具有给定 URL 的服务器获取 CSV 文件。具有此文件的服务器具有 NTLM 授权,每次我尝试使用以下代码时都会得到:
"The remote server returned an error: (401) Unauthorized."
$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)
最佳答案
如果您想使用 NTLM
,则应指定该内容,而不是 Basic
:
$credCache.Add($source, "NTLM", $creds)
您可能还需要指定域:
$creds = new-object System.Net.NetworkCredential("user", "p@ssw0rd", "MYDOMAIN")
关于Powershell使用NTLM身份验证从服务器获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27897102/