####################################################################
# User needs to change $vCenter, $Filer and $Volume before running #
####################################################################
# Add the vSphere PowerCLI SnapIn and the DATA ONTAP Module
Import-module DataONTAP
# Set my variables
$vCenter = "VCENTER HOSTNAME OR ADDRESS"
$Filer = "CONTROLLER HOSTNAME OR ADDRESS"
$Volume = "VOLUME NAME"
$VM = "VM NAME"
# Adds the base cmdlets if needed
$SnapinLoaded = get-pssnapin | Where-Object {$_.name -like "*VMware*"}
If (!$SnapinLoaded) {
Add-PSSnapin VMware.VimAutomation.Core
# Add the following if you want to do things with Update Manager
# Add-PSSnapin VMware.VumAutomation
# This script adds some helper functions and sets the appearance. You can pick and choose parts of this file for a fully custom appearance.
# . "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1"
}
# Connect to vCenter and our NetApp Filer
Connect-VIServer $vCenter
Connect-NaController $Filer
$snap = get-nasnapshot $Volume | Where-Object {$_.name -like "*recent*"}
$snaplistbefore = get-nasnapshot $Volume
if (!$snap) {
Write-Output "There are no snapshots available for restore"
exit}
$PowerState = get-vm $VM
$PowerState = $PowerState.PowerState
Write-Output " " "$VM is $PowerState" " "
if ($PowerState -like "PoweredOn") {
stop-vm $VM -confirm:$false
Write-Output "$VM is powered off" " "}
Write-Output "Restoring Snapshot $snap on Volume $Volume" " "
restore-nasnapshotvolume $Volume $snap.name -confirm:$false
$snaplistafter = get-nasnapshot $Volume
$snapcount = $snaplistbefore.length-1
if ($snapcount -lt 0) {
rename-nasnapshot $Volume $snaplistafter.name $snaplistbefore.name}
else {
rename-nasnapshot $Volume $snaplistafter[$snapcount].name $snaplistbefore[$snapcount].name}
Write-Output "Restore complete, power on VM in vCenter Client"