msgraph-sdk-powershell icon indicating copy to clipboard operation
msgraph-sdk-powershell copied to clipboard

Cannot use KQL in Get-MgSite (PowerShell 7 only)

Open joshtransient opened this issue 6 years ago • 3 comments

The following works on Windows PowerShell 5.1, but not PowerShell 7:

Get-MgSite -Search 'GroupId:76493310-6708-48a9-a4dc-d36e7a239ff1'

Error message returned in 7 is "Syntax error: character ':' is not valid at position 7 in 'GroupId:76493310-6708-48a9-a4dc-d36e7a239ff1'". Fiddler shows that PowerShell 7 URL-encodes the value in -Search:

/v1.0/sites?$search=GroupId%3A76493310-6708-48a9-a4dc-d36e7a239ff1

For whatever reason, the current 5.1 issue where an extra "?" appears with every query parameter call seems to do fine, as Fiddler captures this:

/v1.0/sites??$search=GroupId%3A76493310-6708-48a9-a4dc-d36e7a239ff1

Invoke-MgGraphRequest also forcefully adds a "$" to the search parameter with this example:

Invoke-MgGraphRequest -Uri '/v1.0/sites?search=GroupId:76493310-6708-48a9-a4dc-d36e7a239ff1'

...which causes it to fail with an HTTP 400 (again, only on 7) AB#7399

joshtransient avatar Oct 06 '20 20:10 joshtransient

I was experiencing a very similar issue with the Search switch in PowerShell 7.0.2.

$searchString = "Weekly digest: Office 365 changes"
$messages = Get-MgUserMessage -UserId $user -Search $searchString

Error: $messages = Get-MgUserMessage -UserId $user -Search $searchString #`" … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Syntax error: character ':' is not valid at position 13 in 'Weekly digest: Office 365 changes'.

In order to fix it i had to use double quotes and escape them as follows:

$searchString = "Weekly digest: Office 365 changes"
$messages = Get-MgUserMessage -UserId $user -Search `"$searchString`"

ATJSM avatar Oct 08 '20 21:10 ATJSM

This is indeed a client bug and is being tracked by https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/399. A workaround for now is to double quote your -Search value as mentioned by @ATJSM .

peombwa avatar Oct 21 '20 22:10 peombwa

This won't fly for the /sites endpoint with ?search. ?search always works with KQL and ?$search does not. This issue was opened for a SharePoint-specific reason.

joshtransient avatar Nov 23 '20 16:11 joshtransient