尝试创建.初始化和格式化VHD磁盘 [英] Trying to create. initialize, and format VHD disks

查看:312
本文介绍了尝试创建.初始化和格式化VHD磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景:我在实验室环境中工作,遇到许多问题,这些问题需要创建VHD并将其附加到VM进行压力测试.我想出了一个脚本,该脚本使用户可以使过程尽可能简单,如下所示:

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++
    }
}

当我运行代码时,它给了我一个错误,指出Dismount-VHD无法使用给定的路径.我尝试输入$ vhdPath变量,但它仍然被阻止.

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.

我遇到的另一个问题是,while语句不会使$ val递增.当它进入下一条语句时,它将引发错误并停止运行,表示所讨论的VM已经附加了磁盘.

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.

推荐答案

我了解PowerShell中管道的美妙之处,但这有点太过分了:).如果您将大型管道分开,则一切正常.

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 

您尝试执行的操作的主要问题是 Dismount-VHD 不接受管道输入,即使接受,它也不知道如何处理卷对象(这是 Format-Volume )的输出

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)

如果要保留管道,请将Dismount-VHD -Path $vhdPath放在单独的行上,一切都应该很好.

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

此外,在创建路径时,还应使用 join-path 以避免出现问题.

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

这篇关于尝试创建.初始化和格式化VHD磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆