Creating & Adding Lots Of Shared VHDX Files To VMs Using PowerShell

As previously mentioned, I want to run a virtual SOFS in my lab using Storage Spaces.  This is completely unsupported, but it’s fine for my lab.  In the real world, the SOFS with Storage Spaces features physical servers with a JBOD.

I’m lazy, and I don’t want to be creating VHDX files, adding disks by hand, configuring sharing, and repeating for each and every VHDX on each and every VM.

First up: creating the VHDX files.  This snippet will create a 1 GB witness and 8 * 100 GB data disks:

$Folder = “E:Shared VHDXDemo-FSC1″

New-VHD -Path “$FolderDemo-FSC1 Witness Disk.vhdx” -Dynamic -SizeBytes 1GB

for ($DiskNumber = 2; $DiskNumber -lt 9; $DiskNumber++)
{
New-VHD -Path “$FolderDemo-FSC1 Data Disk$DiskNumber.vhdx” -Dynamic -SizeBytes 100GB
}

The next snippet will (a) attach the VHDX files to the default SCSI controller of the VMs, and (b) enable Shared VHDX (Persistent Reservations):

$VMName = “Demo-FS1.demo-internal”

Add-VMHardDiskDrive $VMName -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 0 -Path “E:Shared VHDXDemo-FSC1Demo-FSC1 Witness Disk.vhdx” -SupportPersistentReservations

for ($DiskNumber = 1; $DiskNumber -lt 9; $DiskNumber++)
{
Add-VMHardDiskDrive $VMName -ControllerType SCSI -ControllerNumber 0 -ControllerLocation $DiskNumber -Path “E:Shared VHDXDemo-FSC1Demo-FSC1 Data Disk$DiskNumber.vhdx” -SupportPersistentReservations
}

And there’s the power of PowerShell.  In just a few minutes, I figured this out, and saved myself some boredom, and then sped up my job. With a bit more crafting, I could script a hell of a lot more in this:

  • Creation of VMs
  • Copying and attaching OS disks
  • The above
  • Booting the VMs
  • Use functions to group code and an array of user entered VM names

Note: You might want to consider adding more SCSI controllers to the VMs, especially if you have plenty of vCPUs to spare.  And yes, I did use Dynamic VHDX because this is a lab.

Please follow and like us:

1 Comment on Creating & Adding Lots Of Shared VHDX Files To VMs Using PowerShell

  1. for ($DiskNumber = 2; $DiskNumber -lt 9; $DiskNumber++)
    {
    New-VHD -Path “$FolderDemo-FSC1 Data Disk$DiskNumber.vhdx” -Dynamic -SizeBytes 100GB
    }

    Since the number of disks is hardcoded, the above code could be condensed down to:

    2..8 | foreach {New-VHD -Path “$FolderDemo-FSC1 Data Disk$_” -Dynamic -SizeBytes 100GB}

Leave a comment

Your email address will not be published.

*