[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 890
  • Last Modified:

Powershell script to reverse Snapshots

Hello

Is there any way to use a PowerShell script to reserve a snapshot.

We need to do some tests on a VM during the night, and if the tests fail, we want to run a script to reserve a snapshot.

The snapshot name will always be the same, I don’t want to add vCenter credentials in the script as plain text.

I have found same examples on the internet, but there not what I need.

Please don’t provide just links.

Is this possible?

Thank You

JL
0
ServerHome
Asked:
ServerHome
  • 2
  • 2
2 Solutions
 
Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
You mean LOCK a snapshot?

or reverse - roll back or reserve (keep)?

e.g. here a revert script

$snapshot = Get-Snapshot –VM myVM –Name ”Target-Snapshot”
Set-VM –VM myVM –Snapshot $snapshot
0
 
Seaton007Commented:
I will assume you mean "revert" to a snapshot, and yes, this is possible.  Here is a script to do just that:
###################################################################
#
# RevertToSnapshot.ps1
# Version 1.0
# B. Lawson
# www.AdminNation.com
####################################################################

### Global Settings ###
get-viserver -server #Add VMWare server
$vmname = "" #Add virtual machine
$snapshotsname = ""#Add Snapshot

### Power Functions ###
function PowerOff-VM{
param([string] $vm)
if((Get-VM $vm).powerstate -eq ?PoweredOff?){
Write-Host ?$vm is already powered off?}
else{
Shutdown-VMGuest -VM (Get-VM $vm) -Confirm:$false | Out-Null
Write-Host ?Shutdown $vm?
do {
$status = (get-VM $vm).PowerState
}until($status -eq ?PoweredOff?)
}
}

function PowerOn-VM{
param( [string] $vm)
if((Get-VM $vm).powerstate -eq ?PoweredOn?){
Write-Host ?$vm is already powered on?}
else{
Start-VM -VM (Get-VM $vm) -Confirm:$false | Out-Null
Write-Host ?Starting $vm?
do {
$status = (Get-vm $vm | Get-View).Guest.ToolsRunningStatus
}until($status -eq ?guestToolsRunning?)
}
}

### Main script ###
$poweroff = PowerOff-VM $vmname

# Set Snapshot name
$snapname = Get-Snapshot -VM (Get-VM -Name $vmname) -Name $snapshotsname

# Set VM to snapshot
Set-VM -VM $vmname -Snapshot $snapname -confirm:$false

$poweron = PowerOn-VM $vmname

Open in new window

For more information and further instructions regarding this script and it's use, please see: http://www.adminnation.com/2011/07/09/revert-vmware-snapshot-to-original-state-with-powershell
0
 
ServerHomeAuthor Commented:
Hello

Thank You for your reply.

@hanccocka: I mean reserve the VM to a snapshot

@Seaton007: I found that one also, but that one don’t explain how to send credentials to vCenter(or host) without adding them on the script.

Thank You

JL
0
 
ServerHomeAuthor Commented:
I did not get a 100% answered question, but I will close this.
0
 
Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
You cannot RESERVE (protect) LOCK a snapshot, you would need to copy the VM with the Snapshot.
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now