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
}
}