ConfigMgr

PowerShell & Task Scheduler: Scheduling Inventory

Topics: ConfigMgr, Endpoint Insights

Back in 2014, I published a blog post called, How to Avoid Receiving Inventory Re-Sync Requests for Snapshot VMs. What I loved about that post was how task scheduler was used to schedule ConfigMgr inventory thereby avoiding ConfigMgr re-sync errors. What I disliked about that 2014 blog post was how each TriggerSchedule task needed to be created by hand. Moving forward to 2020, I turned to PowerShell in order to solve this problem and automated the process. Why? I was testing Power BI Report Server with Endpoint Insights reporting and part of this testing involved creating new Windows 10 servers along with several new VMs. I didn’t want to create each task manually every time, so PowerShell does all of the heavy lifting for me and now I’m sharing with you how I did it, along with the script, in this post.

Background

In my original post, I linked to a few blog posts on how to force inventory to a collection and computer by using Right Click Tools by Recast. This is great, even today, BUT it doesn’t help with VMs where a snapshot is taken.

Here, at Recast Software, Hyper-V is used to make snapshots of the VMs in our test lab. We do this in case we need to quickly revert a VM to a known state. When this happens the VM inventory can become out-of-sync with ConfigMgr. In theory, ConfigMgr forces out of sync computers to re-sync and update themselves, but this task can take a while. That time lag is not helpful in ensuring that your inventory is always up-to-date in ConfigMgr.

Our solution to this problem, each time a VM is restarted (rebooted), is to force the ConfigMgr client to perform the following actions in this order:

  1. Perform a heartbeat discovery (data discovery cycle).
  2. Delete the last full hardware in order to force the next hardware inventory cycle to perform a full inventory.
  3. Delete the software inventory data in order to force the next software inventory cycle to perform a full inventory.
  4. Perform a hardware inventory.
  5. Perform a software inventory.

Since I don’t want to perform this task manually each time, I scripted all of the action items listed above by using WMIC. Then I set a scheduled task to run every time a VM re-starts with a 5-minute delay. The 5-minute delay allows the ConfigMgr client to start before it’s forced to perform the inventory action.

In the original blog post, I created one scheduled task for all of the actions. When I went to automate this process with PowerShell, it made more sense to create one scheduled task for each action item listed above. In addition to those actions, I also wanted to force a full inventory at 9 a.m. every day on each VM. In order to keep things tidy, I placed all of the scheduled tasks in a folder.

Schedule ConfigMgr Inventory - Task Scheduler

Actionable dashboards? Yes, please with Right Click Tools Enterprise

LAPS, BitLocker, and software update compliance all in an easy-to-view formats with remediation just a right click away.

Schedule ConfigMgr Inventory

I am not posting the entire script here because you can download the full script from Recast Software’s site and then review it. Instead, I am showing you the three most important lines below.

$action = New-ScheduledTaskAction -Execute ‘WMIC.exe’ -Argument ‘/namespace:\rootccm path sms_client CALL TriggerSchedule “{00000000-0000-0000-0000-000000000001}” /NOINTERACTIVE’
$trigger = New-ScheduledTaskTrigger -Daily -At 9:01:01am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $FolderName”HW Inv” -User “System”

The first line creates the scheduled task. The second line creates the scheduled task trigger; in this example it occurs at 9:01:01 am. The third line joins both the first and second lines together and runs the scheduled task using the system account.

Watch for word-wrapping of each line. Also, if you cut and paste this into your own script, watch out for stylized (curly) quotes. Quotation marks should always be the straight, single ones.

Looking at the screenshot above, you can see that once the PowerShell script is run, all of the scheduled tasks are created and placed neatly within the folder.

Deep Freeze

Using this PowerShell script to schedule ConfigMgr inventory is also important if you have any computers that are using Faronics Deep Freeze. However, I would make a few adjustments to the script. First, remove the “delete” hardware and software scheduled tasks at the system restart, but keep the scheduled tasks to trigger inventories at the system boot. Also, I’d adjust the 9 a.m. timeframe to occur a few minutes after your Deep Freeze thaw cycle occurs.

Summary: Schedule ConfigMgr Inventory

At the end of the day, these scheduled ConfigMgr inventory tasks and this PowerShell script saved my team a lot of time and effort. If you have a test lab, I recommend that you look at using this process too because it ensures that your inventory is updated regularly.

If you have any questions about how to schedule ConfigMgr inventory, please feel free to touch base with me @GarthMJ.


Additional ConfigMgr Hardware Inventory Posts

Revolutionize your reporting.

Access information not natively available in ConfigMgr and other ITSMs with Endpoint Insights.

Back to Top