【问题标题】:How to use use default session credentials bitstransfer proxy in powershell如何在 Powershell 中使用默认会话凭据比特传输代理
【发布时间】:2017-02-23 12:08:46
【问题描述】:

我正在尝试使用“start-bitstransfer”cmdlet 自动下载一些文件,但我应该使用代理。

我使用“get-credentials”没有问题下载文件没有问题,但我想避免提示当前用户会话。 $mycred=get-credential Start-BitsTransfer -proxyusage override -proxylist @("myproxy.com:8080") -proxycredential $mycred -Proxyauthentication Ntlm http://go.microsoft.com/fwlink/?LinkId=76054 .\​​wsusscn2.cab

但是当我尝试使用 defaultcredentials 来避免提示时 $mycred= [System.Net.CredentialCache]::DefaultCredentials

我收到与“用户名”相关的错误,如下所示: Start-BitsTransfer:无法处理参数“ProxyCredential”的参数转换。用户名

如何使用默认用户会话凭据?我已经尝试使用我见过的其他示例将凭据传递给代理,但没有一个有效。 有什么建议吗?

问候

【问题讨论】:

    标签: powershell proxy networkcredentials microsoft-bits credentialscache


    【解决方案1】:

    由于处理异步文件传输的底层 .NET 方法,Start-BitsTransfer 似乎无法使用默认凭据。

    如果Start-BitsTransfer 不是硬性要求,我会推荐WebClient

    创建$global:webClient对象的函数;

    function Set-WebClient {
       if(!($global:webClient)){
          try   {  $global:webClient = New-Object System.Net.WebClient     }
          catch {  $M="WebC:  Unable to setup WebClient" ; write-output $M }
          if($global:webClient){
             try   { $global:webClient.Proxy = [System.Net.WebRequest]::DefaultWebProxy }
             catch { $M="WebC:  Unable to set WebClient Proxy" ; write-output $M        }
             if($global:webClient.Proxy){
                try   { $global:webClient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials }
                catch { $M="WebC:  Unable to set WebClient Proxy Credentials for Authorization " ; write-output$M     }
             }
          }
       }
    }  ## end function Set-WebClient
    

    现在调用函数,然后让 .Net 对象下载文件。

    Set-WebClient
    
    $url    = "http://go.microsoft.com/fwlink/?LinkId=76054"
    $output = "$((Get-Location).Path)\wsusscn2-$($DT).cab"
    
    $webClient.DownloadFile($url,$output) ## downloads the file
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 2016-07-07
      • 2019-06-08
      • 1970-01-01
      相关资源
      最近更新 更多