Powershell Script to Poweroff a VM and perform a Volume Snaprestore of the datastore

by Frequent Contributor on ‎2011-06-09 05:00 PM

####################################################################

# 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"

Warning!

This NetApp Community is public and open website that is indexed by search engines such as Google. Participation in the NetApp Community is voluntary. All content posted on the NetApp Community is publicly viewable and available. This includes the rich text editor which is not encrypted for https.

In accordance to our Code of Conduct and Community Terms of Use DO NOT post or attach the following:

  • Software files (compressed or uncompressed)
  • Files that require an End User License Agreement (EULA)
  • Confidential information
  • Personal data you do not want publicly available
  • Another’s personally identifiable information
  • Copyrighted materials without the permission of the copyright owner

Files and content that do not abide by the Community Terms of Use or Code of Conduct will be removed. Continued non-compliance may result in NetApp Community account restrictions or termination.