Check Disk Number

I wrote this script to detect the disk number and assign it a variable in the SCCM task sequence. Most hardware manufactures always set the default disk to 0. However, there are some instances where that is not the case. Running this script before the task sequence formats the hard drive will assign the correct disk number. In this script I use the Get-Disk command-let and grab the first value. You can also adapt other methods with a “Get-WmiObject -Class Win32_LogicalDisk” command, but I decided to go with this method since it was pretty straight forward.

# NOTE: Only use if you are running this script within an task sequence.  Otherwise you will need to adjust or remove the $tsEnv variables.
# Invoke Task Sequence Environment Object.
$tsEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment

# Query active disk(s) and select the first.
$diskNumber = (Get-Disk | Select-Object Number -First 1).Number

# If Get-Disk determines the number, assign the Task Sequence variable.
if ($diskNumber -eq 0) {$tsEnv.Value("diskNumberIs") = "0"}
if ($diskNumber -eq 1) {$tsEnv.Value("diskNumberIs") = "1"}
if ($diskNumber -eq 2) {$tsEnv.Value("diskNumberIs") = "2"}

# If Get-Disk determines there is no disk, assign the Task Sequence variable.
if ($diskNumber -eq $null) {$tsEnv.Value("diskNumberIs") = "NULL"}

# The "diskNumberIs" Task Sequence variable is then assigned to the "Partition Disk # - UEFI" options to be evaluated.

In SCCM you can assign the PowerShell Script above and set the Output Variable to “diskNumberIs”. This will tell SCCM to capture this value from the script that was run.

Then on the task to format the drive, set a condition to only run if the “diskNumberIs” variable is set to the corresponding disk.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.