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
}
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
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
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
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
ah yes, of course, thank you a lot 🙂
It works!