REST api call using Start-BitsTransfer

Hi, I’m working on re-writing my code to make a REST api call Start-BitsTransfer instead of the Invoke-RestMethod @params | ConvertTo-Json | Out-File $DestFile and I would like to have my payload saved as JSON document. Invoke-RestMethod works fine when the data volume is not too big. When the volume of data gets bigger I’m running into all sorts of performance issues.

$Header = @{
    "authorization" = "Bearer $token"
#make REST API call
$Parameters = @{
    Method      = "GET"
    Uri         = ""
    Headers     = $Header
    ContentType = "application/json"
    Body        = $BodyJson
Start-BitsTransfer -Priority High -TransferType Download -Source $Parameters -Destination $DestFile | Complete-BitsTransfer;


Start-BitsTransfer : Cannot find path 'C:\Users\uname\System.Collections.Hashtable' because it does not exist.
At line:62 char:5
+     Start-BitsTransfer -Priority High -TransferType Download -Source  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\uname...tions.Hashtable:String) [Start-BitsTransfer], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferCommand`Preformatted text`

Thank you

The parameter -Source takes a string or an array of strings with the source file paths. If you have more than one file to download you have to either provide a list of files or use wildcards.

@Olaf , I have only one file to download. Not too sure why I get that error.

Because you provided a hastable instead of a string. For the parameter -Source you have to provide a complete file path.

Hi @Olaf , How do I pass an URL with parameters to the source in order to make the api call.

AFAIK you can’t. Please read the help completely including the examples. I’d try to play around with the parameters -Authentication and -CustomHeaders.

Hi @Olaf , I am using System.Net.WebClient instead in the below code.

    $wc = New-Object System.Net.WebClient
    $wc.Headers.Add('Authorization', "Bearer $tokenurl")
    $temp = $wc.DownloadString($apiurl)
    $temp | ConvertTo-Json | Out-File $outfile

Please let me know your thoughts.

Hi @Olaf I updated my code for error handling but I don’t capture the actual error message.

    $wc = New-Object System.Net.WebClient
    $wc.Headers.Add('Authorization', "Bearer $tokenurl")
    $temp = $wc.DownloadString($apiurl)
    $temp | ConvertTo-Json | Out-File $outfile
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    ((Get-Date).ToString() + " | Error: " + $ErrorMessage + " - " + $FailedItem);

For the try block to work properly there has to be a terminating error. If the cmdlet you’re using does not raise a terminating error by default you have to force it with the parameter -ErrorAcction Stop. If you’re using a dotNet method instead of cmdlets it gets even more complex. I’d urgently recommend to stay with PowerShell cmdlets and only use dotNet or other methods if PowerShell cmdlets are not available or not suitable for your particular requirements.

To call a REST API there is the cmdlet

Please read the help completely including the examples to learn how to use it.