Add and Remove Snapshots with PowerCLI for multiple servers

10,183

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 )

Share:
10,183
rozbeh85
Author by

rozbeh85

Updated on June 18, 2022

Comments

  • rozbeh85
    rozbeh85 almost 2 years

    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.

  • Admin
    Admin over 9 years
    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