How to use use default session credentials bitstransfer proxy in powershell

1.8k views Asked by At

I'm trying to automate the download of some files using "start-bitstransfer" cmdlet, but I should use a proxy.

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. $mycred=get-credential Start-BitsTransfer -proxyusage override -proxylist @("myproxy.com:8080") -proxycredential $mycred -Proxyauthentication Ntlm http://go.microsoft.com/fwlink/?LinkId=76054 .\wsusscn2.cab

But when I'm trying to use the defaultcredentials to avoid prompting for it $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'. 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

1

There are 1 answers

0
Signal15 On

It appears that Start-BitsTransfer is unable to use default credentials because of the underlying .NET method that handles asynchronous file transfers.

If Start-BitsTransfer is not a hard requirement, I would recommend WebClient.

A function to create a $global:webClient object;

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.

Set-WebClient

$url    = "http://go.microsoft.com/fwlink/?LinkId=76054"
$output = "$((Get-Location).Path)\wsusscn2-$($DT).cab"

$webClient.DownloadFile($url,$output) ## downloads the file