diff --git a/src/Authentication/Authentication/custom/common/GraphUri.ps1 b/src/Authentication/Authentication/custom/common/GraphUri.ps1 index 04395b53798..f309603546d 100644 --- a/src/Authentication/Authentication/custom/common/GraphUri.ps1 +++ b/src/Authentication/Authentication/custom/common/GraphUri.ps1 @@ -48,11 +48,29 @@ function GraphUri_TokenizeIds { ) $TokenizedUri = $Uri.GetComponents([System.UriComponents]::SchemeAndServer, [System.UriFormat]::SafeUnescaped) + $LastSegmentIndex = $Uri.Segments.length - 1 + $LastSegment = $Uri.Segments[$LastSegmentIndex] + $UnescapedUri = $Uri.ToString() for ($i = 0 ; $i -lt $Uri.Segments.length; $i++) { # Segment contains an integer/id and is not API version. if ($Uri.Segments[$i] -match "[^v1.0|beta]\d") { - # Substitute integers/ids with {id} tokens, e.g, /users/289ee2a5-9450-4837-aa87-6bd8d8e72891 -> users/{id}. - $TokenizedUri += "{id}/" + #For Uris whose last segments match the regex '(.*?)', all characters from the first '(' are substituted with '.*' + if ($i -eq $LastSegmentIndex) { + if ($UnescapedUri -match '(.*?)') { + try { + $UpdatedLastSegment = $LastSegment.Substring(0, $LastSegment.IndexOf("(")) + $TokenizedUri += $UpdatedLastSegment + ".*" + + } + catch { + $TokenizedUri += "{id}/" + } + } + } + else { + # Substitute integers/ids with {id} tokens, e.g, /users/289ee2a5-9450-4837-aa87-6bd8d8e72891 -> users/{id}. + $TokenizedUri += "{id}/" + } } else { $TokenizedUri += $Uri.Segments[$i] diff --git a/src/Authentication/Authentication/test/Find-MgGraphCommand.Tests.ps1 b/src/Authentication/Authentication/test/Find-MgGraphCommand.Tests.ps1 index 09de2aa61da..300fc850c7a 100644 --- a/src/Authentication/Authentication/test/Find-MgGraphCommand.Tests.ps1 +++ b/src/Authentication/Authentication/test/Find-MgGraphCommand.Tests.ps1 @@ -46,6 +46,7 @@ Describe "Find-MgGraphCommand Command" { $MgCommand.Command | Should -Be "Get-MgEntitlementManagementAccessPackageAssignmentResourceRole" } | Should -Not -Throw } + It 'Should find command using URI with query parameters' { { $Uri = "beta/users?`$select=displayName&`$filter=identities/any(c:c/issuerAssignedId eq 'j.smith@yahoo.com')" @@ -159,6 +160,20 @@ Describe "Find-MgGraphCommand Command" { It 'Should throw error when command name is invalid' { $ExpectedErrorMessage = "*'New-MgInvalid' is not a valid Microsoft Graph PowerShell command.*" { Find-MgGraphCommand -Command "New-MgInvalid" -ErrorAction Stop | Out-Null } | Should -Throw -ExpectedMessage $ExpectedErrorMessage } + + It 'Should find command using actual id in key segments inside parenthesis' { + { + $ExpectedResourceUri = @("/reports/getSharePointActivityUserCounts(period='{period}')", "/reports/getSharePointActivityUserCounts(period='{period}')") + $Uri = "/reports/getSharePointActivityUserCounts(period='D3')" + $MgCommand = Find-MgGraphCommand -Uri $Uri + $MgCommand | Should -HaveCount 2 + $MgCommand.Method | Should -Be @("GET", "GET") + $MgCommand.APIVersion | Should -BeIn @("v1.0", "beta") + $MgCommand.Variants | Should -Contain "Get" + $MgCommand.URI | Should -Be $ExpectedResourceUri + $MgCommand.Command | Should -Be @("Get-MgReportSharePointActivityUserCount", "Get-MgReportSharePointActivityUserCount") + } | Should -Not -Throw + } } } } \ No newline at end of file