---
title: "Discussion on \"Get Intune Compliance data using PowerShell leveraging Graph API\" | Hashnode"
description: "Discussion on \"Get Intune Compliance data using PowerShell leveraging Graph API\". With device details & error messages"
url: "https://hashnode.com/posts/get-intune-compliance-data-using-powershell-leveraging-graph-api/60f6969fd6a73079b398bae3"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Get Intune Compliance data using PowerShell leveraging Graph API](https://doitpshway.com/get-intune-compliance-data-using-powershell-leveraging-graph-api?utm_source=hashnode&utm_medium=feed)

With device details & error messages

Jul 20, 2021 · 7 min read · doitpshway.com  
Updated Mar 29, 2022  
Read the article

-   [#powershell](https://hashnode.com/tag/powershell)
-   [#automation](https://hashnode.com/tag/automation)
-   [#rest-api](https://hashnode.com/tag/rest-api)
-   [#windows](https://hashnode.com/tag/windows)

## Written by

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf)

@AndrewZtrhgf · Czech Republic

  
[View profile](https://hashnode.com/@AndrewZtrhgf)

System Administrator that loves PowerShell

I work as System Administrator for more than 15 years now and I love to make my life easier by automating work &amp; personal stuff via PowerShell (even silly things like food recipes list generation).

## Responses 2

Markdown supported · ⌘↵ to post

[Pavel Duda](https://hashnode.com/@GhuulCZ)

[May 17, 2024](https://hashnode.com/posts/get-intune-compliance-data-using-powershell-leveraging-graph-api/60f6969fd6a73079b398bae3/comment/664726038a87ec10e6789307)

Hi [Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf) I was trying to do similar thing but for device configurations so I know what devices have which configurations set properly and which failed. I was able to list some statuses "an block" but so far I did not manage to query status of configuration for particular device - well at least not through PS (I did end up digging into Intune using developer tools and checking how it displays the device state via standard GET/POST). Have you ever tried to dig this information using MgGraph modules?

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf)

[May 17, 2024](https://hashnode.com/posts/get-intune-compliance-data-using-powershell-leveraging-graph-api/60f6969fd6a73079b398bae3/comment/664726038a87ec10e6789307#reply-664729b7e8c01717000c79c4)

1.  I don't have such function, but to get all intune policies for a given account check [doitpshway.com/get-all-intune-policies-assigned-t…](https://doitpshway.com/get-all-intune-policies-assigned-to-the-specified-account-using-powershell) (uses [doitpshway.com/get-all-intune-policies-using-powe…](https://doitpshway.com/get-all-intune-policies-using-powershell-and-graph-api) in the background)
2.  if you know the correct API URI, you can easily translate it back to the corresponding cmdlet by Find-MgGraphCommand -Uri "[graph.microsoft.com/v1.0/deviceManagement](https://graph.microsoft.com/v1.0/deviceManagement)" (just replace the URI part)
3.  if you want to check policy status directly on the client itself, check [doitpshway.com/get-a-better-intune-policy-report-…](https://doitpshway.com/get-a-better-intune-policy-report-part-3-final)
4.  this is how I am getting other types of reports, so it might help [doitpshway.com/get-intune-reports-using-powershel…](https://doitpshway.com/get-intune-reports-using-powershell-leveraging-graph-api)

[Pavel Duda](https://hashnode.com/@GhuulCZ)

[May 19, 2024](https://hashnode.com/posts/get-intune-compliance-data-using-powershell-leveraging-graph-api/60f6969fd6a73079b398bae3/comment/664726038a87ec10e6789307#reply-6649c1375c57baae47364594)

Thanks [Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf) will check that. Coming from the other MDMs I share your frustration of terrible reporting capabilities of Intune :)

[Stevie Lamb](https://hashnode.com/@StevieLamb)

[Jun 22, 2022](https://hashnode.com/posts/get-intune-compliance-data-using-powershell-leveraging-graph-api/60f6969fd6a73079b398bae3/comment/62b39fb186d22092a7547476)

Very good: clever innovative idea, well laid-out and associated code quality appears very good.

[Stevie Lamb](https://hashnode.com/@StevieLamb)

[Jun 20, 2022](https://hashnode.com/posts/get-intune-compliance-data-using-powershell-leveraging-graph-api/60f6969fd6a73079b398bae3/comment/62b072f3c0f84f6b540d1028)

Hi Ondrej,

Thanks for you published work, I've found it very useful so far.

I've chosen to use MSAL.PS to acquire a header, alongside a self-signed certificate, rather than the hard-coded secret.

Regardless of that: many people have environments large enough that their token will expire. Can you suggest a simple method of handling token refresh when calling your functions to avoid such scenarios?

Many thanks,

Stevie

For those interested in using MSAL.PS and certs instead of New-IntuneAuthHeader:

-   generate a self-signed certificate key-pair on your computer
    
-   add its public key to the App Reg you created as documented by Ondrej
    
-   install the MSAL.PS module
    
-   use Get-MsalToken to acquire a token
    

```
# store your thumbprint
$thumbprint = <your certificate's thumbprint> # this is visible in your registered app's Credentials and Secrets section once it has been added

# store the path to your private key based on the thumbprint
$path = "Cert:\CurrentUser\My\" + $thumbprint

# set a hashtable for the connection parameters
$connectionDetails = @{
            'TenantId'          = <your tenant Id>
            'ClientId'          = <your app's client ID>
            'ClientCertificate' = Get-Item -Path $path
        }

$token = Get-MsalToken @connectionDetails

# create an auth header from that token
 $header = @{
            'Authorization' = $token.CreateAuthorizationHeader()
        }
```

you can now use the above `$header` when calling either of Ondrej's awesome functions.

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf)

[Jun 23, 2022](https://hashnode.com/posts/get-intune-compliance-data-using-powershell-leveraging-graph-api/60f6969fd6a73079b398bae3/comment/62b072f3c0f84f6b540d1028#reply-62b3faf2b86f939ac81aec15)

Thank you 🙂

According the expired header. I saw solution on the internet where custom new attribute is put into $header variable with time of the token generation. Not sure what the token lifetime is but before you use it, you can then check whether it is valid and if not generate the new one...
