SCCM set a custom variable dependency using Powershell or Vbs

When you are a frequent user of SCCM, sooner or later you will want a custom variable on which u can set a dependency for jobs further on in the task sequence.

After searching online for a while and not finding a satisfying answer I decided to create a short how-to.

For this Powershell how-to I will use a situation where there a multiple locations with each having there own default gateway. Other locations might need different settings then the main location. The settings can be anything you like as long SCCM can deploy it.

First we need to create a custom variable in SCCM where we can refer to later on in the task sequence. Create a new “Run Powershell script” job in the task sequence.


Make sure that the execution policy is on “Bypass” so that the script will run correctly.
The script that is run (DG.ps1 in this case) is fairly simple:

   1: $tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment

   2: $DG = (Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway

   3: $tsenv.Value("NameofVariable") = "$DG"

First line is the line to add it to the Task sequence.

Second line is the command u want to run on the endpoint to which the task sequence is deployed to.

In this case I extract the default gateway the endpoint is having. This way I can see in which location the device is. After extracting the gateway I set the name of the variable in which the gateway is written. (I named it OSDG)

Next when I want to make a setting for a specific location I just create a new SCCM job with the following condition.


Now if the default gateway of the endpoint matches the IP set in the condition it will get these settings.

The next possible way is to use a Visual Basic script.

In this case I am using a script that extract the OU name from the endpoint.

Based on this OU name you can set dependencies in the task sequence.

Instead of running a Powershell script we will now run a .vbs script.

Make sure to do this you use the “Run Command Line” job in SCCM.

As command line use “Cscript NameOfScript.vbs”

In the script is the following text.

   1: Option Explicit

   2: Dim WshNetwork

   3: Dim ComputerName

   4: Dim objADSysInfo

   5: Dim strComputerName

   6: Dim objComputer

   7: Dim strOUName

   8: Dim strOUs

   9: Dim StrOU

  10: Dim osd

  11: Dim env


  13: Set WshNetwork = CreateObject("WScript.Network")

  14: ComputerName = WshNetwork.ComputerName


  16: Set objADSysInfo = CreateObject("ADSystemInfo")

  17: strComputerName = objADSysInfo.ComputerName

  18: Set objComputer = GetObject("LDAP://" & strComputerName)

  19: strOUName = objComputer.DistinguishedName

  20: strOUs = Split(strOUName, ",")

  21: strOU = Split(strOUs(1), "=")



  24: Set env = CreateObject("Microsoft.SMS.TSEnvironment")

  25: Env("CheckOU") = strOU(1)

In the last two lines the variable is set as “CheckOU”

After setting this variable, you can now set dependencies on this name.

To accomplish this, use the same settings as used above.

Then instead of using OSDG you can now use CheckOU (or the name you gave it) with “equals NameOfTheOU”