r/PowerShell • • 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

10 comments sorted by

View all comments

Show parent comment

1

u/Dragennd1 Feb 13 '26 edited Feb 13 '26

Don't use the debug preference. If you dont get a list of devices look into why. Maybe your data is incorrectly formatted. Maybe the policy is missing and you shouldnt get data back.

If you do get a list of devices, something else may be clearing out your data. There are multiple points where you clear out the array contents. One of those may be deleting your data.

As I stated previously, remove the debug preference and step through your code line by line. See when you get data in your array and when it disappears.