Skip to content

Instantly share code, notes, and snippets.

@kiwi-cam
Created July 29, 2018 23:35
Show Gist options
  • Save kiwi-cam/4c0b040956ca69c3b7db949274d32a5e to your computer and use it in GitHub Desktop.
Save kiwi-cam/4c0b040956ca69c3b7db949274d32a5e to your computer and use it in GitHub Desktop.
Scans for RAW drives attached to the computer and formats them. Drive Letters are assigned sequentially from D:\ on, except on computers with SQL in the Computer name where Drive 2 will become L drive for Logs.
<#
.Synopsis
Scans for RAW drives attached to the computer and formats them.
.DESCRIPTION
Drive Letters are assigned sequentially from D:\ on, except on computers with SQL in the Computername where
Drive 2 will become L drive for Logs. Subsequent drives are lettered sequentially.
.PARAMETER UnitSize
An Int specifying the AllocationUnitSize used when formatting
.PARAMETER FileSystem
Specifies the file system with which to format the volume. The acceptable values for this parameter are:NTFS,
ReFS, exFAT, FAT32, and FAT.
.PARAMETER SQLServer
Forces drive configuration for SQL servers, otherwise Auto-detected based on server names. I.e. Drive 2 is L for LOGS
.PARAMETER MinSize
An Int specifying the smallest volume to be considered an additional drive - to prevent false detections.
.PARAMETER ShowCommands
If set, the script will Output the commands being run. Useful for documentation
.EXAMPLE
./Initalize-RAWDisks.ps1 -UnitSize 8192 -SQLServer
.NOTES
Version: 1.0
Author: Cameron McConnochie
Creation Date: 30 July 2018
Purpose/Change: Initial script development
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[int32]$UnitSize = 4096,
[Parameter(Mandatory=$False)]
[ValidateSet('NTFS','ReFS', 'exFAT', 'FAT32', 'FAT')]
[string]$FileSystem = "NTFS",
[Parameter(Mandatory=$False)]
[int32]$MinSize = 20,
[Parameter(Mandatory=$False)]
[switch]$SQLServer = $false,
[Parameter(Mandatory=$False)]
[switch]$ShowCommands = $false
)
$RAWDisks = Get-Disk | Where-Object {$_.partitionstyle -eq 'raw' -and $_.Size -gt "$($MinSize)GB"}
If($RAWDisks.Length -eq 0){
Write-Host "No RAW Disks Found" -ForegroundColor Red
}
ForEach ($Disk in $RAWDisks) {
#Initalise the Disk
Initialize-Disk -Number $Disk.Number -PartitionStyle GPT -PassThru
If($ShowCommands){Write-Host "Initialize-Disk -Number $($Disk.Number) -PartitionStyle GPT -PassThru" -ForegroundColor Yellow}
#Find Drive Letter (Start at D), If Server name includes SQL, set next drive as L for Logs
[string]$DriveLabel = ""
[char]$DriveLetter = "D"
If ($Disk.Number -eq 1) {
$DriveLetter = "D"
$DriveLabel = "DATA"
}elseif ($env:computername -match "SQL" -or $SQLServer) {
#It's a SQL server, setup L drive then continue
If ($Disk.Number -eq 2) {
$DriveLetter = "L"
$DriveLabel = "LOGS"
} else {
#Character should be E (ACSII 69) onwards, from disk 3
$DriveLetter = [char](($Disk.Number - 3) + 69)
}
}else{
#Character should be E (ACSII 69) onwards, from disk 2
$DriveLetter = [CHAR](($Disk.Number - 2) + 69)
}
#Confirm the Drive Letter is free
While(Test-Path "$($DriveLetter):\"){
Write-Host "Drive $($DriveLetter) is already in use, incrementing" -ForegroundColor Red
$DriveLetter = [CHAR]([INT32]$DriveLetter + 1)
}
#Create Volume
Write-Host "Formatting disk $($Disk.Number) as $($DriveLetter):\ labelled $($DriveLabel)"
New-Partition -DiskNumber $Disk.Number -UseMaximumSize -DriveLetter $DriveLetter | Format-Volume -FileSystem $FileSystem -AllocationUnitSize $UnitSize -NewFileSystemLabel $DriveLabel
If($ShowCommands){Write-Host "New-Partition -DiskNumber $($Disk.Number) -UseMaximumSize -DriveLetter $($DriveLetter) | Format-Volume -FileSystem $($FileSystem) -AllocationUnitSize $($UnitSize) -NewFileSystemLabel $($DriveLabel)" -ForegroundColor Yellow}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment