2

I am trying to get a snapshot from multiple servers with PowerCLI.

Connect-VIServer -server 192.168.0.1 -user acconut -password xxx

$vmlist = Get-Content C:\Users\Desktop\Test\Servers.txt

foreach($VM in $VMlist) {
    New-Snapshot -VM $vm -Name Temp-SnapShot -description (get-date),'Created for patching'
}

Disconnect-VIServer -Confirm:$false    

If I delete get-date, the script will work. But I need to type date in descriptions. How should I change the Script above to have Get-Date in snapshot's descriptions?

Also, I need to delete these snapshot after a couple days:

Connect-VIServer -server 192.168.0.1 -user acconut -password xxx

$vmlist = Get-Content C:\Users\Desktop\Test\Servers.txt

foreach($VM in $VMlist) {
    Remove-Snapshot -VM $vm -snapshot -confirm:$false
}

Disconnect-VIServer -Confirm:$false  

I could not delete snapshot with Remove-Snapshot because I get this error:

Remove-Snapshot : Missing an argument for parameter 'Snapshot'. Specify a parameter of type 'VMware.VimAutomation.ViCore.Types.V1.VM.Snapshot[]' and try again.

Thank you for your help.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
rozbeh85
  • 37
  • 2
  • 3
  • 7
  • For your first problem, my guess is that `-Description` only takes a string. I can show you how to fix that when the question reopens. For the second part, do you get an error when you try that script? – JasonMArcher Aug 08 '14 at 18:17
  • Yes,, I am getting Remove-Snapshot : Missing an argument for parameter 'Snapshot'. Specify a parameter of type 'VMware.VimAutomation.ViCore.Types.V1.VM.Snapshot[]' and try again. – rozbeh85 Aug 08 '14 at 18:26
  • Ah, you will need to get the specific snapshot first, then pass that to `Remove-Snapshot`. – JasonMArcher Aug 08 '14 at 18:31
  • Why did you close this question? I was waiting till question reopens. – rozbeh85 Aug 14 '14 at 13:45

2 Answers2

2

On the description part you can put $date = get-date and do -description $date. That should work.

before you can remove the snapshot you need to get the snapshot. I would say edit your remove-snapshot line to include this:

Get-Snapshot -VM $vm | Remove-Snapshot -confirm:$false

you might even want to add -RemoveChildren:$true ( this will remove "All" snapshots )

ChargerIIC
  • 1,620
  • 1
  • 33
  • 47
Rich
  • 21
  • 2
  • I also have a script to take snapshots using a list of vms in a .txt file. Not sure how to put the whole script here for anyone so if you need access to the script [link](http://justawinadmin.blogspot.com/2014/12/power-cli-script-to-take-snapshots-from.html) – Rich Dec 08 '14 at 18:55
0

This should help with what you are looking for. It finds the snaps and removes any greater than 10 days old.

$snaps = Get-VM | Get-snapshot | Select vm,name,created

if ($snaps.created -le (get-date).adddays(-10))
{
  $remsnaps = $snaps | Where {$_.created -le (get-date).adddays(-10)}
  $remsnaps | Out-File "C:\Scripts\RemoveSnaps\logs\remsnapsVC.txt"
  $remobject = Get-Content "C:\Scripts\RemoveSnaps\logs\remsnapsVC.txt"

  $rmsnap = Get-Snapshot $remsnaps.vm
  Remove-Snapshot -Snapshot $rmsnap -RemoveChildren -Confirm:$false #-WhatIf


 $MessageSubject = "The following snaps were removed from NTVCenter01!"
 $MessageBody = $remobject | fl | out-string

 SendEmail

}