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

问题描述:

我发现自己不得不围绕Powershell的Remove-VMSnapshotCheckpoint-VM编写包装。文档没有提到它,但是基于在执行下面的两个代码片段中的Write-Host,在MS提供cmdlet之后,检查点没有被完全删除/创建。在创建错误后立即尝试通过名称恢复到检查点时,我尝试了这种方法。Hyper-V Powershell检查点创建/删除不同步。有更好的选择吗?

有没有其他人遇到过这个?有更好的方法来处理它的想法?诀窍,以防止我的代码直接调用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 
    } 
} 

也有类似的问题,请参见Can I override a Powershell native cmdlet ...它表明你是多么容易重写命令答案。

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

+0

太棒了,这会阻止我调用不安全的异步版本。 :-) – aggieNick02