Howdy. I’ve been spending the past month or so automating a lot of repeatable tasks, none of which have been groundbreaking. Yet, I don’t want to leave you hanging, so I thought I’d share a quick PowerShell script I created to initiate a deployed Task Sequence on a client machine. It is a nice tool to keep in your utility belt and you can use it to automate any deployed application that may appear as available in Software Center.

The script syntax is:

Execute-TaskSequence.ps1 -TSName <TSNAME>

Obviously, the TSName can be edited to an Application name, but I needed this specifically for a project that needed to automate the initiation of a Task Sequence. Stop yelling at me.

This will initiate a Machine Policy Evaluation and Retrieval a maximum of five times (15 second intervals) before it aborts, just in case you ran the script but forgot to deploy the TS or add the machine to a collection. It will stop requesting policy once the TS is found.

Param (
[parameter(Mandatory=$true)]
$TSName,
$trigger = "{00000000-0000-0000-0000-000000000021}",
$machine = $env:COMPUTERNAME
)
$VerbosePreference = "continue"
## Connect to Software Center
Try {
Write-Verbose "$((Get-Date).ToShortTimeString()) - Connecting to the SCCM client Software Center..."
$SoftwareCenter = New-Object -ComObject "UIResource.UIResourceMgr"
}
Catch {
Write-Verbose "$((Get-Date).ToShortTimeString()) - Failed to connect to Software Center."
exit 1
}
## Runs a maximum of 5 times until TS is present; abort if not found
for ($i = 1; $i -le 5; $i++) {

##Initiates trigger of Machine Policy Retrieval and Evaluation
Write-Verbose "$((Get-Date).ToShortTimeString()) - Trying to invoke Machine Policy Retrieval..."
Invoke-WmiMethod -ComputerName $machine -Namespace root\ccm -Class sms_client -Name TriggerSchedule $trigger | Out-Null
Start-Sleep -Seconds 15

## Search for Task Sequence
Write-Verbose "$((Get-Date).ToShortTimeString()) - Searching for $TSName..."
$TS = $SoftwareCenter.GetAvailableApplications() | ?{$_.PackageName -eq "$TSName"}
## If Task Sequence is found
If ($TS) {
$i = 5
Write-Verbose "$((Get-Date).ToShortTimeString()) - Found $TSName."

## Attempt to execute the Task Sequence
Try {
Write-Verbose "$((Get-Date).ToShortTimeString()) - Executing $TSName..."
$SoftwareCenter.ExecuteProgram($($TS.ID),$($TS.PackageID),$true)
Write-Verbose "$((Get-Date).ToShortTimeString()) - $TSName executed."
}
Catch {
Write-Verbose "$((Get-Date).ToShortTimeString()) - Failed to run $TSName"
exit 1
}
}
else {
if ($i -ne 5) {
Write-Verbose "$((Get-Date).ToShortTimeString()) - Could not find $TSName. Trying $(5 - $i) more time(s)."
}
else {
Write-Verbose "$((Get-Date).ToShortTimeString()) - Could not find $TSName after maximum attempts. Exiting script."
exit 1
}
}
}

 

 

 

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