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:
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();
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.