首页 文章

PowerShell中的webclient身份验证错误

提问于
浏览
1

我对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)未经授权 . ”

如果这是跨平台问题的任何想法?以及如何解决这个问题?

谢谢

1 回答

  • 6

    你在你的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)
    

相关问题