SCCM 2012: Execute task sequence with PowerShell

Here is a PowerShell script to initiate a task sequence with PowerShell. The script takes one argument – the name of the task sequence. The task sequence must be deployed/advertised to the machine already. It searches the deployed task sequences on the machine and if it finds a task sequence matching the one you specified, it will execute it.

Executing a task sequence this way bypasses the prompt to install a new operating system, which may be useful if you have a task sequence that just performs a user capture or restore, or if you use a task sequence for purposes other than OSD and you don’t want the “install a new operating system” warning dialog.

\"\" Install Operating System Warning Dialog

You could potentially use this script to deploy task sequences from the ConfigMgr 2012 Application Catalog. This would give you the benefits of the application model such as dependency and requirement rules as well as the benefits of the Application Catalog with its request/approval system. You could then use the Application Catalog as a request/approval portal for OSD refresh scenarios, something that usually requires a third party tool such as SCCM Expert.

In order to make this work, you would need to deploy an application that runs a script which does the following:

  1. Add the machine to a collection that has the task sequence deployed to it.
  2. Trigger a machine policy evaluation.
  3. Trigger the task sequence.

The application deployment type would need a detection rule to indicate that the task sequence has executed successfully so that the application installation doesn’t interfere with the task sequence. The detection rule could vary depending on what the task sequence does.

I haven’t tried the above and one of the potential issues would be granting the client/script permission to add a machine to a collection. Who knows, maybe Microsoft has plans to allow task sequences to be published to the Application Catalog in a future service pack for ConfigMgr 2012.

I have tested this script with ConfigMgr 2012, but it should with with ConfigMgr 2007 also.

Function Execute-TaskSequence {
    Param (
        [parameter(Mandatory = $true)]
        [string]$Name
    )
    Try {
        Write-Host "Connecting to the SCCM client Software Center..."
        $softwareCenter = New-Object -ComObject "UIResource.UIResourceMgr"
    }
    Catch {
        Throw "Could not connect to the client Software Center."
    }
    If ($softwareCenter) {
        Write-Host "Searching for deployments for task sequence [$name]..."
        $taskSequence = $softwareCenter.GetAvailableApplications() | Where { $_.PackageName -eq "$Name" }
        If ($taskSequence) {
            $taskSequenceProgramID = $taskSequence.ID
            $taskSequencePackageID = $taskSequence.PackageID
            Write-Host "Found task sequence [$name] with package ID [$taskSequencePackageID]."
            # Execute the task sequence
            Try {
                Write-Host "Executing task sequence [$name]..."
                $softwareCenter.ExecuteProgram($taskSequenceProgramID, $taskSequencePackageID, $true)
                Write-Host "Task Sequence executed."
            }
            Catch {
                Throw "Failed to execute the task sequence [$name]"
            }
        }
    }
}

8 thoughts on “SCCM 2012: Execute task sequence with PowerShell”

  1. Greg Kourosh

    Thanks for this, very useful. I did find a bug, however. I’m using this function inside of the appdeploy toolkit, and having the toolkit start the TS. Since I’m chaining one to the other, I named the package/program the same as the TS. Which is not a problem for SCCM. But, the function then returns more than one tasksequencePackageID and fails. I modified the code to check $tasksequence.name, which is “*” for a TS and some other string for a package. Thanks again!

  2. Greg Kourosh

    Edit – Turns out my issue is that the function does not work when run in they SYSTEM context (as SCCM packages do). My solution is to have a seperate script (RunTS) that has this function in it and use this code in the Appdeploy Toolkit to call it:

    write-host $PSScriptRoot
    Execute-ProcessAsUser -Path “$PSHOME\powershell.exe” -Parameters “-Command ” -Wait
    Start-Sleep 300
    Wait-Process TSProgressUI

  3. This used to work great. Not sure what changed recently. Any idea as to why I am getting the below? If I manually launch it then it goes but running it from powershell as admin fails everytime.

    Failed to execute the task sequence [Feature Update – Windows 10 Ent 20H2]
    + CategoryInfo : OperationStopped: (Failed to execu…t 20H2]:String) [], RuntimeException
    + FullyQualifiedErrorId : Failed to execute the task sequence [Feature Update – Windows 10 Ent 20H2]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top