Forum Discussion
Anonymous
2 years agoNot 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 // Code try { ...
Anonymous
2 years agoNot 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
2 years agoNot 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!