Initiating Deployed Applications and Task Sequences Using PowerShell
By Jesse Walter
Published September 24, 2015
Estimated Reading Time: 2 minutes

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)]
[string]$TSName,
[string]$trigger = "{00000000-0000-0000-0000-000000000021}",
[string]$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
}
}
}

 

 

 

Article By 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#.

Related Posts