top of page
  • Writer's pictureshishir kushawaha

PowerShell to dynamically set the disk for Windows OS Deployment

Introduction: During imaging with USB drives, there is a risk of the USB drive being assigned disk number 0 instead of the intended hard disk. This can lead to unintended formatting of the USB drive if disk 0 is referenced in the format and partitioning steps of the SCCM or MDT task sequence. To address this issue, it is crucial to assign the correct disk for formatting, ensuring that the operating system (OS) is installed on the desired disk.

In this blog post, we will explore a PowerShell script that efficiently help in:

  1. Proper disk assignment for formatting

  2. Excluding USB drives

  3. Handling scenarios with multiple hard disks or SSDs

The script is available at GitHub. Please find the code snippet below:


$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$logfile = "$($tsenv.Value('_SMSTSLogPath'))\WindowsDeploymentCustom.log"
Write-Output "SECTION START : Setting proper disk number for formatting : $(Get-Date)" | Out-File $logfile -append
Write-Output "" | Out-File $logfile -append
$listdisk=get-physicaldisk | Select-Object DeviceID,FileSystem,Friendlyname,Manufacturer,Model,SerialNumber,Size,BusType,MediaType,OperationalStatus
$osDiskNumber=$null
$listdisk | Out-File $logfile -append
Write-Output "" | Out-File $logfile -append
if($null -ne $listdisk)
{
    if($listdisk.count -gt 1)
    {
        Write-Output "More than 1 disk exists." | Out-File $logfile -append
        $nonUSBDisks=$listdisk | Where-Object { $_.BusType -ne 'USB'} 
        if($null -ne $nonUSBDisks)
        {
            if($nonUSBDisks.count -gt 1)
            {$osDiskNumber=($nonUSBDisks | Sort-Object -Property Size | Select-Object -First 1).DeviceID}
            else{$osDiskNumber=$nonUSBDisks.DeviceID} 
        }
        else 
        {
            Write-Output "No NON-USB disks available." | Out-File $logfile -append
            $osDiskNumber=10
        }
        $TSEnv.Value("OSDDiskIndex") = $osDiskNumber    
      }
    else 
    {   Write-Output "Only one disk available." | Out-File $logfile -append
        if(($listdisk | Where-Object { $_.BusType -ne 'USB'}).count -eq 0)
        {
            $osDiskNumber=10
            $TSEnv.Value("OSDDiskIndex") = $osDiskNumber
            Write-Output "Only USB disk." | Out-File $logfile -append
        }
    }Write-Output "OS DISK number set = $($TSEnv.Value("OSDDiskIndex"))" | Out-File $logfile -append
}
else 
{Write-Output "No disks available." | Out-File $logfile -append}
Write-Output "SECTION END : Setting proper disk number for formatting : $(Get-Date)" | Out-File $logfile -append

Processing of PowerShell Script Step 1: Creating the Environment and Log File

To start, the script initializes the necessary environment and sets up a log file. It utilizes the Microsoft.SMS.TSEnvironment COM object to handle task sequence environment variables. The log file is created based on the _SMSTSLogPath variable and will be used to track the script's execution details.


Step 2: Retrieving Disk Information

The script proceeds by obtaining information about all the physical disks present on the system. It utilizes the Get-PhysicalDisk cmdlet to gather properties such as DeviceID, FileSystem, FriendlyName, Manufacturer, Model, SerialNumber, Size, BusType, MediaType, and OperationalStatus for each disk.


Step 3: Identifying the Proper Disk for Formatting

To ensure that the correct disk is selected for formatting, the script employs a series of conditions:


3.1. Multiple Disks Exist:

If there are more than one disk available, the script takes the following steps:

  • It filters out USB disks by examining the BusType property. Disks with a BusType of 'USB' are excluded from further consideration.

  • Next, the script determines whether multiple non-USB disks remain. If so, it selects the disk with the smallest size as the target disk for the OS installation.

  • In the case of only one non-USB disk being available, that disk is designated as the OS disk.

3.2. No Non-USB Disks Available:

If there are no non-USB disks detected, it indicates that no suitable disks are present for the OS deployment. It may be possible that you are left with only multiple USB disks. So, those will get disk number starting from 0. In this situation, the script assigns a random value of 10 to the OS disk number to avoid formatting USB.


3.3. Only One Disk Available:

When there is only a single disk in the system, the script checks if it is a USB disk. If it is identified as a USB disk, the script assigns a random value of 10 to the OS disk number. This ensures that USB disks are not inadvertently formatted during the imaging process.


Step 4: Updating the Environment Variable

Once the appropriate disk for formatting has been determined, the script updates the OSDDiskIndex environment variable with the identified disk number using the $TSEnv.Value("OSDDiskIndex") syntax.


Notes:
It is essential to have PowerShell module integration in the SCCM/MDT boot image as a prerequisite for utilizing this script.
The advantage of using this script is that there is no need to manually modify the 'Disk number'. The script dynamically assigns the appropriate disk number using the 'OSDDiskIndex' PowerShell variable, eliminating the need for manual intervention.
The script must be used before any 'Format and Partition Disk' task sequence step. Also, no need to change the disk number in the step to any other number than 0.

Conclusion:

The PowerShell script showcased in this blog post provides a reliable and effective solution for assigning the proper disk number during SCCM/MDT imaging to avoid accidental formatting of USB disks. By excluding USB disks, accommodating multiple hard disks and SSDs, and ensuring the correct disk is selected for formatting, the script streamlines the deployment process, minimizing the risk of errors. Proper disk identification is essential for a smooth and successful SCCM/MDT imaging process, and this script provides a robust solution to address the disk numbering challenge.

527 views0 comments

Recent Posts

See All

PowerShell Flow Control and Conditional Statements

PowerShell Flow Control and Conditional Statements are fundamental concepts that allow you to control the execution flow of your scripts based on specific conditions. They provide the flexibility to m

bottom of page