Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke-MgGraphRequest can't handle URI with # #1947

Closed
julmsy opened this issue Apr 17, 2023 · 5 comments · Fixed by #2455 or #2690
Closed

Invoke-MgGraphRequest can't handle URI with # #1947

julmsy opened this issue Apr 17, 2023 · 5 comments · Fixed by #2455 or #2690
Assignees
Labels

Comments

@julmsy
Copy link

julmsy commented Apr 17, 2023

Hello,

I would like to use the Invoke-MgGraphRequest cmdlet to request Guest users. Guest users' UPN contains '#' characters.
Example: first.last_foo.com#EXT#@contoso.onmicrosoft.com

$upn = first.last_foo.com#EXT#@contoso.onmicrosoft.com
Invoke-MgGraphRequest -Method GET -Uri https://graph.microsoft.com/beta/users/$upn
Invoke-MgGraphRequest : GET https://graph.microsoft.com/beta/users/first.last_foo.com
HTTP/1.1 404 Not Found

If I request the same URI on MS Graph Explorer, it works well.
It seems that Invoke-MgGraphRequest cut the URI at the first '#' character.

I've tried different variations, but issue persists:

Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/beta/users/$upn"
$uri = 'https://graph.microsoft.com/beta/users/first.last_foo.com#EXT#@contoso.onmicrosoft.com'
Invoke-MgGraphRequest -Method GET -Uri $uri

Could you please have a check and fix it?

Module version tested: 1.20.0 and 1.25.0.

Best,
julmsy

@ghost ghost added the ToTriage label Apr 17, 2023
@RayGHeld
Copy link

Hi @julmsy , have you tried to URL encode the # sign? The Graph explorer tool will automatically URL encode that.

@SeniorConsulting
Copy link

SeniorConsulting commented Apr 17, 2023

Yeah, good call Ray.

Replace # with %23

$uri = 'https://graph.microsoft.com/beta/users/first.last_foo.com%23EXT%23@contoso.onmicrosoft.com'
Invoke-MgGraphRequest -Method GET -Uri $uri

Just out of curiosity though, is there a reason you can't use Get-MgUser? This works out of the box and will retrieve the same sort of info
e.g.

$upn = "first.last_foo.com#EXT#@contoso.onmicrosoft.com"
Get-MgUser -UserId $upn

It's no big deal if you have a good reason to use Invoke-MgGraphRequest, I'm just wondering if you had a reason.

@salbeck-sit
Copy link

salbeck-sit commented Apr 18, 2023 via email

@julmsy
Copy link
Author

julmsy commented Apr 18, 2023

Hello everyone,

Thanks for the suggestion @RayGHeld, it works like a charm. I hadn't thought of encoding in HTTP format.

$upn = $_.Value -replace '#','%23'
$response = Invoke-MgGraphRequest -Method GET -Uri https://graph.microsoft.com/beta/users/$upn

@SeniorConsulting, well I can use Get-MgUSer right, but using Invoke-MgGraphRequest is better to handle exceptions. I'm able to get the HTTP code, and depending of the answer (404, throttling, etc.) I can play different scenarios. Here is part of my code:

Try
{
    $response = Invoke-MgGraphRequest -Method GET -Uri https://graph.microsoft.com/beta/users/$upn
}
Catch [Microsoft.Graph.PowerShell.Authentication.Helpers.HttpResponseException]
{
    $statusCode = $Error[0].Exception.Response.StatusCode.value__

    If ($statusCode -eq 404)
    {
        [...]
    }
}

@peombwa
Copy link
Member

peombwa commented Dec 13, 2023

Reopening the issue due to a bug introduced in #2488.
cc\ @timayabi2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment