Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to create. initialize, and format VHD disks

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.

like image 512
Gavin_Talyn Avatar asked Apr 24 '15 00:04

Gavin_Talyn


People also ask

How do I create a VHD image?

To create a VHDOn the Action menu, select Create VHD. In the Create and Attach Virtual Hard Disk dialog box, specify both the location on the physical computer where you want the VHD file to be stored, and the size of the VHD. In Virtual hard disk format, select Dynamically expanding or Fixed size, and then click OK.

What PowerShell command creates a new VHDX?

The New-VHD cmdlet creates one or more new virtual hard disks in either VHD format or the newer VHDX format.


1 Answers

I understand the beauty of pipelines in PowerShell, but this is going a little bit too far :). If you split the large pipeline, things work fine.

    $vhdPath = (Join-path $Path  ($fileName + '-' + $val + '.vhdx'))
    New-VHD -Path $vhdPath -SizeBytes $vhdSize -Fixed 
    Mount-VHD -Path $vhdPath
    $disk = get-vhd -path $vhdPath
    Initialize-Disk $disk.DiskNumber
    $partition = New-Partition -AssignDriveLetter -UseMaximumSize -DiskNumber $disk.DiskNumber
    $volume = Format-Volume -FileSystem NTFS -Confirm:$false -Force -Partition $partition
    Dismount-VHD -Path $vhdPath
    Add-VMHardDiskDrive -VMName $n -Path $vhdPath 

The main problem with what you trying to do, is that Dismount-VHD does not accept pipeline input and even if it did, it would not know what to do with a volume object (this is the output of Format-Volume)

If you want to keep the pipeline, put Dismount-VHD -Path $vhdPath on a separate line and all should be well.

Also when creating paths, you should use join-path to avoid issues.

like image 121
Jan Chrbolka Avatar answered Dec 16 '22 23:12

Jan Chrbolka