PowerShell DSC - Configure Disk Volume using Script resource

If you are building Windows Based application stack using automation either on Cloud VM ( on of any cloud vendor i.e. Google, Azure or AWS) or even on premise, first thing you need to configure disk volume. You will get disk in raw format and need to configure them before doing application configuration.

You may use CloudFormation on AWS, ARM on Azure, Terraform (support all major cloud platform) to build VM. Post that you need to run code (remote powershell code execution or DSC) to configure disk.

In my case, I used DSC throughout for any server/application configuration post OS build.

Below is my snippet for DSC script resource to do disk configuration.

What it does @high level?

  1. It looks for RAW disk. If it finds any raw disk, It will go to set step else won't trigger dsc resource
  2. It can be done for n disk. In below snippet, I have taken array of two disk labels, but can be easily change as per the requirement.
  3. I am formatting disk with default allocationunitsize, but can be increased to any value suits to your workload. For example, 64K is the requirement for SQL Server
  4. It looks allocate drive letter dynamically. Post that, add it in environment variable for future reference. This way you don't need to hard code disk volume in your application configuration, rather may fetch the volume drive detail from environment variable. For example, I get F: drive for my data drive. Now I will look for $env:datadrive letter value from environment variable and fetch F: drive info to use in my code.

# Configure Disk volumes

Script SetNewDiskVolumes {

SetScript = {

Write-Verbose -Verbose "Setting New Disk Volumes"

$disks = Get-Disk | Where partitionstyle -eq 'raw' | sort number

$letters = 70..89 | ForEach-Object { [char]$_ }

$count = 0

$labels = "data1","data2"


foreach ($disk in $disks) {

$ConfirmPreference = "None"

$driveLetter = $letters[$count].ToString()

$disk | Initialize-Disk -PartitionStyle GPT

$partition = $disk | New-Partition -UseMaximumSize -AssignDriveLetter:$False

$partition | Format-Volume -FileSystem NTFS -Confirm:$False -force


$drive=Get-WmiObject -Class win32_volume | Where-Object {$_.Driveletter -eq $null}

$drivelet="${driveLetter}:"

Set-WmiInstance -input $drive -Arguments @{DriveLetter=$drivelet;label=$labels[$count]}

Write-Verbose -Verbose $driveLetter

$datadrive=[Environment]::GetEnvironmentVariable("datadrive","Machine")

if ($datadrive -eq $null) {

Write-Verbose -Verbose "Setting environment variable datadrive"

Environment]::SetEnvironmentVariable("datadrive", "${driveLetter}", "Machine")

Write-Verbose -Verbose "Drive Latter ${driveLetter}:"

start-sleep -Seconds 5

$env:datadrive=[Environment]::GetEnvironmentVariable("datadrive","Machine")

$datadrive=[Environment]::GetEnvironmentVariable("datadrive","Machine")

Write-Verbose -Verbose "Set environment variable datadrive as ${datadrive}"

}

$count++

}

Write-Verbose -Verbose "Disk Configured $datadrive"

Write-Verbose -Verbose "Waiting for disk to come online"

while ((test-path "${datadrive}:\") -ne $false) {

Write-Verbose -Verbose "Waiting for $datadrive to come online"

start-sleep 10

}

}

GetScript = {

$disks = Get-Disk | Where-Object partitionstyle -eq 'raw' | sort number

#$CurrentPort = $Matches.Groups[1].Value

Return @{

'Diskcount' = $disks.count

}

}


TestScript = {

$disks = Get-Disk | Where-Object partitionstyle -eq 'raw' | sort number

If (($disks.Number).count -eq 0) {

# Raw disks are there and need to configure

Return $True

}

# Disk already configured

Return $False

}

}       



To view or add a comment, sign in

Others also viewed

Explore content categories