MSEndpointMgr

Hide and Resume a Task Sequence in ConfigMgr using Microsoft.SMS.TSEnvironment and C#

Building application with a purpose of being used in a Task Sequence in ConfigMgr can be accomplished with various methods in different languages. Recently, I’ve started to explore C# and built a couple of applications meant for configuring the task sequence environment. If you for instance are building an application (e.g. a front end) and want to hide the progress UI, run your application and then resume the task sequence from the last instruction, the following code snippets can be handy and hopefully point you in the right direction for your project (shout out to Johan Schrevelius that got me started with loading the ComObject in the first place).

Hide Task Sequence

public static void CloseTSProgressUI()
{
	Type tsProgress = Type.GetTypeFromProgID("Microsoft.SMS.TSProgressUI");
	dynamic comObject = Activator.CreateInstance(tsProgress);
	comObject.CloseProgressDialog();
	if (System.Runtime.InteropServices.Marshal.IsComObject(comObject) == true)
		System.Runtime.InteropServices.Marshal.ReleaseComObject(comObject);
}

Resume Task Sequence

public static void ShowTSProgressUI()
{
	Type environmentType = Type.GetTypeFromProgID("Microsoft.SMS.TSEnvironment");
	dynamic tsEnv = Activator.CreateInstance(environmentType);
	Type progressType = Type.GetTypeFromProgID("Microsoft.SMS.TSProgressUI");
	dynamic tsProgress = Activator.CreateInstance(progressType);
	string orgName = tsEnv.Value["_SMSTSOrgName"];
	string pkgName = tsEnv.Value["_SMSTSPackageName"];
	string customMessage = tsEnv.Value["_SMSTSCustomProgressDialogMessage"];
	string currentAction = tsEnv.Value["_SMSTSCurrentActionName"];
	string nextInstructor = tsEnv.Value["_SMSTSNextInstructionPointer"];
	string maxStep = tsEnv.Value["_SMSTSInstructionTableSize"];
	tsProgress.ShowTSProgress(orgName, pkgName, customMessage, currentAction, nextInstructor, maxStep);
	if (System.Runtime.InteropServices.Marshal.IsComObject(tsEnv) == true)
		System.Runtime.InteropServices.Marshal.ReleaseComObject(tsEnv);
	if (System.Runtime.InteropServices.Marshal.IsComObject(tsProgress) == true)
		System.Runtime.InteropServices.Marshal.ReleaseComObject(tsProgress);
}

Nickolaj Andersen

Chief Technical Architect and Enterprise Mobility MVP since 2016. Nickolaj has been in the IT industry for the past 10 years specializing in Enterprise Mobility and Security, Windows devices and deployments including automation. Awarded as PowerShell Hero in 2015 by the community for his script and tools contributions. Creator of ConfigMgr Prerequisites Tool, ConfigMgr OSD FrontEnd, ConfigMgr WebService to name a few. Frequent speaker at conferences such as Microsoft Ignite, NIC Conference and IT/Dev Connections including nordic user groups.

1 comment

Sponsors

Categories

MSEndpointMgr.com use cookies to ensure that we give you the best experience on our website.