【问题标题】:How to delete a specific snapshot using Powershell in Hyper-V如何在 Hyper-V 中使用 Powershell 删除特定快照
【发布时间】:2020-07-22 19:03:34
【问题描述】:

亲爱的,

我正在尝试删除我们其中一个虚拟机的特定快照,但正在删除所有快照。

PS C:\Users\abood>  Get-VMSnapshot -VMName KUW-HV01

VMName   Name SnapshotType CreationTime         ParentSnapshotName
------   ---- ------------ ------------         ------------------
KUW-HV01 OLD  Standard     7/22/2020 9:17:48 PM                   
KUW-HV01 NEW  Standard     7/22/2020 9:18:08 PM OLD  

PS C:\Users\abood>  Remove-VMSnapshot -VMName KUW-HV01 -WhatIf | Where-Object {$_.Name -eq "NEW"}
What if: Remove-VMSnapshot will remove snapshot "NEW".
What if: Remove-VMSnapshot will remove snapshot "OLD".

如何只删除“NEW”或“OLD”而保留另一个?

提前致谢,

【问题讨论】:

    标签: powershell snapshot hyper-v checkpoint


    【解决方案1】:

    大多数对某些对象(例如Remove-VMSnapshot)采取操作的 cmdlet 将允许您将对象通过管道传递到它们中,以指定要对哪些对象执行该操作。例如,您已经使用Get-VMSnapshot 来获取该特定 VM 的两个快照。然后,您可以使用Where-Object 仅指定要删除的快照并过滤掉您要保留的任何快照,如下所示:

    Get-VMSnapshot -VMName KUW-HV01 | Where-Object {$_.Name -eq "NEW"}
    
    VMName   Name SnapshotType CreationTime         ParentSnapshotName
    ------   ---- ------------ ------------         ------------------
    KUW-HV01 NEW  Standard     7/22/2020 9:18:08 PM OLD  
    

    然后将其通过管道传递给 Remove-VMSnapshot 以准确指定要删除的内容。

    Get-VMSnapshot -VMName KUW-HV01 | Where-Object {$_.Name -eq "NEW"} | Remove-VMSnapshot -WhatIf
    

    这应该会导致:

    What if: Remove-VMSnapshot will remove snapshot "NEW".
    

    【讨论】:

    • 谢谢哥们,它成功了,但为什么“Remove-VMSnapshot -VMName KUW-HV01 -WhatIf | Where-Object {$_.Name -eq "NEW"}" 未能按预期工作?这是一个错误还是什么?
    • 不,根本不是错误。该命令告诉它删除快照(所有快照,因为您没有为Remove-VMSnapshot cmdlet 指定哪一个),然后您将输出发送到Where-Object。老实说,这一切都可以简化为Remove-VMSnapshot -VMName KUW-HV01 -Name NEW,但我想向您展示Where 是如何工作的,以及它如何只影响更进一步的事情,而不是之前的事情。
    • 好吧,我明白了。最后一个问题,我可以在 Remove-VMSnapshot -VMName KUW-HV01 -Name NEW 中使用通配符命名吗?
    • 根据documentation可以。他们在他们给出的例子中做到了。
    • 谢谢哥们。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-08-09
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多