Formatting additional drives

he need for this script came along when I noticed that I was creating disks for one specific need with the specific cache policy and didn’t know for sure which disk was the right disk to format it properly. That was the moment that I figure: “hmmm I need a script for that…”Normally, if you ever have the need of additional disks, most likely that you’ll do it right and will select the proper cache policy accordingly to the disk’s usage during the disk creation.

If you need to understand better the cache policy, please refer to this link.

So, to facilitate the process of formatting and assigning drive letter for them, I’ve created a small script that will do the job for you. Enjoy 😊

#Reference:
#https://msdn.microsoft.com/en-us/library/windows/desktop/hh830493(v=vs.85).aspx

$DISK01 = @{
	'DriveLetter' = 'F'
	'FileSystem' = 'NTFS'
	'DriveLabel' = 'DATA'
}
$DISK02 = @{
	'DriveLetter' = 'I'
	'FileSystem' = 'NTFS'
	'DriveLabel' = 'INDEX'
}
$DISK03 = @{
	'DriveLetter' = 'L'
	'FileSystem' = 'NTFS'
	'DriveLabel' = 'LOG'
}
$DISK04 = @{
	'DriveLetter' = 'T'
	'FileSystem' = 'NTFS'
	'DriveLabel' = 'TEMPDB'
}

#REGEX Used on Path propriety to capture LUN number
[regex]$regex = '\d\d\d\d\d\d'

#BusType 10 = Serial Attached SCSI (SAS)
$ALLDisks = Get-WmiObject -Class MSFT_Disk -Namespace Root\Microsoft\Windows\Storage | Where-Object –FilterScript {($_.BusType -EQ "10")}

Foreach ($Disk in $ALLDisks){
If ($Disk.PartitionStyle -eq "0")
     { Initialize-Disk -Number $Disk.Number -PassThru }
    If ($regex.Match($Disk.Path).Value.EndsWith("1") -eq $true){
		New-Partition -DiskNumber $Disk.Number -DriveLetter $DISK01.DriveLetter -UseMaximumSize | Out-Null
		Format-Volume -DriveLetter $DISK01.DriveLetter -FileSystem $DISK01.FileSystem -NewFileSystemLabel $DISK01.DriveLabel | Out-Null
        }
        Elseif (($regex.Match($Disk.Path).Value).EndsWith("2") -eq $true){
		New-Partition -DiskNumber $Disk.Number -DriveLetter $DISK02.DriveLetter -UseMaximumSize | Out-Null
		Format-Volume -DriveLetter $DISK02.DriveLetter -FileSystem $DISK02.FileSystem -NewFileSystemLabel $DISK02.DriveLabel | Out-Null
		}
	Elseif (($regex.Match($Disk.Path).Value).EndsWith("3") -eq $true){
		New-Partition -DiskNumber $Disk.Number -DriveLetter $DISK03.DriveLetter -UseMaximumSize | Out-Null
		Format-Volume -DriveLetter $DISK03.DriveLetter -FileSystem $DISK03.FileSystem -NewFileSystemLabel $DISK03.DriveLabel | Out-Null
        }
        Elseif (($regex.Match($Disk.Path).Value).EndsWith("4") -eq $true){
		New-Partition -DiskNumber $Disk.Number -DriveLetter $DISK04.DriveLetter -UseMaximumSize | Out-Null
		Format-Volume -DriveLetter $DISK04.DriveLetter -FileSystem $DISK04.FileSystem -NewFileSystemLabel $DISK04.DriveLabel | Out-Null
        }        
}

The output should look like this:

Note1: You’ll see the out put of what was changed, not how the settings are after running the command.
If you want to double check the running configuration, just run:

Get-Disk

As you noticed,  the outputs for New-Partition and Format-Volume are being omitted  with the pipe to the command Out-Null. You can costumize this as you whish.

To make code adjustments easier, I took advantage of the Splatting.
So, if you need to add or remove more disks, it will be a piece of cake.

The $regex.Match($Disk.Path).Value).EndsWith(“X”) is the taking the matching number of your LUN, if you want to add more disks to this script, just follow the logic.
You can check the LUN’s number on the Azure portal.

I hope this helps and happy scripting !