Reset Inventory on SCCM client if over 7 days since report

Here is a little script that is a combination of several different pieces I found around the web. Basically it will check a machine and get the last HW & SW inventory dates. Then if those dates are over 7 days it will reset the inventory and trigger a new inventory to occur. Like I said nothing fancy, this script does require admin privs to run. However one could modify it to use the control panel com objects to call the built in inventory cycles like you would through the Configuration Manager control panel dialog.



$inventoryQuery = "Select InventoryActionID, LastCycleStartedDate, LastReportDate from InventoryActionStatus"

$inventoryResult = get-wmiobject -query $inventoryQuery -namespace "root\ccm\Invagt"

$HWdate = $null
$SWdate = $null
$HWold = $true
$SWold = $true

ForEach ($iResult in $inventoryResult)
{

    $inventoryType = [convert]::ToInt32(($iResult.InventoryActionID).substring(35,2), 10)

    switch ($inventoryType)
    {

        1 {$HWdate = $iResult.LastReportDate}
        2 {$SWdate = $iResult.LastReportDate}

    }

}

$HWdate = [System.Management.ManagementDateTimeConverter]::ToDateTime($HWdate)
$SWdate = [System.Management.ManagementDateTimeConverter]::ToDateTime($SWdate)

$limit = (Get-Date).AddDays(-7)


if ($HWdate -gt $limit)
{
    $HWold = $false
}
if ($SWdate -gt $limit)
{
    $SWold = $false
}

if (($HWold -eq $true) -or ($SWold -eq $true))
{
    #HW Inventory reset and trigger
    [wmi]"ROOT\ccm\invagt:InventoryActionStatus.InventoryActionID='{00000000-0000-0000-0000-000000000001}'" | remove-wmiobject
    ([wmiclass]'ROOT\ccm:SMS_Client').TriggerSchedule('{00000000-0000-0000-0000-000000000001}')
    #SW Inventory reset and trigger
    [wmi]"ROOT\ccm\invagt:InventoryActionStatus.InventoryActionID='{00000000-0000-0000-0000-000000000002}'" | remove-wmiobject
    ([wmiclass]'ROOT\ccm:SMS_Client').TriggerSchedule('{00000000-0000-0000-0000-000000000002}')
}

Comments

Popular posts from this blog

Intune Hybrid - NDES Cert Issue

Stuck @ "Waiting for user logon"

Triggering a software update install via Powershell