VMware Cloud Community
YanSi
Enthusiast
Enthusiast

How to restore a snapshot of a virtual machine with the same name using moref-ID

I have a virtual machine with the same name, but it belongs to a different vApp.
I need to perform snapshot recovery on both virtual machines at the same time.
Although I can use this command method, how can I solve the problem of error reporting?

The specified snapshot does not belong to the specified VM.
Get-View -ViewType VirtualMachine -Filter @{"Snapshot" = ""} -Property Name | % {Get-VM -id $_.MoRef | Get-Snapshot | Sort-Object -Property Created -Descending | Select-Object -First 1 | Set-VM 'test*' -Confirm:$false -RunAsync}

微信截图_20220105123443.png

0 Kudos
2 Replies
LucD
Leadership
Leadership

I'm not sure what you are trying to do.
With Get-View you select VMs that do not have a snapshot, and then you want to restore those VMs to a Snapshot.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
YanSi
Enthusiast
Enthusiast

My friend provided a way to solve the problem of batch snapshot recovery.

For ($i = 1; $i -le 2; $i++) {
    $vAppName=("vApp-" + "{0:d2}" -f $i)
    Write-Host "Revert a Snapshot for" $vAppName"..."
    $VMs = Get-vApp $vAppName | Get-VM
    ForEach( $vm in $VMs ) { Set-VM -VM $vm -Snapshot '20220105' -Confirm:$false }
    # ForEach( $vm in $VMs ) { Start-VM $vm }
    # Start-VApp $vAppName
}

Hope to be useful to others.....  🙂

 

0 Kudos