Re-running a ConfigMgr Task Sequence on Multiple Computers

Recently I deployed a very simple task sequence to all our laptop computers which installs a new WiFi profile.  However, on viewing the deployment reports I noticed a number of machines where the deployment was stuck in either the ‘running’ state, or ‘failed’.  Although the deployment is set to ‘Re-run if failed previous attempt’ it seems the task sequence will not re-run automatically, only if a new schedule is created.  I wanted a quick and easy way to simply trigger the deployment again on all the computers that needed it, so I modified a script I use for re-running a task sequence to run against all computers in a csv.

Here is the script.  You need PS remoting active in your environment.  Simply add the location of the csv that contains the computernames you want to re-run the deployment on (no header required), and the name of the task sequence, and the script will do the rest.  It’s a little crude at the moment, but it works!  After running this, my compliance figures for the deployment were immediately boosted, and only a handful of machines remained that required individual troubleshooting 🙂

$ComputerNames = Get-Content -Path C:\Script_Files\WiFi_Profile_Failures.csv
$TaskSequenceName = "WiFi Profile"

cls
foreach ($ComputerName in $ComputerNames)
{ 
# Test connectivity to the computer
if (Test-Connection -Quiet -Count 2 -ComputerName $ComputerName -ErrorAction SilentlyContinue)
{$Online = "Yes"}else{$Online = "No"}
if ($Online -eq "No")
{write-host "$ComputerName is not online!"}
if ($Online -eq "Yes")
{
 
$s = New-PSSession -ComputerName $ComputerName
Invoke-Command -Session $s -Argu $ComputerName,$TaskSequenceName -ScriptBlock `
{
param ($ComputerName,$TaskSequenceName)
write-host "Getting PackageID for '$TaskSequenceName' on $ComputerName"
 $PackageID = get-wmiobject -computername $ComputerName -query "SELECT * FROM CCM_SoftwareDistribution" -namespace "root\ccm\policy\machine\actualconfig" | where {$_.PKG_Name -like $TaskSequenceName} | Select PKG_PackageID
 $PackageID = $PackageID.PKG_PackageID
 write-host $PackageID -ForegroundColor Yellow
 write-host "Getting ScheduleID for '$TaskSequenceName' on $ComputerName"
 $ScheduleID = Get-WmiObject -computername $ComputerName -Namespace "root\ccm\scheduler" -Class ccm_scheduler_history | where {$_.ScheduleID -like "*$PackageID*"} | Select-Object ScheduleID 
 $ScheduleID = $ScheduleID.ScheduleID
 write-host $ScheduleID -ForegroundColor Yellow
 write-host "Getting AdvertisementID for '$TaskSequenceName' on $ComputerName"
 $AdvertisementID = get-wmiobject -computername $ComputerName -query "SELECT * FROM CCM_SoftwareDistribution" -namespace "root\ccm\policy\machine\actualconfig" | where {$_.PKG_Name -like $TaskSequenceName} | Select ADV_AdvertisementID
 $AdvertisementID = $AdvertisementID.ADV_AdvertisementID
 write-host $AdvertisementID -ForegroundColor Yellow
 write-host "Setting re-run behaviour"
 $a = get-wmiobject -computername $ComputerName -query "SELECT * FROM CCM_TaskSequence" -namespace "root\ccm\policy\machine\actualconfig" | Where {$_.ADV_AdvertisementID -like "*$AdvertisementID*"} 
 $a.ADV_RepeatRunBehavior='RerunAlways'
 $a.Put() | Out-Null
 write-host "Creating mandatory assignment"
 $a = get-wmiobject -computername $ComputerName -query "SELECT * FROM CCM_TaskSequence" -namespace "root\ccm\policy\machine\actualconfig" | Where {$_.ADV_AdvertisementID -like "*$AdvertisementID*"}
 $a.ADV_MandatoryAssignments=$True
 $a.Put() | Out-Null
 
 write-host "Triggering the schedule now!" -ForegroundColor Green
 Invoke-WmiMethod -ComputerName $ComputerName -Namespace ROOT\ccm -Class SMS_Client -Name TriggerSchedule -ArgumentList "$ScheduleID" | Out-Null
 }
 Remove-PSSession $s
 }
 }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.