WinPE and .NET (Part 1): Get Task Sequence Variables

When WinPE greeted me with the “.NET Framework” component I thought nothing of it…Then I decided to use it and couldn’t have seen more opportunity than when I first saw OSD.

I plan to make this a series of posts utilizing the .NET Framework with OSD ranging from the basics of Getting Task Sequence Variables, Setting Task Sequence Variables, and even adding these to make a visually rich WPF Task Sequence Wizard. I’ll focus on the C# language that Microsoft has made so easy to love. Although, keep in mind all these things can be done with C++ and .NET as well.

First things first we need the necessary dll in order to use the Task Sequence Environment.

Let me break and mention that there are 2 examples out there that explain this part a bit more than I will and may also provide more details for you:

Whether or not you’re following my blog or one of the two references there are a few prerequisites:

Creating the Necessary DLL

First we’ll need to crack open our boot media and get the tscore.dll file. We’ll use the handy DISM tool to do this:

  1. Get a SCCM Boot media ISO and mount it.
  2. Open the appropriate folder for your architecture and grab the tscore.dll file:
    1. <root>\SMS\bin\i386\tscore.dll
    2. <root>\SMS\bin\x64\tscore.dll
  3. Copy this locally.
  4. You’ll need to run the type library importer (tlbimp.exe) against this. As mentioned in John Socha-Leialoha’s blog above, this is specific to the .NET version you’ll be compiling in and installed to the boot media. A safe bet is .NET 4.0 (at least at this point in time) so we’ll go with that:
  5. “%WindowsSdkDir%\Bin\TlbImp” <PathFromStep3>\TSCore.dll”
  6. This will spit out a TsEnvironmentLib.dll file for us to import to Visual Studio.

Importing the DLL

For those of you familiar with adding references this should be fairly straight forward:

  1. Create your solution in Visual Studio and make sure to set the Target Framework to .NET Framework 4.
  2. In the Solution Explorer right-click References
  3. select Add Reference
  4. Click Browse and select the dll.
  5. Add the appropriate “using” to your code and you’re ready!

Getting Task Sequence Variables

There are a lot of variables in a Task Sequence and even more if you’re as customized as my solutions usually are. We’ll focus on the built-in variables for simplicity. Here are a few TechNet articles to become familiar with:

In the code we’ll need to get an instance of our Task Sequence Environment:

ITSEnvClass TsEnvVar = new TSEnvClass();

Now we could access any variable (if we know the name of it) which will return the value:

TsEnvVar["OSDComputerName"];

In order to get all of the variables we have set within our OSD environment, simply call the “GetVariables” method of our TSEnvClass instance which returns an array of our variable Names (not values).

object[] x = (object[])TsEnvVar.GetVariables();

You can then loop through the array from the snippet above to get all variables along with their values:

for (int i = 0; i < x.Length; i++)
{
    Console.WriteLine(x[i].ToString() + "\t:\t" + TsEnvVar[x[i].ToString()]);
}

And there we have it! For my own purposes I’ve created a nice little utility that will spit out all of the variables in a variety of formats. The solution is published to my GitHub here.

Up Next, Creating and Editing those variables!

 

Enjoi!

Advertisements

7 thoughts on “WinPE and .NET (Part 1): Get Task Sequence Variables

  1. Pingback: WinPE and .NET (Part 2): Creating and Editing Variables | Noah Swanson's Tech Blog

  2. Pingback: WinPE and .NET (Part 3): WPF OSD Task Sequence Wizard | Noah Swanson's Tech Blog

    • You won’t be able to run/debug in Windows, yes. The Microsoft.sms.tsenvironment is only registered while a task sequence is running.

      What I do is have a vm with a pre start command (this is loaded when a pre start is running) then f8, map a drive back to my debug folder and can test that way

  3. Many thanks, Your Post is very useful ! Do you know if we need to compile the C# exe for each new SCCM CB (1703 -1706 -1709 ) or the TsEnvironmentLib.dll works for all SCCM version ?

    • It all depends on what you’re doing. If you’re just getting and setting variables you should be okay. I would look for major version changes with the dll to know for sure

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s