Skip to content

Search-MgDrive: JsonObject to JsonArray InvalidCastException #853

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

Closed
Viajaz opened this issue Sep 10, 2021 · 3 comments
Closed

Search-MgDrive: JsonObject to JsonArray InvalidCastException #853

Viajaz opened this issue Sep 10, 2021 · 3 comments

Comments

@Viajaz
Copy link

Viajaz commented Sep 10, 2021

When handling the HTTP response (200 OK) an exception is thrown when using the Search-MgDrive Cmdlet. No issue with Graph Explorer.

CmdletException: InvalidCastException - Unable to cast object of type 'Microsoft.Graph.PowerShell.Runtime.Json.JsonObject' to type 'Microsoft.Graph.PowerShell.Runtime.Json.JsonArray'

Example usage of Cmdlet:

Search-MgDrive -DriveId $DriveId -Q "Sample Text"

Heavily redacted debug log of Cmdlet use.:

DEBUG: CmdletBeginProcessing: 
DEBUG: CmdletProcessRecordStart:
DEBUG: CmdletProcessRecordAsyncStart:
DEBUG: CmdletGetPipeline:
DEBUG: CmdletBeforeAPICall:
DEBUG: URLCreated: https://graph.microsoft.com/v1.0/drives/REDACTEDDRIVEID/microsoft.graph.search(q='Sample Text')
DEBUG: RequestCreated: https://graph.microsoft.com/v1.0/drives/REDACTEDDRIVEID/microsoft.graph.search(q='Sample Text')
DEBUG: HeaderParametersAdded: https://graph.microsoft.com/v1.0/drives/REDACTEDDRIVEID/microsoft.graph.search(q='Sample Text')
DEBUG: GET /v1.0/drives/REDACTEDDRIVEID/microsoft.graph.search(q='Sample%20Text') HTTP/1.1
HTTP: graph.microsoft.com


DEBUG: BeforeCall: 
DEBUG: ResponseCreated: 
DEBUG: BeforeResponseDispatch: 
DEBUG: GET https://graph.microsoft.com/v1.0/drives/REDACTEDDRIVEID/microsoft.graph.search(q='Sample%20Text')
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Vary: Accept-Encoding
Strict-Transport-Security: max-age=31536000
request-id: 00000000-0000-0000-0000-000000000000
client-request-id: 00000000-0000-0000-0000-000000000000
x-ms-ags-diagnostic: REDACTED
X-SearchPlatform: Substrate,Substrate
OData-Version: 4.0
Cache-Control: no-cache
Date: Fri, 10 Sep 2021 04:06:06 GMT
Content-Encoding: gzip
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true; IEEE754Compatible=false; charset=utf-8

{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)","value":[{"@odata.type":"#microsoft.graph.driveItem","createdDateTime":"2020-01-01T00:00:00Z","id":"0000000000000000000000000000000000","lastModifiedDateTime":"2020-01-01T00:00:00Z","name":"Example.pdf","webUrl":"https://redacted-my.sharepoint.com/personal/user_example_com/Documents/Example.pdf","size":250000,"createdBy":{"user":{"email":"[email protected]","displayName":"User"}},"lastModifiedBy":{"user":{"email":"[email protected]","displayName":"User"}},"parentReference":{"driveId":"REDACTEDDRIVEID","driveType":"business","id":"0000000000000000000000000000000000"},"file":{"mimeType":"application/pdf"},"fileSystemInfo":{"createdDateTime":"2020-01-01T00:00:00Z","lastModifiedDateTime":"2020-01-01T00:00:00Z"},"searchResult":{},"shared":{"scope":"users"}}]}
DEBUG: Finally:
DEBUG: CmdletProcessRecordAsyncEnd: 
DEBUG: CmdletException: InvalidCastException - Unable to cast object of type 'Microsoft.Graph.PowerShell.Runtime.Json.JsonObject' to type 'Microsoft.Graph.PowerShell.Runtime.Json.JsonArray'. :    at Microsoft.Graph.PowerShell.Files.<>c.<DrivesSearch1_Call>b__789_0(Task`1 body)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.PowerShell.Cmdlets.SearchMgDrive_Search1.<onOk>d__57.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.PowerShell.Files.<DrivesSearch1_Call>d__789.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Graph.PowerShell.Files.<DrivesSearch1_Call>d__789.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.PowerShell.Files.<DrivesSearch1>d__787.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.PowerShell.Cmdlets.SearchMgDrive_Search1.<ProcessRecordAsync>d__53.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Graph.PowerShell.Cmdlets.SearchMgDrive_Search1.<ProcessRecordAsync>d__53.MoveNext()
Search-MgDrive : Unable to cast object of type 'Microsoft.Graph.PowerShell.Runtime.Json.JsonObject' to type 'Microsoft.Graph.PowerShell.Runtime.Json.JsonArray'.
At line:1 char:1
+ Search-MgDrive -DriveId $DriveId -Q "Sample Text"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Search-MgDrive_Search1], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Cmdlets.SearchMgDrive_Search1

DEBUG: CmdletProcessRecordEnd: 
DEBUG: CmdletEndProcessing:
@ghost ghost added the ToTriage label Sep 10, 2021
@Viajaz
Copy link
Author

Viajaz commented Sep 10, 2021

Workaround example

$Response = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/drives/$($DriveId)/root/search(q='Sample Text')"
$Response.value | ForEach-Object { Write-Host "$($_.name) [$($_.id)] by $($_.createdBy.user.email) at $($_.webUrl)" }

Seems like Microsoft Graph has it's own undocumented endpoint of .../microsoft.graph.search instead of .../root/search.

@peombwa
Copy link
Member

peombwa commented Sep 20, 2021

Thanks for opening the issue. This is an issue with the OpenAPI document that the SDK is generated from and is being tracked here. Our recommendation is for customers to use Invoke-MgGraphRequest as shown above.

@peombwa
Copy link
Member

peombwa commented Sep 20, 2021

Closing as duplicate of #610

@peombwa peombwa closed this as completed Sep 20, 2021
@ghost ghost removed the ToTriage label Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants