Re-running a Task Sequence

What happens if your SCCM task sequence fails?  Well you troubleshoot it, fix the problem, and rerun it.  Easy to do if the task sequence is an ‘available’ deployment.  You just refresh the machine policy on the computer, go the Software Center, or Run Advertised Programs, and run it again.   And if your task sequence rerun behaviour is set to ‘Rerun if failed previous attempt’ it will run again anyway.

But what if it’s a required deployment?  There seems to be no easy ‘out-of-the-box’ way to manually rerun a task sequence.  Powershell to the rescue!

This little script will rerun the task sequence by connecting to the remote machine, deleting the TS schedule from WMI, and restarting the SMS Agent Host service.  After a few seconds, the TS will run again.

You need the Task Sequence Package ID, which you can get easily from the SCCM Console, and you should have Powershell remoting configured in your environment.  You can of course run the script on a local machine by removing the ‘PSSession’ commands.

<#
ReRunTaskSequence v1
-----------------

This script reruns a task sequence on a remote computer.

Tested on: SCCM2012R2

-Prereqs:-
> You need the PackageID of the Task Sequence which you can get from the SCCM Console
> Powershell remoting must be permitted on the remote computer
> You should have local admin rights on the remote computer to restart the ccmexec service
> Variables, enter the $ComputerName and $TSID (PackageID) variables
#>

# Enter remote computername
$ComputerName = "remotecomputer"

# Start a remote PSSession
$s = New-PSSession -ComputerName $ComputerName

# Main script
Invoke-Command -Session $s -ScriptBlock `
{
$TSID = "SMS000B1"
Get-WmiObject -Namespace "root\ccm\scheduler" -Class ccm_scheduler_history | where {$_.ScheduleID -like "*$TSID*"} | ft ScheduleID # Outputs the Schedule ID
Get-WmiObject -Namespace "root\ccm\scheduler" -Class ccm_scheduler_history | where {$_.ScheduleID -like "*$TSID*"} | Remove-WmiObject # Deletes the Schedule
Get-WmiObject -Namespace "root\ccm\scheduler" -Class ccm_scheduler_history | where {$_.ScheduleID -like "*$TSID*"} | ft ScheduleID # No output confirms the deletion
Get-Service | where {$_.Name -eq "CCMExec"} | Restart-Service # Restart the SMS Agent Host service
}
Remove-PSSession $s

More info:

http://www.scconfigmgr.com/2012/11/21/re-run-task-sequence-with-powershell/
http://blogs.msdn.com/b/steverac/archive/2009/11/06/sccm-forcing-a-task-sequence-to-rerun.aspx

Advertisements

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 )

Google+ photo

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

Connecting to %s