Search code examples
powershelldsc

Initialize variable amount of disks with a single DSC configuration


Is it possible to initialize all attached disks to multiple VMs with a single DSC configuration? For example if VM1 has 1 disk attached that DSC configuration would initialize that disk as disk F, VM2 has 2 disks, so the very same DSC configuration would attack disks as F and G. The idea is to reuse that configuration file for multiple VMs with a variable amount of disks without getting errors.


Solution

  • This should work if you are compiling locally. Since the language allows imperatively building the declared state. You can query the disks and set the state.

    The assignment of drive letters in my sample is rather crude. You should improve it as well.

    This uses xStorage which can be found on the PowerShell Gallery

    Configuration disks
    {
      $DriveLetters = 'DEFGHIJKLMNOPQSRT'
      Import-DscResource -ModuleName xStorage
    
      Node localhost
      {
        Get-Disk | Where-Object {$_.NumberOfPartitions -lt 1} | Foreach-Object {
          Write-Verbose "disk($($_.Number))" -Verbose
          xDisk "disk($($_.Number))"
          {
            DriveLetter = $DriveLetters[$_.Number]
            DiskNumber = $_.Number
            FSFormat = 'NTFS'        
          }
        }
      }
    }