Hi. My name is Jesse, and I’m a script-aholic.

I also enjoy sleep, which is typically what motivates me to automate many processes using Powershell. Assuming that you enjoy sleeping as much as I do, I’ll gladly share with you a recent script that I wrote.


I was tasked to deploy an application via ConfigMgr to a group of VPN users around the world. Timing was of the essence, and I needed to ensure that the clients received policy as soon as they were available. However, as the users were global and it was uncertain as to exactly when they would be checking in, I needed to devise a way to detect network connectivity and trigger the machine policy evaluation and retrieval, even as it occurred overnight.

After grouping the machines into a collection in which the application was deployed, I decided to write the following script to automate the policy retrieval. The script does the following:

  • Imports the ConfigMgr module and changes to the site directory
  • Grabs all of the machine names in the defined collection
  • Grabs a count of machines in the collection
  • Starts a do-while loop that continues until $i equals the total count of the collection
  • For each machine, it tries to connect once
    • If successful
      • Triggers the Machine Policy Evaluation and Retrieval
      • Removes the machine from the $Machines variable list (not the collection) so as to not continuously check the same successful machine
      • Increments the value of $i for the do-while loop
      • Writes the machine name and time of successful connectivity to .\MachineEvalTrigger.txt
    • If failed connectivity
      • Will continuously try each member of the $Machines variable until $i reaches the count of machines in the variable

There is a mandatory parameter, so the syntax is:

.\Trigger-MachinePolicyEval.ps1 –CollectionName <CollectionName>

The script is below. Now, catch up on some beauty sleep!


Name: Trigger-MachinePolicyEval.ps1

Author: Jesse Walter

Date: 02/25/2015



Function Import-CMModule

$Drives = Get-PSDrive -PSProvider FileSystem | Where-Object -Property Free
$FreeDrives = $Drives.root
$CMLets = "configurationmanager.psd1"

if (Test-Path -Path "c:\program files (x86)\Microsoft Configuration Manager\AdminConsole\bin\configurationmanager.psd1")
Write-Host "Importing from C:"
Import-Module "c:\program files (x86)\Microsoft Configuration Manager\AdminConsole\bin\configurationmanager.psd1"

ForEach ($drive in $FreeDrives)
Write-Host "Attempting to locate CMLets on $drive..." -ForegroundColor Yellow
cd $drive
$FilePath = (gci $drive -File -Filter $CMLets -Recurse -Force -ErrorVariable FailedItems -ErrorAction SilentlyContinue).FullName
if ($FilePath)
$CMModule = $FilePath
Write-Host "Found $CMModule. Importing..." -ForegroundColor Green

Import-Module $CMModule
Write-Host "Cannot find in $drive..." -ForegroundColor Red


$site = (gwmi -ComputerName $env:COMPUTERNAME -Namespace "root\SMS" -Class "SMS_ProviderLocation").SiteCode
$sitecodeDir = $site + ":"
CD $sitecodeDir
$Machines = (Get-CMDevice -CollectionName $CollectionName).name

$i = 0

do {

ForEach ($machine in $Machines){
if (Test-Connection -ComputerName $machine -Count 1 -Quiet) {
$trigger = "{00000000-0000-0000-0000-000000000021}"
$time = (Get-Date).DateTime
$Machines = $Machines | ?{$_ -ne $machine}
Invoke-WmiMethod -ComputerName $machine -Namespace root\ccm -Class sms_client -Name TriggerSchedule $trigger
Write-Host $machine "is connected" -ForegroundColor Green
Add-Content -Value "$machine $time" -Path .\MachineEvalTrigger.txt
else {Write-Host $machine "not reachable" -ForegroundColor Yellow}

while ($i -le $Machines.count)

About the Author: Jesse Walter

Jesse Walter is a Partner with Model Technology Solutions and the Vice President of Research and Development. He has an extensive background in Microsoft endpoint management tools, such as Microsoft Endpoint Configuration Manager and Intune, as well as a strong foundation in the Microsoft 365 Defender stack. Additionally, he enjoys automating repeatable operational tasks using PowerShell, and has developed several security tools using C#.

Three Minutes For A More Secure & Efficient Infrastructure

Short and to the point, Steve’s Email Blasts give you endpoint management tips, tricks, and news in three minutes or less email read-time, guaranteed.

Model says no to spam. Privacy Policy

Model Technology Solutions

Model Technology Solutions is a small but mighty band of infrastructure experts. We’ve helped companies in diverse industries to modernize and automate their infrastructures through effectively managing their Microsoft endpoint suite.

With us on your team, you’ll watch your security and compliance go up and your IT team’s costs (and headaches) go down. You’ll relax in knowing that your endpoints will be secure and online when your users need them most. And you’ll finally get back to your most-important tasks.

Model Technology Solutions
12125 Woodcrest Executive Drive, Ste. 204 Creve Coeur, MO 63141

Phone: (314) 254-4138
General Inquiries: model@model-technology.com
Sales and Quotes: sales@model-technology.com