Using PowerShell and Task Scheduler to Schedule ConfigMgr Inventory

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 Enhansoft 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 the Recast tool. This is great, even today, BUT it doesn’t help with VMs where a snapshot is taken.

Here, at Enhansoft, 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

Schedule ConfigMgr Inventory

I am not posting the entire script here because you can download the full script from Enhansoft’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:\\root\ccm 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

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.

Schedule ConfigMgr Inventory - Task Scheduler

Leave a Comment

Computer Replacement Plan - LaptopSpooktacular Report Giveaway - Enhansoft
Share via
Copy link