Add and Remove Snapshots with PowerCLI for multiple servers

rozbeh85 picture rozbeh85 · Aug 8, 2014 · Viewed 9.9k times · Source

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.

Answer

Rich picture Rich · Dec 8, 2014

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 )