[英]How to use use default session credentials bitstransfer proxy in powershell
我正在嘗試使用“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 。出租車
但是,當我嘗試使用defaultcredentials以避免提示它時,$ mycred = [System.Net.CredentialCache] :: DefaultCredentials
我得到的錯誤與“用戶名”有關,如下所示:Start-BitsTransfer:無法處理參數'ProxyCredential'上的參數轉換。 用戶名
如何使用默認用戶會話憑據? 我已經嘗試過我見過的其他樣本將憑據傳遞給代理,但沒有一個正常工作。 有什么建議嗎?
問候
由於處理異步文件傳輸的基礎.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.