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.
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
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 !
$tsenv.Value("WebSite") = "www.PowerShellDistrict.com"
Now, you can access the variable “MyVariable” through your task sequence by using the the method described above:
$CurrentWebSite = $tsenv.Value("WebSite")
$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).