2

I am having difficulty in checking if a group exists or not via a Rest API.

Call the Api to check if the Group exists. My logic: Use the 'Invoke-RestMethod' to call the API. If the API call is successful (Group exists) it will store the result in '$reader_response_success', if the group does not exist, the cmdlet will fail, the error details are stored in the Error Variable '$reader_response_failure'.

Depending on which variable has values, I can conclude whether group exists or not. However, our framework has a Try/Catch implementation. So, in case if the group does not exist, the entire script will STOP executing.

How can I make sure that the script will continue execution even if the API call fails. (As I know that the API call will fail if the group does not exist). I have given $errorAction as 'SilentlyContinue' but still it STOPS the script execution.

function Test-CoeAzureInstancePowerBI{

param
(
    [Parameter (Mandatory = $true, ValueFromPipeline = $true)]
    $ServiceConfiguration
)

## TODO: Check if the Reader and Creator Group exists in dwgm 
# Checking if ReaderGroup exists
$Group = @{
GroupName = "ITGA_" + $ServiceConfiguration.ReaderGroup
}
$json = $Group | ConvertTo-Json 
$reader_response_success = Invoke-RestMethod 'https://api.corpinter.net/dwgm/v1/lookup/' -Headers $ServiceConfiguration.Headers -Method POST -Body $json -ContentType 'application/json' -ErrorVariable $reader_response_failure -ErrorAction SilentlyContinue
# In the above api call, 
# $reader_response_success contains data if the group exists
# $reader_response_failure contains data if the group does not exist
if($reader_response_success -eq $null) {$ServiceConfiguration.ReaderGroupExists = $False}

# Checking if ReaderGroup exists
$Group = @{
GroupName = "ITGA_" + $ServiceConfiguration.CreatorGroup
}
$json = $Group | ConvertTo-Json 
$creator_response_success = Invoke-RestMethod 'https://api.corpinter.net/dwgm/v1/lookup/' -Headers $ServiceConfiguration.Headers -Method POST -Body $json -ContentType 'application/json' -ErrorVariable $creator_response_failure -ErrorAction SilentlyContinue
# In the above api call, 
# $creator_response_success contains data if the group exists
# $creator_response_failure contains data if the group does not exist
if($creator_response_success -eq $null) {$ServiceConfiguration.CreatorGroupExists = $False}


## The instance should be SKIPPED only if both the ReaderGroup and CreatorGroup exist 
if($ServiceConfiguration.CreatorGroupExists -eq $true -and $ServiceConfiguration.ReaderGroupExists -eq $true){
    return $true
}else{
 return $False
}

}

1 Answer 1

0

-ErrorAction SilentlyContinue will continue on non-terminating errors only. So you probably get a terminating error. You can just do your own error handling for that particular cmdlet:

try {
    $reader_response_success = Invoke-RestMethod 'https://api.corpinter.net/dwgm/v1/lookup/' -Headers $ServiceConfiguration.Headers -Method POST -Body $json -ContentType 'application/json'
} catch {
    # Do some error handling here (probably nothing according to your code so far)
}

Do the same for the second Invoke-RestMethod call.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. If I get an error, i do not want the control to go inside the catch. I want the control to go to the next line after 'Invoke-RestMethod'. Is this possible for 'terminating' error?
@ManjunathRao Yes, just use the code from this answer. Put your code, that follows Invoke-RestMethod, after the catch block. Leave the catch block empty as the comment already suggests. My answer is designed to replace each line of your Invoke-RestMethod calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.