ugurkocde / IntuneAssignmentChecker

https://intuneassignmentchecker.ugurkoc.de/
MIT License
107 stars 13 forks source link

Improvements to error handling for 'Users' assignments #22

Closed BatisteDONDOGLIO closed 3 weeks ago

BatisteDONDOGLIO commented 2 months ago

Hi @ugurkocde !

I hope you are well :)

I noticed a small issue happening when trying to check Users assignments (option 1), in case the user doesn't exists (not found).

PowerShell throws an error because it is not handled by the script itself. The script also attempts to continue checking for groups that user belongs to, despite the user not existing. Which obviously also fails.

This occurs here: https://github.com/ugurkocde/IntuneAssignmentChecker/blob/3ca0448875c6ded317d8accfc09352c42476fabc/IntuneAssignmentChecker_v2.ps1#L265

I've added a simple fix locally on the script by adding something like this:

try {
    $userResponse = Invoke-MgGraphRequest -Uri $userUri -Method Get
    $userId = $userResponse.id
} catch { 
    if ($_.Exception.Response.StatusCode.value__ -eq 404) {
        Write-Host "No user found for UPN: $upn" -ForegroundColor Red
    } else {
        Write-Host "An error occured while checking UPN: $upn" -ForegroundColor Red
        Write-Host "Error: $($_.Exception.Response)" -ForegroundColor Red
    }
        continue
}

This catches the 404 Not Found error and displays a simple error message accordingly, and catches all over responses and displays only their content.

I don't know if this is desired, and I'm sure it can be improved (for example, by adding more if statements to handle more errors, in case the Invoke-MgGraphRequest fails entirely?). But as a quick fix, it gets the job done.

It also calls continue meaning it won't try to check the group assigned to the user that wasn't found and skips to the next user in the loop (if applicable).

I'm sorry again for not submitting a pull request for this!

Greetings from France :)

ugurkocde commented 2 months ago

Hi @BatisteDONDOGLIO,

thanks for pointing that out. I will fix thex script next week after the Workplace Ninja Summit.

Best Ugur

ugurkocde commented 3 weeks ago

Hi @BatisteDONDOGLIO,

Thanks again for the issue report! I’ve implemented your recommendations in version 2.4, along with some new features.

Best, Ugur