Installation succeded

Installation successfull

 

So this is a topic that you can easily go through on the internet. But I have stubbleld on a specefic case that I havent seen mentionned any where else and which I will talk about in a few. But first let’s talk about the basis.

How to read task sequence variable with PowerShell

The Task sequence environement varaibles are easily accessible through the Microsoft.SMS.TSEnvironment ComObject .

To use this com object simply use the following line in your powershell script.

You can then easilly access any existing variable using the following syntax :

Using this line will retrive the current logging path.

 

How to write new task sequence variables with Powershell ?

So know you know how to read the Built-in varaibles variables, you would most likley like create your own OSD variables ?

Well, powershell made it very very easy !

Now, you can access the variable “MyVariable” through your task sequence by using the the method described above:

$CurrentWebSite will contain the string : “www.PowerShellDistrict.com”

SCCM OSD Built-in variables list:

The most common ones (in my opionion) are the following ones :

_SMSTSInWinPE  –> contains a boolean value saying if the task sequence is currently in WinPe mode or not

_SMSTSLogPath –> Contains the path to the currently used log folder. (Since the paths are not the same depending on how far you are in your stagging process).

_SMSTSMachineName –> The machine name of the computer where the task sequence is currently executed on.

The complete variables used and accessible by SCCM during the task sequence can be found here:

  • SCCM 2012 and above click here
  • SCCM 2007 click here

Side note : All variables can be read and written except the ones starting with “_” (Underscore). They are read only variables.

So, that is for when everything goes well, and therefore we dont really really need help with (do we ??). Where it becomes a bit tricky is when you try to use your PowerShell script in a 64x Bits environment.

The 64x bits part :

So basically it is very easy to access the OSD variables using the very friendly Microsoft.SMS.TSEnvironment com object. But, there is a small drawback of using this comboject when using it on 64x bits machines : It simply doesn’t work.

Indeed, the comobject get actually always started in 32bits mode, even if it is running in a 64x bits environment. Which means that if you try to read the variables from PowerShell in a 64x bits envinroment, you will not see any variables.

When you need to access OSD variables in x64 bits mode from a PowerShell script, I recommend you try to avoid to read the varaibles directly from within the script (thus avoiding to use the Microsoft.SMS.TSEnvironment com object).

[stextbox id=”note”]Write your PowerShell script in a way so you can call the script with parameters, and give the OSD variables to your script as parameters.[/stextbox]

Still don’t see how to solve this ? see the examples below.

More SCCM osd variable manipulations:

Check my example out here: How to measure an SCCM task sequence execution time

Osd tattoo windows image –> Link

external and usefull links :

Technet article about SCCM variables : link

Article describing the Com object 64 bits issue : link

 

 

By | 2016-10-19T21:00:18+00:00 May 22nd, 2014|COM objects, Functions, PowerShell, Sticky|2 Comments

About the Author:

Stéphane is a dynamic and passionate Cloud and datacenter Microsoft MVP since. He is the founder of the Basel PowerShell user Group (BPUG), the co-founder of the French Speaking PowerShell UserGroup (FRPSUG), author, blogger, and received the community award "PowerShell Hero" from PowerShell.org. Stéphane has implemented microsoft infrastructure solutions in various countries of Europe and is currently working in Basel / Switzerland. Stéphane help his clients to reduce their global infrastructure costs by implementing Microsft infrastructure solutions by combining great products such as System Center, Windows Server, with heavy automation using Windows PowerShell. Stéphane loves languages, Belgium beer, French cheese and French Wine. If any of these topics are of your interest, don't hesitate to come and say hi.

2 Comments

  1. Tony June 17, 2015 at 2:37 am - Reply

    Hello Stephan!

    In my SCCM task sequence I am attempting use the Microsoft.SMS.TSEnvironment COM object, but have some questions. Using the the ‘Run Power Shell’ task, the command of $tsenv=New-Object -COMObject Microsoft.SMS.TSEnvironment works, but when using it a ‘Run Command Line’ task it fails with this error: New-Object : The term ‘New-Object’ is not recognized as the name of a cmdlet, function, script file, or operable InstallSoftware . Do you know what different is between these two has? Second, do you know how to make $tsenv=New-Object -COMObject Microsoft.SMS.TSEnvironment work using the ‘Run Command Line’ task? I need to be able to use it there because I need to be able to specify credentials for other portions of the script. Here is a post on TechNet that provides more details about what I am trying to accomplish.

    https://social.technet.microsoft.com/Forums/en-US/a7a217ff-ff32-4b48-9827-8286cd13f9f4/run-powershell-script-vs-run-command-line?forum=configmanagersdk#f995bdbf-3121-4456-aaa2-ffd59bd5868d

    Hope you are willing to help Stephan!

    Thanks!

    -Tony

  2. Zak November 12, 2014 at 3:30 pm - Reply

    The X64 issue that you wrote about, is this only an issue when using a PowerShell script as a prestart command? I’m using an X64 boot image and I haven’t ran into any problems creating the TSEnvironment or TSProgressUI com objects in PowerShell.

Leave a Reply

%d bloggers like this: