I was recently adding a number of hard disks to a virtual machine on an number of SCSI controller devices, so rather than using the vSphere Web Client I looked at using the New-HardDisk cmdlet in order to repeat this task of adding a new virtual machine hard disk .
I wanted to create the virtual machine hard disks on a single datastore with the below characteristics:
- Capacity: 30GB
- Storage Format: EagerZeroedThick
- SCSI Controller: SCSI Controller 1
I could achieve this for a single hard disk by invoking the New-HardDisk cmdlet as below:
New-HardDisk -VM VM1 -CapacityGB 30 -Datastore "Datastore1" -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1"
However, as I was required to this multiple times, I leveraged the use of pure math with the ForEach loop to achieve this task to create the six virtual hard disks.
ForEach ($HardDisk in (1..6)) { New-HardDisk -VM VM1 -CapacityGB 30 -Datastore Datastore1 -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1" }
Furthermore, I was adding additional hard disks on a number of SCSI controllers, I could expand the above further to invoke multiple ForEach loops for each SCSI controller, specifying a different datastore and size for each.
ForEach ($HardDisk in (1..6)) { New-HardDisk -VM VM1 -CapacityGB 30 -Datastore Datastore1 -StorageFormat EagerZeroedThick -Controller "SCSI Controller 1" } ForEach ($HardDisk in (1..2)) { New-HardDisk -VM VM1 -CapacityGB 10 -Datastore Datastore2 -StorageFormat EagerZeroedThick -Controller "SCSI Controller 2" } ForEach ($HardDisk in (1..3)) { New-HardDisk -VM VM1 -CapacityGB 20 -Datastore Datastore3 -StorageFormat EagerZeroedThick -Controller "SCSI Controller 3" }