Configuration Manager (SCCM) | OSD | Multiple Hard Drives | Basic

Configuration Manager (SCCM) | OSD | Multiple Hard Drives | Basic

Ever had multiple disks hardware builds and the Operating System is not getting installed on the expected hard drive…..well here are steps to overcome that

The script will identify the total number of Physical Disks excluding ‘BusType’ USB and order by ‘Size’ – smallest to largest.

The default configuration sets the ‘smallest’ sized hard drive as the Primary Disk. This can be reconfigured in the script.

The Configuration Manager variable ‘OSDDiskIndex’ will be assigned the disk number of the ‘Primary Disk’ and used in the deployment step ‘Partition Disk 0 – BIOS’ or ‘Partition Disk 0 – UEFI’, ‘Pre-provsion BitLocker’ and Apply Operating System.

Remaining, detected hard drives will be formatted as a simple volume.

More information on the CM variable OSDDiskIndex:  

https://docs.microsoft.com/en-us/mem/configmgr/osd/understand/task-sequence-variables#OSDDiskIndex

Pre-Requisites

To enable the script to run the following optional components will need to be added to your Boot Image – WinPE-EnhancedStorage , WinPE-StorageWMI), Microsoft .NET (WinPE-NetFx and WinPE-PowerShell

  1. Set Disk and Format – PowerShell Script
Configuration Manager Set Disk and Format

Add a ‘Run Powershell Script’ to the task sequence before the step ‘Partition Disk’. 

Type:  Run PowerShell Script

Edit Script: Copy and Paste PowerShell script

PowerShell Execution Policy: Bypass

<#
.WARNING
    This script is destructive as it contains Clear-Disk

.SYNOPSIS
    Warning Destructive - Created to run in WinPE OSD to Clear-Disk and assign OSDDiskIndex to task sequence Disk Advanced Option Partition Variable

.DESCRIPTION
    Runs in WinPE OSD to find all non USB Bus Types, format disk drive(s) and assign smallest disk as OSDDiskIndex variable to be used in the
    task sequence

.NOTES
    File Name      : osddiskcleanbasic.ps1
    Author         : S.P.Drake

.COMPONENT
    (WinPE-EnhancedStorage),Windows PowerShell (WinPE-StorageWMI),Microsoft .NET (WinPE-NetFx),Windows PowerShell (WinPE-PowerShell). Need to
    added to the WinPE Boot Image to enable the script to run

    The COMObject Microsoft.SMS.TSEnvironment is only available in MDT\SCCM WinPE
#>


# Log file function, just change destaination path if required
Function Write-Log {
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$False)]
    [ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
    [String]
    $Level,

    [Parameter(Mandatory=$False)]
    [string]
    $Message,

    [Parameter(Mandatory=$False)]
    [string]
    $logfile = "$($TSEnv.value('_SMSTSLogPath'))\OSD_DiskSearchandClean.log"
    )

    $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
    $Line = "$Stamp $Level $Message"

    If($logfile) {
        Add-Content $logfile -Value $Line
    }
    Else {
        Write-Output $Line
    }
}


# Outer Try Catch
try{

# Import Microsoft.SMS.TSEnvironment
$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment

# Read Task Sequnce Variable _SMSTSBootUEFI and set Partition Style
if ($TSEnv.Value('_SMSTSBootUEFI') -eq $True){$Style = 'GPT'} else {$Style = 'MBR'}

Write-Log -Message  "All BusTypes excluding USB are searched"
Write-Log

# Get only physical disks that are not BusType USB and order by size (smallest to largest)
$physicalDisks = Get-PhysicalDisk | Where-Object {$_.Bustype -ne 'USB'} | Sort-Object -Property @{Expression = "Size"; Descending = $False}

    # Did we find any matching physical disks ?
    if ($null-eq $physicalDisks) {
        Write-Log -Message "No physical disks have been detected"
        Write-Log
    }
    else {
        Write-Log -Message "The following physical disks have been detected: -"
        Write-Log

        # Display all physical disks that have been found
        foreach ($disk in $physicalDisks) {
                Write-Log -Message "FriendlyName:  $($disk.FriendlyName)"
                Write-Log -Message "MediaType:  $($disk.MediaType)"
                Write-Log -Message "BusType:  $($disk.BusType)"
                Write-Log -Message "Size:  $($disk.Size /1GB)"
                Write-Log -Message "DeviceID:  $($disk.DeviceID)"
                Write-Log
        }

     }

   # Display action to be performed
    $firstItem = 0
    foreach ($disk in $physicalDisks) {
            # Is it the first item in the list ?
            if ($firstItem -eq 0){

                # Get first physical disk in our list - Ordered by BusType and Size
                Write-Log -Message "The physical drive $($disk.FriendlyName) of Bustype $($disk.BusType) and Media Type $($disk.MediaType) on Device ID : $($disk.DeviceId) will be assigned to OSDDiskIndex"

                # Assign task sequence variable OSDDiskIndex
                $TSEnv.Value('OSDDiskIndex') = $disk.DeviceId

            }
            else {

                Write-Log -Message "The physical drive $($disk.FriendlyName) of Bustype $($disk.BusType) and Media Type $($disk.MediaType) on Device ID : $($disk.DeviceId) will be cleaned and used as a Data Disk"

                # If disk is new and Partition Style 'RAW' then Initialize Disk
                if (get-disk -Number $disk.DeviceId | Where-Object {$_.PartitionStyle -eq 'RAW'}){Initialize-Disk -Number $disk.DeviceId -PartitionStyle $style}

                # Clear disk partition and data
                Clear-Disk -Number $disk.DeviceId -RemoveOEM -RemoveData -Confirm:$false
                Write-Log -Message "Command: Clear-Disk -Number $($disk.DeviceId) -RemoveOEM -RemoveData -Confirm:$false"

                # Initialize-Disk
                Initialize-Disk -Number $disk.DeviceId -PartitionStyle $style
                Write-Log -Message "Command: Initialize-Disk -Number $($disk.DeviceId) -PartitionStyle $($style)"

                # Create and format data disk
                New-Partition -DiskNumber $disk.DeviceId -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel Data -Confirm:$False
                Write-Log -Message "Command: New-Partition -DiskNumber $disk.DeviceId -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel Data -Confirm:$False"
                Write-Log

            }
    $firstItem = $firstItem +1
    }
}catcH{
    Write-Log
    Write-Log -Level ERROR -Message $_.Exception.Message
    Write-Log
    Exit 1
}

  1. Configure Partition Disk 0 – BIOS
Configuration Manager Partition Disk BIOS

Partition Disk 0 – BIOS : Select the ‘Windows (Primary)’ volume and Set the ‘Advance options Variable’ to ‘OSDisk’.

The variable ‘OSDisk’ will be used in the ‘Apply Operating System’ to ensure the operating system gets applied to the correct hard drive.

NOTE: There is no need to change the ‘Disk number’, as this will be ignored and dynamically assigned in the PowerShell script ‘OSDDiskIndex’

  1. Configure Partition Disk 0 – UEFI
Configuration Manager Partition Disk UEFI

Partition Disk 0 – UEFI : Select the ‘Windows (Primary)’ volume and Set the ‘Advance options Variable’ to ‘OSDisk’.

The variable ‘OSDisk’ will be used in the ‘Apply Operating System’ to ensure the operating system gets applied to the correct hard drive.

NOTE: There is no need to change the ‘Disk number’, as this will be ignored and dynamically assigned in the PowerShell script ‘OSDDiskIndex’

  1. Pre-provision BitLocker
Configuration Manager Pre-Provision Bitlocker

Pre-Provision Bitlocker :  Under ‘Apply BitLocker to the specified drive’, select the ‘Properties’ tab and change the ‘Destination’ to Logical driver letter stored and ‘Variable name’ to OSDisk 

NOTE: The Pre-Provision is applied to the Operating System as the ‘OSDisk’ variable was assigned in the PowerShell script

  1. Apply Operating System
Configuration Manager Apply Operating System

Apply Operating System : Select the ‘Properties’ tab and change the ‘Destination’ to ‘Logical driver letter stored in a variable‘ and ‘Variable name’ to ‘OSDisk

NOTE: The Operating System is applied to the correct Drive as the ‘OSDisk’ variable was assigned in the PowerShell script

Log log file of the actions performed can be found at C:\Windows\CCM\Logs\OSD_DiskSearchandClean.log

Downloadable source files can be found at https://github.com/Drakey2000/CommunityHelper/tree/master/OSDDiskFormat

Update : Since writing, Microsoft has enhanced the Task Sequence Step Properties for ‘Format and Partition Disk‘. So check in out

https://docs.microsoft.com/en-us/mem/configmgr/osd/understand/task-sequence-steps#BKMK_FormatandPartitionDisk

This enables you to use scripts to assign variables to all disks, not just ‘OSDDiskIndex‘ and use the ‘Format and Partition Disk‘ in the Task Sequence to dynamically perform actions

2 thoughts on “Configuration Manager (SCCM) | OSD | Multiple Hard Drives | Basic

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: