Hyper-V Powershell 检查点 creation/deletion 不同步。有更好的选择吗?

Hyper-V Powershell checkpoint creation/deletion is not synchronous. Is there a better option?

我发现自己必须围绕 powershell Remove-VMSnapshot and Checkpoint-VM 编写包装器。文档没有提及它,但基于下面执行的两个代码片段中的 Write-Host,在 MS 提供 cmdlet 之后,检查点未完全 deleted/created。我在创建它后立即尝试按名称恢复到检查点时遇到了这个错误。

有没有人遇到过这个?关于更好的处理方法的想法?防止我的任何代码直接调用 MS cmdlet 的技巧?

function Remove-VMSnapshots-Sync
{
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True)][object]$VM,
    [Parameter(Mandatory=$True)][string]$CheckpointName,
    )
    $matchingSnapshots = @(Get-VMSnapshot $VM | Where-Object {$_.Name -eq $CheckpointName})
    $matchingSnapshots | Remove-VMSnapshot
    do
    {
        $matchingSnapshots = @(Get-VMSnapshot $VM | Where-Object {$_.Name -eq $CheckpointName})
        $stillThere = $matchingSnapshots.length -gt 0
        if ($stillThere)
        {
            Write-Host 'sleeping to try to let snapshot disappear'
            start-sleep -s 1
        }
    } while ($stillThere)
}

function Checkpoint-VM-Sync
{
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True)][object]$VM,
    [Parameter(Mandatory=$True)][string]$CheckpointName
    )
    $checkpoint = Checkpoint-VM -VM $VM -SnapshotName $CheckpointName -PassThru
    $checkpoint | Write-Host
    while (-not (@(Get-VMSnapshot $VM | Select -ExpandProperty Id)).Contains($checkpoint.Id))
    {
        Write-Host 'waiting for checkpoint to be in list'
        Get-VMSnapshot $VM | Write-Host
        start-sleep -s 1
    }
}

有类似的问题,请参阅 中的答案,它向您展示了覆盖命令是多么容易。

您需要将其添加到您的个人资料中(仅供您使用),或将其添加到脚本中(对于每个运行脚本的人),这取决于您的情况。