For this problem, I don't know how to run Get-IntuneManagedDevice
with token in azure powershell function. But I can provide a workaround below for your reference(use rest api to get the same result in azure powershell function which you expected).
1. Open fildder and run the command Get-IntuneManagedDevice
in powershell, we can see the command request the microsoft graph api in the backend. The api is https://graph.microsoft.com/v1.0/deviceManagement/managedDevices
and this page is its document (you do not need to do this step).
2. We need to add the permission for your app registered in azure ad.
After add the permission, don't forget grant admin consent for it.
3. We can find the graph api just support Delegated permission type but not support Application permission type according to the document.
So we can't use client credential as grant type to get the access token(we can't just use secret key to get access token) as you mentioned in the comments above. We need to request the access token by password grant type, so use the command below to get access token:
$AppId = 'xxx'
$AppSecret = 'xxx'
$Scope = "https://graph.microsoft.com/.default"
$TenantName = "xxx"
$username = "xxx"
$password = "xxx"
$Url = "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token"
Add-Type -AssemblyName System.Web
$Body = @{
client_id = $AppId
client_secret = $AppSecret
scope = $Scope
username = $username
password = $password
grant_type = 'password'
}
$PostSplat = @{
ContentType = 'application/x-www-form-urlencoded'
Method = 'POST'
Body = $Body
Uri = $Url
}
$Request = Invoke-RestMethod @PostSplat
$Request.access_token
4. In step 1 we know the command Get-IntuneManagedDevice
request the graph api in the backend, so we just need to request the graph api and then we can get the result.
$Uri = "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices"
$Header = @{
Authorization = "$($Request.token_type) $($Request.access_token)"
}
$result = Invoke-RestMethod -Uri $Uri -Headers $Header -Method Get -ContentType "application/json"
$result.value
Hope it helps~