In Part 1 we saw how to prepare a visual studio solution to be able to access the OSD Environment as well as obtain one specific or all of our task sequence variables. In there I linked a full solution that allows you to get all of those variables in a variety of file formats (txt, csv, xml, etc…).
In Part 2, I’ll be going over creating and setting Task Sequence variables via c# and .NET. If you have not read Part 1, please do. There are some important references and steps that need to be performed in order to properly access the environment.
- WinPE and .NET (Part 1): Get Task Sequence Variables
- WinPE and .NET (Part 2): Creating and Editing Variables (This Post)
- WinPE and .NET (Part 3): WPF OSD Task Sequence Wizard (Coming soon!)
As highlighted in Part 1, we need to:
- Extract and create our DLL (If you did this in Part 1, you can reuse this DLL)
- Create a solution setting the target framework to “.Net Framework 4”
- Reference the assembly in your solution
Now, just like we did in Par 1, we’ll need to create an instance of our Task Sequence environment:
ITSEnvClass TsEnvVar = new TSEnvClass();
If you recall the way we obtained variable names was using the GetVariables method of our Task Sequence Environment Instance. To get the value of a specific variable we accessed this by:
Creating and Setting variables is just as easy! In fact, its the exact same! With our Task Sequence Environment Instance (TsEnvVar):
TsEnvVar["VariableName"] = "VariableValue";
Really…that’s all there is to it! I’d like to say there’s more magic behind this but really there isn’t!
To help with your own implementations of this, I’ve created a helper class that can be used for getting and setting variables. While is is very basic, it does give a good ‘skeleton’ to work with. I’ve used this class in countless projects for both my company and other experiments/tests I’ve done. You can view this class here.
Up Next: In Part 3 I’ll go through a very quick and basic Task Sequence Wizard using WPF and C#!