VMware Cloud Community
rneloms
Contributor
Contributor
Jump to solution

Help figuring out flaw in a Snapshot removal script using an input file

I would like to create a script that will make use of an input file that has the virtual machine names containing snapshots with a common name. I used some of the logic in the script to create the snapshots and I am trying to reverse engineer to delete them now. I have attached the delete script, the input file, and the create script. The create script works fines using the input file but I know my logic in the delete is flawed. Please let me know if you can see were the corrections is needed

Tags (3)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You have to replace that line with the line I showed in the code snippet.

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

The loop where you get the snapshots first needs a VirtualMachineImpl object as input.

That's why I added the Get-VM to line where you initialise the $vm array.

...
# This is the input file for  snapshot creation script
$list = Get-Content C:\temp\vms.txt 

$vm = $list | Get-VM | Sort-Object 
$vm | Get-Snapshot | Where { $_.Description -like "Patch"} | Remove-Snapshot -Confirm:$false | Out-Null
...

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
rneloms
Contributor
Contributor
Jump to solution

I tried that and I get the below.

"System.Management.Automation.ScriptBlock".

At :line:32 char:20

+ $vm | ForEach-Object <<<< Get-Snapshot | Where { $_.Description -like "Patch"}|Remove-Snapshot -Snapshot $_ -Confirm:$false | Out-Null

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to replace that line with the line I showed in the code snippet.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
rneloms
Contributor
Contributor
Jump to solution

Here is what the code snippet looks like for me now:

$vCenter = connect-viserver ATL4VCS01

$Date = Get-Date -Format "yyyy-MM-dd-HHmm"

  1. This is the report file

$filelocation = "C:\temp\SnapsDeleted-$Date.csv"

  1. This is the input file for snapshot creation script

$list = Get-Content C:\temp\vms.txt

$vm = $list | Get-VM | Sort-Object

$vm | Get-Snapshot | Where { $_.Description -like "Patch"} | Remove-Snapshot -Confirm:$false | Out-Null

So at this point I have replaced the line and this is the complete error I get when running it in debug:

Cannot process argument transformation on parameter 'Datastore'. Strings as pipeline input are not supported.

At :line:31 char:20

+ $vm = $list | Get-VM <<<< | Sort-Object

Cannot process argument transformation on parameter 'Datastore'. Strings as pipeline input are not supported.

At :line:31 char:20

+ $vm = $list | Get-VM <<<< | Sort-Object

Value cannot be found for the mandatory parameter VM

At :line:32 char:18

+ $vm | Get-Snapshot <<<< | Where { $_.Description -like "Patch"} | Remove-Snapshot -Confirm:$false | Out-Null

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, I see what went wrong the curly braces dropped off during my copy/paste.

I attach the updated script

____________

Blog: LucD notes

Twitter: lucd22


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

rneloms
Contributor
Contributor
Jump to solution

Smiley Happy Be patient with me. It is now reading the input file and it is also creating the report but it is not doing the most important part, removing the snapshot. I checked to make sure the description field does have patch which it does. The contents of the description field is"Patch Management 2010-04-28-0939". Am I using the wrong command for Removing the snapshot?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to provide some meta characters with the -like operator when you're not looking for an exact match.

Try it like this (replace the line)

...
$vm | Get-Snapshot | Where { $_.Description -like "Patch*"} | Remove-Snapshot -Confirm:$false | Out-Null
...

Now you saying that you are looking for text that starts with "Patch" and that you don't care what comes after that string (the asterisk after "Patch").

____________

Blog: LucD notes

Twitter: lucd22


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

rneloms
Contributor
Contributor
Jump to solution

You are the Man!!!!. That Fixed it by using the wild card.

Reply
0 Kudos
Matt_B1
Enthusiast
Enthusiast
Jump to solution

This type of script is exactly what I am looking for. I modified the script based on the edits by LucD . I ran it against one VM and it populated the .csv file correctly. However, the screenshot was never deleted. I get the following warning (see attached) but I don't think that should prevent it from running. I am going to use VGC, http://labs.vmware.com/flings/vgc, since it packages other features and uses a GUI. I am hoping to have this script available to use if needed though.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

VGC is a good tool, although it is still in preview.

The message you're seeing is a known problem.

See for example

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
rneloms
Contributor
Contributor
Jump to solution

That will not prevent the script from working but just in case here is my end result script as well. That I am runing successfully.

Reply
0 Kudos
Matt_B1
Enthusiast
Enthusiast
Jump to solution

It looks identical to my script but your version worked just fine several times. I did manually update VIX, http://www.vmware.com/support/developer/vix-api/, which should be unrelated but I might as well note that if anyone else still has an issue with your updated script. Thanks again rneloms and LucD.

Reply
0 Kudos