Search code examples
powershellazure-powershell

Get-PnPAzureADUser : Code: Request_UnsupportedQuery


I have this weird behavior inside my PnP Power Shell:-

now if i write this Power Shell i will get results:-

Get-PnPAzureADUser -Filter  {AccountEnabled eq true and Mail  eq '[email protected]'}

but if i define the Mail inside a variable i will get this error:-

PS C:\windows\system32> $rrr = "[email protected]"
PS C:\windows\system32> Get-PnPAzureADUser -Filter  {AccountEnabled eq true and Mail  eq $rrr}                          
Get-PnPAzureADUser : Code: Request_UnsupportedQuery
Message: Unsupported Query.
Inner error:
        AdditionalData:
        date: 2022-12-28T22:13:33
        request-id: 3f8d45ef-ab8e-49c9-bf6e-41e4a56c11e3
        client-request-id: 3f8d45ef-ab8e-49c9-bf6e-41e4a56c11e3
ClientRequestId: 3f8d45ef-ab8e-49c9-bf6e-41e4a56c11e3
At line:1 char:1
+ Get-PnPAzureADUser -Filter  {AccountEnabled eq true and Mail  eq $rrr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-PnPAzureADUser], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,PnP.PowerShell.Commands.Principals.GetAzureADUser

Or this

PS C:\windows\system32> $rrr = "'[email protected]'"
PS C:\windows\system32> Get-PnPAzureADUser -Filter  {AccountEnabled eq true and Mail  eq $rrr}                          
Get-PnPAzureADUser : Code: Request_UnsupportedQuery
Message: Unsupported Query.
Inner error:
        AdditionalData:
        date: 2022-12-28T22:13:33
        request-id: 3f8d45ef-ab8e-49c9-bf6e-41e4a56c11e3
        client-request-id: 3f8d45ef-ab8e-49c9-bf6e-41e4a56c11e3
ClientRequestId: 3f8d45ef-ab8e-49c9-bf6e-41e4a56c11e3
At line:1 char:1
+ Get-PnPAzureADUser -Filter  {AccountEnabled eq true and Mail  eq $rrr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-PnPAzureADUser], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,PnP.PowerShell.Commands.Principals.GetAzureADUser

Any advice? how i can pass the Mail as a variable inside the Get-PnPAzureADUser??


Solution

  • Use a expandable string instead of a script block to formulate your filter:

    $rrr = "[email protected]"
    Get-PnPAzureADUser -Filter "AccountEnabled eq true and Mail eq '$rrr'"