kusky kusky - 1 year ago 472
PowerShell Question

Authentication error with webclient in powershell

I'm relatively new to Powershell so really not sure where to go with this issue now. I am trying to download a file from a subversion repository and am getting the (401) Unauthorized" error. I am able to log into the site and download the file using IE using the exact Same credentials on the same machine.

$source = "http://repository/folder/File.exe"
$destination = "E:\Temp\File.exe"
$wc = New-Object System.Net.WebClient
$pwd=convertto-securestring -string "password" -AsPlainText -force
$creds=New-Object System.Management.Automation.PSCredential -ArgumentList $user, $pwd
$wc.Credentials = New-Object System.Net.NetworkCredential ($user, $Creds.GetNetworkCredential().Password,"DOMAIN")

$download=$wc.DownloadFile($source, "$destination")

Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (401) Unauthorized."

Any ideas if this is cross platform issue? And how to get around this?


Raf Raf
Answer Source

Are you using basic auth on your iis/apache? If so try this:

$source = "http://repository/folder/File.exe"
$destination = "E:\Temp\File.exe"
$wc = new-object System.Net.WebClient
$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential($user,$pwd)
$credCache.Add($source, "Basic", $creds)
$wc.Credentials = $credCache
$wc.DownloadFile($source, $destination)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download