From 5d29cc42d0206cb05e85863874d79a31230d6cb3 Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa <10947120+timayabi2020@users.noreply.github.com> Date: Wed, 14 Sep 2022 21:32:15 +0300 Subject: [PATCH 1/8] Updated GraphUri.ps script to handle find commands with parenthesis --- .../Authentication/custom/common/GraphUri.ps1 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Authentication/Authentication/custom/common/GraphUri.ps1 b/src/Authentication/Authentication/custom/common/GraphUri.ps1 index 04395b53798..83e6c664bfa 100644 --- a/src/Authentication/Authentication/custom/common/GraphUri.ps1 +++ b/src/Authentication/Authentication/custom/common/GraphUri.ps1 @@ -48,11 +48,21 @@ function GraphUri_TokenizeIds { ) $TokenizedUri = $Uri.GetComponents([System.UriComponents]::SchemeAndServer, [System.UriFormat]::SafeUnescaped) + $LastSegment = $Uri.Segments[$Uri.Segments.length - 1] + $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") { + #For Uris whose last segments match the regex '(?<={)(.*?)(?=})', all characters from the first '(' are substituted with '.*' + if($i -eq $Uri.Segments.length - 1){ + if($UnescapedUri -match '(?<={)(.*?)(?=})'){ + $UpdatedLastSegment = $LastSegment.Substring(0,$LastSegment.IndexOf("(")) + $TokenizedUri += $UpdatedLastSegment + } + }else{ # Substitute integers/ids with {id} tokens, e.g, /users/289ee2a5-9450-4837-aa87-6bd8d8e72891 -> users/{id}. $TokenizedUri += "{id}/" + } } else { $TokenizedUri += $Uri.Segments[$i] From 832cc7955eecdab5b4177457e5a51deb36bef588 Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa <10947120+timayabi2020@users.noreply.github.com> Date: Wed, 14 Sep 2022 21:40:05 +0300 Subject: [PATCH 2/8] Updated file --- src/Authentication/Authentication/custom/common/GraphUri.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Authentication/Authentication/custom/common/GraphUri.ps1 b/src/Authentication/Authentication/custom/common/GraphUri.ps1 index 83e6c664bfa..8b23c38585f 100644 --- a/src/Authentication/Authentication/custom/common/GraphUri.ps1 +++ b/src/Authentication/Authentication/custom/common/GraphUri.ps1 @@ -57,7 +57,7 @@ function GraphUri_TokenizeIds { if($i -eq $Uri.Segments.length - 1){ if($UnescapedUri -match '(?<={)(.*?)(?=})'){ $UpdatedLastSegment = $LastSegment.Substring(0,$LastSegment.IndexOf("(")) - $TokenizedUri += $UpdatedLastSegment + $TokenizedUri += $UpdatedLastSegment + ".*" } }else{ # Substitute integers/ids with {id} tokens, e.g, /users/289ee2a5-9450-4837-aa87-6bd8d8e72891 -> users/{id}. From 73f820ae442d5c01ff760682dbb2527a87979a5f Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa <10947120+timayabi2020@users.noreply.github.com> Date: Thu, 15 Sep 2022 10:19:01 +0300 Subject: [PATCH 3/8] Added a try catch --- .../Authentication/custom/common/GraphUri.ps1 | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Authentication/Authentication/custom/common/GraphUri.ps1 b/src/Authentication/Authentication/custom/common/GraphUri.ps1 index 8b23c38585f..5656cb6a1aa 100644 --- a/src/Authentication/Authentication/custom/common/GraphUri.ps1 +++ b/src/Authentication/Authentication/custom/common/GraphUri.ps1 @@ -54,14 +54,21 @@ function GraphUri_TokenizeIds { # Segment contains an integer/id and is not API version. if ($Uri.Segments[$i] -match "[^v1.0|beta]\d") { #For Uris whose last segments match the regex '(?<={)(.*?)(?=})', all characters from the first '(' are substituted with '.*' - if($i -eq $Uri.Segments.length - 1){ - if($UnescapedUri -match '(?<={)(.*?)(?=})'){ - $UpdatedLastSegment = $LastSegment.Substring(0,$LastSegment.IndexOf("(")) - $TokenizedUri += $UpdatedLastSegment + ".*" + if ($i -eq $Uri.Segments.length - 1) { + 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 { + # Substitute integers/ids with {id} tokens, e.g, /users/289ee2a5-9450-4837-aa87-6bd8d8e72891 -> users/{id}. + $TokenizedUri += "{id}/" } } else { From 11bd37ead316b713adae20ff7ac401da0d141bc4 Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa <10947120+timayabi2020@users.noreply.github.com> Date: Thu, 15 Sep 2022 13:57:49 +0300 Subject: [PATCH 4/8] Corrected one failing test --- src/Authentication/Authentication/custom/common/GraphUri.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Authentication/Authentication/custom/common/GraphUri.ps1 b/src/Authentication/Authentication/custom/common/GraphUri.ps1 index 5656cb6a1aa..1dd19cd027e 100644 --- a/src/Authentication/Authentication/custom/common/GraphUri.ps1 +++ b/src/Authentication/Authentication/custom/common/GraphUri.ps1 @@ -55,7 +55,7 @@ function GraphUri_TokenizeIds { if ($Uri.Segments[$i] -match "[^v1.0|beta]\d") { #For Uris whose last segments match the regex '(?<={)(.*?)(?=})', all characters from the first '(' are substituted with '.*' if ($i -eq $Uri.Segments.length - 1) { - if ($UnescapedUri -match '(?<={)(.*?)(?=})') { + if ($UnescapedUri -match '(.*?)') { try { $UpdatedLastSegment = $LastSegment.Substring(0, $LastSegment.IndexOf("(")) $TokenizedUri += $UpdatedLastSegment + ".*" @@ -75,6 +75,7 @@ function GraphUri_TokenizeIds { $TokenizedUri += $Uri.Segments[$i] } } + Write-host $TokenizedUri return $TokenizedUri.TrimEnd("/") } From 62606d46878215db3d96644179e430fc81fe79fa Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 17 Sep 2022 08:55:59 +0300 Subject: [PATCH 5/8] Update src/Authentication/Authentication/custom/common/GraphUri.ps1 Co-authored-by: Peter Ombwa --- src/Authentication/Authentication/custom/common/GraphUri.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Authentication/Authentication/custom/common/GraphUri.ps1 b/src/Authentication/Authentication/custom/common/GraphUri.ps1 index 1dd19cd027e..d0571ee1d7d 100644 --- a/src/Authentication/Authentication/custom/common/GraphUri.ps1 +++ b/src/Authentication/Authentication/custom/common/GraphUri.ps1 @@ -54,7 +54,7 @@ function GraphUri_TokenizeIds { # Segment contains an integer/id and is not API version. if ($Uri.Segments[$i] -match "[^v1.0|beta]\d") { #For Uris whose last segments match the regex '(?<={)(.*?)(?=})', all characters from the first '(' are substituted with '.*' - if ($i -eq $Uri.Segments.length - 1) { + if ($i -eq $LastSegmentIndex) { if ($UnescapedUri -match '(.*?)') { try { $UpdatedLastSegment = $LastSegment.Substring(0, $LastSegment.IndexOf("(")) From d00d38071b546bc9e9edcec6e43ad5aae870f8b7 Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 17 Sep 2022 08:56:14 +0300 Subject: [PATCH 6/8] Update src/Authentication/Authentication/custom/common/GraphUri.ps1 Co-authored-by: Peter Ombwa --- src/Authentication/Authentication/custom/common/GraphUri.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Authentication/Authentication/custom/common/GraphUri.ps1 b/src/Authentication/Authentication/custom/common/GraphUri.ps1 index d0571ee1d7d..38a198b8a72 100644 --- a/src/Authentication/Authentication/custom/common/GraphUri.ps1 +++ b/src/Authentication/Authentication/custom/common/GraphUri.ps1 @@ -75,7 +75,6 @@ function GraphUri_TokenizeIds { $TokenizedUri += $Uri.Segments[$i] } } - Write-host $TokenizedUri return $TokenizedUri.TrimEnd("/") } From 700492216c5c657d2483b8c1b84b69daea121745 Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 17 Sep 2022 08:56:37 +0300 Subject: [PATCH 7/8] Update src/Authentication/Authentication/custom/common/GraphUri.ps1 Co-authored-by: Peter Ombwa --- src/Authentication/Authentication/custom/common/GraphUri.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Authentication/Authentication/custom/common/GraphUri.ps1 b/src/Authentication/Authentication/custom/common/GraphUri.ps1 index 38a198b8a72..cb806c1125c 100644 --- a/src/Authentication/Authentication/custom/common/GraphUri.ps1 +++ b/src/Authentication/Authentication/custom/common/GraphUri.ps1 @@ -48,7 +48,7 @@ function GraphUri_TokenizeIds { ) $TokenizedUri = $Uri.GetComponents([System.UriComponents]::SchemeAndServer, [System.UriFormat]::SafeUnescaped) - $LastSegment = $Uri.Segments[$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. From 6818037bd5e7ba53dea8ef3e62cb27fc822b1b6d Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa <10947120+timayabi2020@users.noreply.github.com> Date: Sat, 17 Sep 2022 09:31:05 +0300 Subject: [PATCH 8/8] Added pester test and updated comment --- .../Authentication/custom/common/GraphUri.ps1 | 3 ++- .../test/Find-MgGraphCommand.Tests.ps1 | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Authentication/Authentication/custom/common/GraphUri.ps1 b/src/Authentication/Authentication/custom/common/GraphUri.ps1 index cb806c1125c..f309603546d 100644 --- a/src/Authentication/Authentication/custom/common/GraphUri.ps1 +++ b/src/Authentication/Authentication/custom/common/GraphUri.ps1 @@ -48,12 +48,13 @@ 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") { - #For Uris whose last segments match the regex '(?<={)(.*?)(?=})', all characters from the first '(' are substituted with '.*' + #For Uris whose last segments match the regex '(.*?)', all characters from the first '(' are substituted with '.*' if ($i -eq $LastSegmentIndex) { if ($UnescapedUri -match '(.*?)') { try { 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