Hyper-V Powershell检查点创建/删除不同步。有更好的选择吗?

时间:2017-07-24 21:18:54

标签: powershell hyper-v

我发现自己不得不围绕powershell Remove-VMSnapshotCheckpoint-VM撰写包装。文档没有提及它,但基于执行中的两个代码片段中的写入主机,在MS提供的cmdlet之后,检查点未完全删除/创建。我试图在创建它后立即按名称恢复到检查点时出现错误。

还有其他人遇到过这个吗?关于更好的处理方法的想法?诀窍阻止我的任何代码直接调用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
    }
}

1 个答案:

答案 0 :(得分:1)

有类似问题,请参阅Can I override a Powershell native cmdlet ...中的答案,它会告诉您覆盖命令的容易程度。

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