View Only
  • 1.  schedule Remove-snapshot

    Posted Jan 04, 2019 07:46 AM

    Hi, I'm New User for PowerCLI,I have a question for schedule Remove-snapshot:

    for example:

    I have been created a snapshot in PowerCLI for my vm:

    $Date = Get-Date -UFormat %Y%m%d

    $timechop = Get-Date -Format g

    get-vm | where {$_.PowerState -eq "PoweredOn"} | where {$_.Name -match "Linux"} | New-Snapshot -Name "$Date" -Description "PowerCLI Weekly Snapshot at $timechop"

    get-vm | where {$_.PowerState -eq "PoweredOn"} | where {$_.Name -match "Linux"} | Get-Snapshot

    Name                 Description                    PowerState

    ----                 -----------                    ----------

    20190104             PowerCLI Weekly Snapshot at... PoweredOn

    Now the question:

    I need to Schedule Remove the Snapshot Before 7 days ago (according the snapshot create day,for example:20190104,autoremove the named"20190104" when the current date is change to 20190111),how to write the scripts?

    thanks a lot!

  • 2.  RE: schedule Remove-snapshot

    Posted Jan 04, 2019 08:03 AM

    When you say schedule, do you mean the Task Scheduler in the vCenter?

  • 3.  RE: schedule Remove-snapshot

    Posted Jan 07, 2019 12:52 AM

    schedule means I want to use windows scheduled task to execute xxx.ps1 file.

  • 4.  RE: schedule Remove-snapshot

    Posted Jan 07, 2019 05:29 AM

    The script to remove the snapshots 7 days or older could be something like this

    $now = Get-Date

    Get-Vm | Get-Snapshot |

    where {(New-TimeSpan -Start [DateTime]::ParseExact($_.Name, 'yyyyMMdd', [Globalization.CultureInfo]::CreateSpecificCulture('en-US')) -End $now).TotalDays -ge 7} |

    Remove-Snapshot -Confirm:$false

    To run this as a Windows scheduled task, see for example How to – run a PowerCLI .ps1 script using the Task Scheduler

    If you need more specifics on the scheduled task (like credentials for the Connect-VIServer) let me know.

  • 5.  RE: schedule Remove-snapshot

    Posted Jan 08, 2019 07:09 AM

    got it.

    Thank you very much.

  • 6.  RE: schedule Remove-snapshot

    Posted Feb 17, 2022 08:48 PM

    Is it possible to schedule within vCenter to remove a snapshot? If so, how would the look in powershell?

    Is there a "New-VMScheduled Snapshot" command but for deleting a snapshots?

  • 7.  RE: schedule Remove-snapshot