Some background: I work in a lab environment and have a number of issues that come through that require VHDs to be created and attached to VMs for stress testing. I have come up with a script that allows the user to make the process as simple as possible, which is the following:
$vms=Get-VM
$val = 0
Write-Host "This script is set up for quickly creating and initilizing VHDs"
$Path = Read-Host "Please enter the path you want to create the drives to. Use the formate in this example <E:\VHDS\>"
$fileName = Read-Host "The Drive will be <target>-<number>.vhdx. Please Name the target "
$vhdSize = 1GB
$vmAmount = Read-Host "How many Drives should be attached to each VM?"
foreach ($vm in $vms)
{
$n = $vm.Name
while ($val -ne $vmAmount)
{
$vhdPath = ($Path + $fileName + '-' + $val + '.vhdx')
New-VHD -Path $vhdPath -SizeBytes $vhdSize -Fixed | Mount-VHD -Passthru | Initialize-Disk -Passthru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -Confirm:$false -Force | Dismount-VHD -Passthru
Add-VMHardDiskDrive -VMName $n -Path $vhdPath
$val++
}
}
When I run the code, it gives me an error stating that Dismount-VHD will not work with the path given. I attempt to go in and give it the $vhdPath variable, and it still blocks out.
Another problem I'm running into is that the while statement is not incrementing $val. When it goes into the next statement, it throws an error and stops, saying that the VM in question already has the disk attached to it.
Any help would be appreciated.
-Passthru
on a number of the commands in the middle of that pipelien? Do theAdd-VMHardDiskDrive
and the increment statements actually run? Does the error on the first line "terminate" that loop? – Lat