简体   繁体   中英

How to catch specific start-bitstransfer "Proxy authentication is required" exception?

No matter what I do I can't catch this type of exception:

Start-BitsTransfer : HTTP status 407: Proxy authentication is required.
At line:4 char:6
+      Start-BitsTransfer -Source $url -Destination $fullPath
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-BitsTransfer], Exception
    + FullyQualifiedErrorId : StartBitsTransferCOMException,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferCommand

I cannot use " invoke-command" and [System.Management.Automation.RuntimeException] (PSRemotingTransportException) because BITS is not supported when running remote scriptblock.

How to do it?

Not a direct answer to your question, but as a pre-emptive solution you can check to see if proxy authentication is required before attempting to start your BITS transfer. It will not catch that error, but it will effectively test for the same condition and allow you to compensate for it.

What you can do is create a System.Net.WebClient object and see if it requires proxy authentication or not:

$WC = New-Object System.Net.WebClient
$ProxyAuth = !$WC.Proxy.IsBypassed("http://www.stackoverflow.com")

Now if you're behind a proxy $ProxyAuth will be true, so you can test against that as needed and if you're running PSv3 or higher you can setup the default parameters for Start-BitsTransfer to use the credentials provided:

If($ProxyAuth){
    $ProxyCred = Get-Credential -Message "Enter credentials for Proxy Authentication"
    $PSDefaultParameterValues.Add("Start-BitsTransfer:ProxyAuthentication","Basic")
    $PSDefaultParameterValues.Add("Start-BitsTransfer:ProxyCredential",$ProxyCred)
}

Now you can run the script and if proxy authentication is needed it will ask for credentials, then perform your transfers.

A lot of this was gotten from, or adapted from PowerShell Magazine .

What have you tried? From the posted snippit, One could reasonably assume that using a normal TryCatch block would work when using an ErrorAction of STOP...

Try{ Start-BitsTransfer -ea STOP ....;}catch{ DoSomething;}

Since this is the top internet search result for this problem and the post is so old here is a solution that has worked for me:

 try { Start-BitsTransfer -Source 'https://go.microsoft.com/fwlink/?linkid=2088631' -Destination 'C:\\Windows\\Temp\\Net4.8.exe' -ErrorAction Stop } catch [System.Exception] { if($error[0].exception.message -match 'HTTP status 407:') { 'Cannot transfer file with BITS as Proxy authentication is required' } Else { 'Failed to transfer with BITS. Here is the error message:' $error[0].exception.message } break } catch { 'Failed to transfer with BITS. Here is the error message:' $error[0].exception.message }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM