Authentication error with webclient in powershell

六眼飞鱼酱① 提交于 2019-12-24 10:32:26

问题


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
$user="user"
$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?

Thanks


回答1:


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)


来源:https://stackoverflow.com/questions/24036318/authentication-error-with-webclient-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!