Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
Anonymous
Not applicable

PowerShell How to capture error and assign it to a variable for Invoke-PowerBIRestMethod

I'm trying to write error to log file.. this specific API when it is unable to invoke it has the error message in the console as following

saeruva02_0-1716563312310.png

 

// Code

try

                  {

                  $datasource = Invoke-PowerBIRestMethod -url "gateways/$gatewayId/datasources/$datasourceId" -method PATCH -Body $updateDatasourceBodyStr -ErrorAction Continue

                  }

                  

            Catch

            {

               # if($_.ErrorDetails.Message) {

                  # Write-Host $_.ErrorDetails.Message

            # } 

            else {

                  # Write-Host $_

            # }

                  

                  # write-host "exception: $($_.Exception)"

                  # Resolve-PowerBIError -Last

                  

                  # Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__

                  # Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription

                  

                   # write-host "ErrorVar: $error[0]"

                  

                  Write-Host "ErrorVar: $($_.Exception.Message)"

                  

                  $ErrRespp = $($_.Exception.Message)

                  

                  $ErrRespp | select-object @{n="ErrorMsg";e={$ErrRespp}}, @{n="DataSourceID";e={$datasourceId}}, @{n="GatewayID";e={$gatewayId}} | Export-csv $ErrorPathNew -NoTypeInformation -Append -force

                  

                  # $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())

                  # $ErrRespp = $streamReader.ReadToEnd()

                  # $streamReader.Close()

                  

                  # $ErrRespp | select-object @{n="DataSourceID";e={$datasourceId}}, @{n="GatewayID";e={$gatewayId}} | Export-csv $ErrorPathNew -NoTypeInformation -Append -force

            

                  # $iserror = $true

            }

//

if I have the -ErrorAction set to Stop the error writes to the log file and if I set it to Continue the error only shows in the console but does not write to log file. This will be an iteration script I do not want the PS script set to Stop and want to set ErrorAction to Continue. I've also tried SilentlyContinue but no luck.

The Catch section in the above code has different ways to capture the error and none of it works if -ErrorAction is not set to Stop.

Looking for a feasible solution. Thanks in Advance!

3 REPLIES 3
Anonymous
Not applicable

Trying to resurface this post and see if anyone has a solution for this issue. Thanks!

 

Hi @v-yiruan-msft, could you check my above response and let me know if there is a solution. Thanks!

Anonymous
Not applicable

Hi @Anonymous ,

Please update the codes as below and check if that can help you get the expected result.

try
{
    $datasource = Invoke-PowerBIRestMethod -url "gateways/$gatewayId/datasources/$datasourceId" -method PATCH -Body $updateDatasourceBodyStr -ErrorAction Stop
}
catch
{
    # Capture the error message
    $errorMessage = $_.Exception.Message

    # Write the error message to the console
    Write-Host "Error Message: $errorMessage"

    # Now you can use $errorMessage variable as per your requirement
}

Best Regards

Anonymous
Not applicable

Hi @Anonymous

 

Thanks for the response. As I mentioned in my initial post I want this script to be iterated. If I set the -ErrorAction to Stop the iteration stops. Instead I want the -ErrorAction set to Continue and write the error to a variable and continue the iteration. 
I've tried this but the error is not captured to the variable. This is happening for Invoke-PowerBIRestMethod only.

I'm looking for a solution for this scenario. Thanks!

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.