Jaa


Reading and Writing Task Sequence Variables in .NET

I’m working on a program that needs to read Configuration Manager task-sequence variables while running in the full operating system (as apposed to Windows PE). Reading task sequence variables is really quite simple. However, all the examples are either in VB Script, or C++. I’m writing this program in C#, so I need a managed way to read task sequence variables.

First, some background. You can find more information about task-sequence variables is this article on MSDN: About Task Sequence Variables. The object you need to use is a COM object. C#, as well as other .NET languages, has good support for working with COM objects. However, it’s not always obvious how to reference a COM object in .NET. The normal way is to open the Add Reference dialog box, click the COM section, locate the DLL and then click Add. However, there are cases where this isn’t sufficient, and working with the COM object for task-sequence variables is one of these cases.

Some COM DLLs, such as the TsCore.dll that we need to reference, have what’s known as an embedded type library. This is a description of the COM objects that are exposed by the DLL. In order to use such a DLL, you need to create an interop assembly that provides a bridge between .NET and COM. This is done using a program called TlbImp.

The following command line will run TlbImp and create a new DLL called TsEnvironmentLib.dll:

"%WindowsSdkDir%\Bin\TlbImp"”%SystemRoot%\CCM\TSCore.dll”

Once you run this command, you can open the Add Reference dialog box, click the Browse button and select TsEnvironmentLib.dll, which is a .NET assembly. At this point, reading a task sequence variable looks something like this:

 TSEnvClassClass ts = new TSEnvClassClass();
return ts["MyVarName"];

There is one final issue. You need to make sure you use a version of TlbImp that matches the .NET version you’re targeting. In the case above, I needed to use .NET 3.5 since the target machine probably won’t have .NET 4 installed on it. I tried the different versions of TlbImp that were on my computer, and the one in the Windows SDK folder on my computer targets .NET 3.5. You can see which version TlbImp targets by running it without any parameters.

Comments

  • Anonymous
    March 15, 2011
    Hi, Thanks for the post. How would I enumerate through the Task Sequence Variables?

  • Anonymous
    March 21, 2011
    Hi, very usefull. Is it possible to get all variables, like from multiple installation?

  • Anonymous
    March 21, 2011
    I got it: object[] x = (object[])ts.GetVariables();                for (int i = 0; i < x.Length; i++)                {                    console.WriteLine("Variable({0})-{1}= {2}", i + 1, x[i].ToString(), ts[x[i].ToString()]);                }

  • Anonymous
    June 12, 2014
    using System; using System.Collections.Generic; using System.Text; using TSEnvironmentLib; using System.Runtime.InteropServices; namespace ConsoleApplication2 {    class Program    {        static void Main(string[] args)        {            String message = "";            try            {                TSEnvClassClass ts = new TSEnvClassClass();                Console.WriteLine(ts["MyVarName"]);            }            catch (Exception)            {                int error = Marshal.GetLastWin32Error();                message = String.Format("COM32 Error {0}", error);                Console.WriteLine("Err="+error.ToString() + "  "+ message);            }        }    } } Runnning SSCM 2007 I build a console app ."net 2.0" and used the TlbImp from the 2.0 SDK. But I get a com error 1008 in the  "...= new..." line Any Hints?