Powershell get file from server using NTLM auth

ntlmpowershell

I'm trying to get CSV file from a server with given url. Server with this file has NTLM authorization, and each time I try to use below code I get:

"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)

Best Answer

If you want to use NTLM, that's what you should specify instead of Basic:

$credCache.Add($source, "NTLM", $creds)

Also you might need to specify the domain:

$creds = new-object System.Net.NetworkCredential("user", "p@ssw0rd", "MYDOMAIN")