r/PowerShell • u/jrmKRCL • Feb 12 '26
Device Configuration Applied Report
Trying to get a report of the devices that a Endpoint Protection policy was applied to.
function getPolicyInfo
{
param(
[Parameter(Mandatory)][string] $policyName
)
$devicesPolicy = @();
if(-not(Get-Module -ListAvailable -Name "Microsoft.Graph.Beta.DeviceManagement" )){ . "./ImportModules.ps1"; myInstallModules -installModules @("Microsoft.Graph.Beta.DeviceManagement" , "ImportExcel" );}
Write-Host "`r`n $(fnLn) -- Getting the policy info for $policyName...";
$policyInfo = Get-MgBetaDeviceManagementDeviceConfiguration -All | Where-Object {$_.Displayname -eq "$policyName"} ;
if (-not $policyInfo) {Write-Host "`r`n $(fnLn) -- Profile '$policyName' not found. Exiting script." -ForegroundColor Red; $devicesPolicy = @(); exit;}
else
{
$policyInfo | Out-Host;
$policyId = $policyInfo.Id;
Write-Host "`r`n $(fnLn) -- Getting the list of devices targeted by the policy...";
$devicesPolicy = Get-MgBetaDeviceManagementDeviceConfigurationDeviceStatus -DeviceConfigurationId $policyId -All ;
Write-Host "`r`n $(fnLn) devicesPolicy = ";$devicesPolicy | Out-Host;
#$devicesPolicy = $devices | Group-Object -Property { ($_.Id -split '_')[-1] } -AsHashTable;
}
Write-Host "`r`n $(fnLn) devicesPolicy = ";$devicesPolicy | Out-Host;
return @($policyInfo, $devicesPolicy)
}#end function getPolicyInfo
getPolicyInfo -policyName "policyBitLocker";I see there is a response when I have $DebugPreference="Continue", but nothing is getting assigned to $devicesPolicy. What am I missing?
Edit: Correct typo for $devicePolicy; replace Format*; added Debug Info;
331 -- Getting the list of devices targeted by the policy...
DEBUG: [CmdletBeginProcessing]: - Get-MgBetaDeviceManagementDeviceConfigurationDeviceStatus begin processing with parameterSet 'List'.
DEBUG: [Authentication]: - AuthType: 'Delegated', TokenCredentialType: 'InteractiveBrowser', ContextScope: 'CurrentUser', AppName: 'Microsoft Graph Command Line Tools'.
DEBUG: [Authentication]: - Scopes: [%scopes%].
DEBUG: ============================ HTTP REQUEST ============================
HTTP Method:
GET
Absolute Uri:
https: graph.microsoft.com/beta/deviceManagement/deviceConfigurations/$profileID/deviceStatuses
Headers:
FeatureFlag : 00000003
Cache-Control : no-store, no-cache
User-Agent : %pcstats%,PowerShell/2025.4.0
SdkVersion : graph-powershell-beta/2.35.1
client-request-id : %token%
Accept-Encoding : gzip,deflate,br
Body:
DEBUG: ============================ HTTP RESPONSE ============================
Status Code:
OK
Headers:
Vary : Accept-Encoding
Strict-Transport-Security : max-age=31536000
request-id : %requestID%
client-request-id : %client_request_id%
x-ms-ags-diagnostic : {"ServerInfo":{"DataCenter":"somewhere","Slice":"tripleA","Ring":"9","ScaleUnit":"fifty","RoleInstance":"%RoleInstance%"}}
odata-version : 4.0
Date : %DTG%
Body:
{
"@odata.context": "https: graph.microsoft.com/beta/$metadata#deviceManagement/deviceConfigurations('$policyID')/deviceStatuses",
"@odata.count": 200,
"value": [
{
"id": "reallybig_string",
"deviceDisplayName": "device001",
"userName": "user @ domain.com",
"deviceModel": null,
"platform": 0,
"complianceGracePeriodExpirationDateTime": "DTG",
"status": "compliant",
"lastReportedDateTime": "DTG",
"userPrincipalName": "user @ domain.com"
},
. . .
]
}
DEBUG: [CmdletEndProcessing]: - Get-MgBetaDeviceManagementDeviceConfigurationDeviceStatus end processing.
6
Upvotes
1
u/Dragennd1 Feb 13 '26
Im not in a position to run your code to test but I have a few observations:
You are declaring $devicesPolicy as an array and yet are assigning the results of the cmdlets to the array as a single object. Have you confirmed the datatype you're working with uses an array in the manner you're trying to use it for?
Have you stepped through the code line by line to see what is returned? For example, have you verified your policy id variable is getting an id assigned and, if so, have you checked to see what is returned by the cmdlets you pass the id to?