【问题标题】:Authentication error with webclient in powershellpowershell 中的 webclient 的身份验证错误
【发布时间】:2014-06-04 11:34:57
【问题描述】:

我对 Powershell 比较陌生,所以现在真的不知道该去哪里解决这个问题。我正在尝试从 subversion 存储库下载文件并收到 (401) Unauthorized" 错误。我能够登录网站并使用 IE 在同一台机器上使用完全相同的凭据下载文件。

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

使用“2”参数调用“DownloadFile”的异常:“远程服务器返回错误:(401) 未经授权。”

如果这是跨平台问题,有什么想法吗?以及如何解决这个问题?

谢谢

【问题讨论】:

    标签: authentication powershell webclient


    【解决方案1】:

    您是否在 iis/apache 上使用基本身份验证?如果是这样试试这个:

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

    【讨论】:

    • 感谢您的快速响应。我们确实在 Apache 和上述工作上使用了基本身份验证。
    猜你喜欢
    • 2013-09-03
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多