[英]How to use use default session credentials bitstransfer proxy in powershell
I'm trying to automate the download of some files using "start-bitstransfer" cmdlet, but I should use a proxy. 我正在尝试使用“start-bitstransfer”cmdlet自动下载某些文件,但我应该使用代理。
Wen I use "get-credentials" there is no problem there is no problem to download the file, but I'd like to avoid prompting for the current user session. 我使用“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 $ mycred = get-credential Start-BitsTransfer -proxyusage override -proxylist @(“myproxy.com:8080”)-proxycredential $ mycred -Proxyauthentication Ntlm http://go.microsoft.com/fwlink/?LinkId =76054。\\ wsusscn2 。出租车
But when I'm trying to use the defaultcredentials to avoid prompting for it $mycred= [System.Net.CredentialCache]::DefaultCredentials 但是,当我尝试使用defaultcredentials以避免提示它时,$ mycred = [System.Net.CredentialCache] :: DefaultCredentials
I'm getting and error related with the "username" like this: Start-BitsTransfer : Cannot process argument transformation on parameter 'ProxyCredential'. 我得到的错误与“用户名”有关,如下所示:Start-BitsTransfer:无法处理参数'ProxyCredential'上的参数转换。 userName 用户名
How can I use the default user session credential ? 如何使用默认用户会话凭据? I've tried with other samples I've seen to pass credentials to proxy, but none was working. 我已经尝试过我见过的其他样本将凭据传递给代理,但没有一个正常工作。 Any suggestion? 有什么建议吗?
Regards 问候
It appears that Start-BitsTransfer
is unable to use default credentials because of the underlying .NET method that handles asynchronous file transfers. 由于处理异步文件传输的基础.NET方法, Start-BitsTransfer
似乎无法使用默认凭据。
If Start-BitsTransfer
is not a hard requirement, I would recommend WebClient
. 如果Start-BitsTransfer
不是硬盘要求,我会推荐WebClient
。
A function to create a $global:webClient
object; 一个创建$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
Now call the function, then have the .Net object download the file. 现在调用该函数,然后让.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.