How do I list all ESP using graph API with an appID?

Johan Berg 65 Reputation points
2023-05-30T20:02:35.1966667+00:00

Hi,

I want to list all ESP using graph API with an appID. If I run the following code I get an error.

$ReqTokenBody = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    client_Id     = $clientID
    Client_Secret = $clientSecret
} 

$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody

$URL = "https://graph.microsoft.com/beta/deviceManagement/deviceEnrollmentConfigurations"
Invoke-RestMethod -Headers @{Authorization = "Bearer $($TokenResponse.Access_Token)"} -Uri $URL -Method GET

The error is:

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:3 char:2
+  Invoke-RestMethod -Headers @{Authorization = "Bearer $($TokenRespons ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod 
   ], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCom 
   mand

If I use graph-explorer it works fine.

User's image

How do list all ESP using graph API with an appID?

Br

Johan

Microsoft Security | Intune | Configuration
Microsoft Security | Intune | Enrollment
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Crystal-MSFT 53,981 Reputation points Microsoft External Staff
    2023-05-31T05:37:51.1333333+00:00

    @Johan Berg Thanks for posting in Q&A. To use PowerShell script to get all the ESP, I have tested the script as below. Insert the Global Administrator account. And then it will list all the ESPs' id and displayname.

    User's image

    You can rename the ESP.txtto ESP.ps1 and run the script.

    Hope the above information can help.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.