tests/Test-CmClientStaleInventory.ps1

function Test-CmClientStaleInventory {
    [CmdletBinding()]
    param (
        [parameter()][string] $TestName = "Stale Client Inventory Data",
        [parameter()][string] $TestGroup = "operation",
        [parameter()][string] $Description = "Clients with outdated or missing inventory data",
        [parameter()][hashtable] $ScriptParams
    )
    try {
        $startTime = (Get-Date)
        [int] $DaysOld = Get-CmHealthDefaultValue -KeySet "configmgr:MaxClientInventoryDaysOld" -DataSet $CmHealthConfig
        [System.Collections.Generic.List[PSObject]]$tempdata = @() # for detailed test output to return if needed
        $stat   = "PASS" # do not change this
        $except = "WARNING"
        $msg    = "No issues found" # do not change this either
        $query = "SELECT DISTINCT fcm.Name,
sys.Client_Version0,
fcm.Domain,
sys.User_Name0,
fcm.SiteCode,
chs.LastActiveTime AS AgentTime,
chs.LastHW AS LastHWScan,
chs.LastSW AS LastScanDate
FROM v_FullCollectionMembership fcm
INNER JOIN v_R_System sys ON fcm.ResourceID = sys.ResourceID
INNER JOIN v_CH_ClientSummary chs ON chs.ResourceID = fcm.ResourceID AND chs.ClientActiveStatus = 0
WHERE fcm.CollectionID = 'SMS00001' AND chs.LastActiveTime < DATEADD(dd,-CONVERT(INT,$($DaysOld)),GETDATE())"

        $res = Get-CmSqlQueryResult -Query $query -Params $ScriptParams
        if ($null -ne $res -and $res.Count -gt 0) {
            $stat = $except
            $msg  = "$($res.Count) clients inventory older than $($DaysOld) days: $($res.Name -join ',')"
            $res | Foreach-Object {$tempdata.Add("Name=$($_.Name),LastHW=$($_.LastHWScan),SiteCode=$($_.SiteCode)")}
        }
    }
    catch {
        $stat = 'ERROR'
        $msg = $_.Exception.Message -join ';'
    }
    finally {
        $rt = Get-RunTime -BaseTime $startTime
        Write-Output $([pscustomobject]@{
            TestName    = $TestName
            TestGroup   = $TestGroup
            TestData    = $tempdata
            Description = $Description
            Status      = $stat
            Message     = $msg
            RunTime     = $rt
            Credential  = $(if($ScriptParams.Credential){$($ScriptParams.Credential).UserName} else { $env:USERNAME })
        })
    }
}