pamiller21
Enthusiast
Enthusiast

Remove a snapshot from several VMs

Hey all,

I got a snapshot setup to be created automatically within the vcenter once a month and need a script to remove the snapshot 4 days later. I am testing this script on just one snap to start but will need to expand it.  Also at the end I wanted it to email me just to confirm things were done.

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

# Connect to vCenter        #

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

add-pssnapin VMware.VimAutomation.Core

Connect-VIServer 127.0.0.1 -Protocol https -User <USER> -Password <PW>

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

#          Content          #

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

Get-VM -Name <VM> | Get-Snapshot | Remove-Snapshot

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

# Mail variables #

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

$enablemail="yes"

$smtpServer = "<SERVER>"

$mailfrom = "<ADDY>"

$mailto = "<ADDY>"

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

# E-mail HTML output #

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

if ($enablemail -match "yes")

{

$msg = new-object Net.Mail.MailMessage

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = $mailfrom

$msg.To.Add($mailto)

$msg.Subject = “Monthly Snapshots Removed”

$msg.Body = “Snapshots are removed. Have a good day!”

$smtp.Send($msg)

}

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

# Disconnect session from VC #

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

disconnect-viserver -confirm:$false

0 Kudos
11 Replies
LucD
Leadership
Leadership

Not sure what the actual question is here?

If you want to remove snapshots from multiple VMs, you will just need to adapt the line "Get-VM -Name <VM> | Get-Snapshot | Remove-Snapshot", more specifically the Get-VM cmdlet, so that it returns all the VMs you want to target.

Btw, you can also use the Send-MailMessage cmdlet to send an email.


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

0 Kudos
Mahiee
Enthusiast
Enthusiast

LucD, Can you please help me with the script which create snapshot on multiple servers and delete.

I would like to run the script when there is a requriement on creation/deletion. Thanks in Advance.

0 Kudos
pamiller21
Enthusiast
Enthusiast

What I was looking for:

1) My script does not work

2) If at all possible define the name of the snapshot to be deleted.

Thank you for your help!

0 Kudos
LucD
Leadership
Leadership

On 1), do you get any error messages?


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

No just kept running in scheduled tasks.

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast

First of all, your script points to: 127.0.0.1 - so unless you are running the script on your vCenter server itself, thats not going to work.

Lastly, before you put a PowerShell script into a scheduled task, you need to test it first. Then test it separately in the scheduled task area. From what I remember, to automate a PS1 task, you have to define the full powershell.exe path and then add the respective switches to execute your script.

The script should work, but as LucD rightly said, you need to remove the -Name parameter because it will expect a specific individual server rather than the entire virtual farm.

Other than that, the script looks fine.

Personally I havent done this so im not sure what it will do, but when I run a command I often have the entire action defined as a variable so that when you try to report on the outcome, you have something to echo to the screen or in this case an email as to its success code Smiley Happy

0 Kudos
LucD
Leadership
Leadership

If you run this as a scheduled task, you should remove all interactive elements (prompts).

Add the -Confirm:$false parameter on the Remove-Snapshot cmdlet.


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

1 - This is running from the vCenter

2 - the powershell path is in the scheduled task

3 - the name parameter is needed as I will be only deleting the snaps from specific VMs

0 Kudos
pamiller21
Enthusiast
Enthusiast

Looks like that might be the missing piece, I will be testing this tonight.

Thank you!

0 Kudos
pamiller21
Enthusiast
Enthusiast

This addition worked like a charm, but now I am just confirming that when I want to add additional VMs to the list it will look like this:

Get-VM -Name <VM>,<VM>,<VM> | Get-Snapshot | Remove-Snapshot -Confirm:$false

Is that correct?

I would love to test this but I don't have the chance to with the setup we have on this.

0 Kudos
Zsoldier
Expert
Expert

I suppose you could do that.  You can also use wildcards.

Get-VM -Name (<VM>,<VM>,<VM>) | Get-Snapshot | Remove-Snapshot -Confirm:$false


Wildcard example Gathers all VM's that start w/ MyVM in their name:

Get-VM -Name MyVM* | Get-Snapshot | Remove-Snapshot -Confirm:$false

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos