Azure Role-based access control
An Azure service that provides fine-grained access management for Azure resources, enabling you to grant users only the rights they need to perform their jobs.
814 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How to capture Error like Get-AzRoleAssignment : Operation returned an invalid status code 'NotFound' and do some activity in if-else block based on this error
Use the following:
try {
# Attempt to get a role assignment
$roleAssignment = Get-AzRoleAssignment -ObjectId "your-object-id" -RoleDefinitionName "Contributor"
# If the command succeeds, perform some action
Write-Output "Role assignment found."
# Do some activity if role assignment is found
# For example: Write-Output "Role assignment details: $roleAssignment"
} catch {
# Check if the error message contains 'NotFound'
if ($_.Exception.Message -match "NotFound") {
Write-Output "Role assignment not found. Performing alternate activity."
# Perform alternate activity here
# For example: Write-Output "Creating new role assignment..."
} else {
# Handle other types of errors
Write-Output "An unexpected error occurred: $($_.Exception.Message)"
}
}
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin