VMware Cloud Community
AndreasOC
Contributor
Contributor
Jump to solution

Restore snapshot of multiple VMs

Hello,

I want to  restore from multiple VMs the last snapshot, can some help me to finish the script?

I wrote this script, but it seems something wrong with the variable I think:

$VMs = "VM1, VM3, VM7"

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer -server vcenter -user #####\###### -Password #######
$vm = Get-VM $VMs
output = $vm | foreach {
	$snap = Get-Snapshot -VM $vm | Sort-Object -Property Created -Descending | Select -First 1
	Set-VM -VM $vm -SnapShot $snap -Confirm:$false
}
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

 

$VMs = 'VM1', 'VM3', 'VM7'

Get-VM $VMs -PipelineVariable vm |
ForEach-Object -Process {
	Get-Snapshot -VM $vm | Sort-Object -Property Created -Descending |
	Select-Object -First 1 |
	Set-VM -VM $vm -Confirm:$false
}

 


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

 

$VMs = 'VM1', 'VM3', 'VM7'

Get-VM $VMs -PipelineVariable vm |
ForEach-Object -Process {
	Get-Snapshot -VM $vm | Sort-Object -Property Created -Descending |
	Select-Object -First 1 |
	Set-VM -VM $vm -Confirm:$false
}

 


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

0 Kudos
AndreasOC
Contributor
Contributor
Jump to solution

I get the same error message:

Get-VM : 30.04.2021 13:42:28    Get-VM          VM with name 'VM1, VM3, VM7' was not found using the specified
filter(s).
In Zeile:1 Zeichen:1
+ Get-VM $VMs -PipelineVariable vm |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, I didn't notice you didn't have an array of strings but 1 single string.
I corrected my code.


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

0 Kudos
AndreasOC
Contributor
Contributor
Jump to solution

ah yes, of course, thank you a lot 🙂

It works!

0 Kudos