Invoke-MgGraphRequest can't handle URI with #
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
Hi @julmsy , have you tried to URL encode the # sign? The Graph explorer tool will automatically URL encode that.
Yeah, good call Ray.
Replace # with %23
$uri = 'https://graph.microsoft.com/beta/users/first.last_foo.com%23EXT%[email protected]'
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.
Perhaps an idea to use [System.Web.HttpUtility]::UrlEncode(“the raw URL you want to pass to Invoke-MgGraphRequest”) instead of the raw URL itself - that way any characters with special meaning will be translated
Fra: SeniorConsulting @.> Sendt: 17. april 2023 23:13 Til: microsoftgraph/msgraph-sdk-powershell @.> Cc: Subscribed @.***> Emne: Re: [microsoftgraph/msgraph-sdk-powershell] Invoke-MgGraphRequest can't handle URI with # (Issue #1947)
Yeah, good call Ray.
Replace # with %23
$uri = @.***'
Invoke-MgGraphRequest -Method GET -Uri $uri
— Reply to this email directly, view it on GitHubhttps://github.com/microsoftgraph/msgraph-sdk-powershell/issues/1947#issuecomment-1512093843, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AOROZPASADIADMGIEY3JBGTXBWW35ANCNFSM6AAAAAAXAZNLHY. You are receiving this because you are subscribed to this thread.Message ID: @.@.>>
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)
{
[...]
}
}
Reopening the issue due to a bug introduced in https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/2488. cc\ @timayabi2020