VMware vSphere

 View Only
  • 1.  Auto-delete snapshots via script

    Posted Sep 22, 2015 06:29 PM

    Hi Guys,

    I would like to configure a script in Windows Task Scheduler to automatically delete VM snapshot(s) older than 3 days. I copied the script here (credits to the folks who posted it). Then, I configured the task by following this great post. So I got this script (see attached) and I have these settings on the task that I've created (see attached zip containing the images).

    Program/script: C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe

    Add arguments (optional): -PSConsoleFile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& "C:\auto_delete.psc1""

    Now, I'm not sure what could be wrong as the task seems to be being held up as I have check the running tasks. I even tried running the task manually and the same thing happens. It just created instance that wont finish (see attached). Can you let me know guys what I could be missing here to get this working properly?

    Thanks advanced!

    Arnel



  • 2.  RE: Auto-delete snapshots via script

    Posted Sep 22, 2015 07:17 PM

    are u checked the logs in host



  • 3.  RE: Auto-delete snapshots via script

    Posted Sep 23, 2015 04:07 PM

    Hi SARAVANAN_O,

    Thanks for your reply. May I know, which specific log file should I check from the HOST? I'm kind of a newbie here so I really don't know which log to check.



  • 4.  RE: Auto-delete snapshots via script

    Posted Sep 25, 2015 04:46 PM

    Hi,

    Did you try ton run the PowerCli Script manually ?

    Also I noticed incorrect parameter in remove-snapshot cmdlet

    Remove-Snapshot

      Confirm:$false

    Correct one is ( "-" is missing before Confirm).

    Remove-Snapshot

    -Confirm:$false

    Please check



  • 5.  RE: Auto-delete snapshots via script

    Posted Oct 01, 2015 07:17 PM

    Hi AlladiS,

    Thanks for pointing that out. I have added the missing "-" in the script now. However, it appears that the its still not deleting the snaps older than 3 days. I also tried running the script manually but nothing happens as well. Although, in the history Im seeing this below successful message,  Any other thought what I might be missing here?

    Task Scheduler successfully finished "{9673be4f-4129-4ed0-a060-da392719b564}" instance of the "\Auto-delete snapshot" task for user "Domain\agent02".

    Thank you very much for your help guys!

    Arnel



  • 6.  RE: Auto-delete snapshots via script

    Posted Feb 19, 2016 03:43 PM

    This is the script I'm working with. Install PowerCLI on a server, save this script as a PowerShell script, and create a scheduled task.

    Add-PSSnapin VMware.VimAutomation.Core

    Connect-VIServer -Server YourvCenterServerName

    Get-VM  | `

      Get-Snapshot |  `

      Where-Object { $_.Created -lt (Get-Date).AddDays(-0) } | `

      Remove-Snapshot -Confirm:$False -Whatif

    As is, this will delete ALL snapshots in your environment that are older than X days (0 in the case above). I'm trying to figure out how to make it only delete snapshots in a certain resource pool.

    This is a suggestion I found from someone on the boards, but it is still deleting snapshots in all resource pools. It's supposed to exclude rp1 and rp2.

    Add-PSSnapin VMware.VimAutomation.Core

    Connect-VIServer -Server YourvCenterServerName

    Get-ResourcePool | Where {"rp1","rp2" -notcontains $_.Name} | `

      Get-VM | `

      Get-Snapshot |  `

      Where-Object { $_.Created -lt (Get-Date).AddDays(-0) } | `

      Remove-Snapshot -Confirm:$False -Whatif

    Anyone have any ideas of what I'm doing wrong, or how I could exclude certain resource pools?

    (For testing, I added -Whatif and made the days -0. So you'll want to remove/change those as needed.)



  • 7.  RE: Auto-delete snapshots via script

    Posted Dec 07, 2017 04:23 AM

    Add-PSSnapin VMware.VimAutomation.Core

    Connect-VIServer -Server YourvCenterServerName

    Get-ResourcePool | Where {"rp1","rp2" -notcontains $_.Name} | `

      Get-VM | `

      Get-Snapshot |  `

      Where-Object { $_.Created -lt (Get-Date).AddDays(-0) } | `

      Remove-Snapshot -Confirm:$False -Whatif

    Anyone have any ideas of what I'm doing wrong, or how I could exclude certain resource pools?

    (For testing, I added -Whatif and made the days -0. So you'll want to remove/change those as needed.)

    Sorry for the super late reply but I wanted to answer this anyways in case someone else see's this. After one look at this you should have noticed the problem in the Where "rp1", "rp2" line...

    Add-PSSnapin VMware.VimAutomation.Core

    Connect-VIServer -Server YourvCenterServerName

      Get-ResourcePool | Where {$_.Name -notlike "*rp1*" -and $_.Name -notlike "*rp2*"}

      Get-VM | `

      Get-Snapshot |  `

      Where-Object { $_.Created -lt (Get-Date).AddDays(-0) } | `

      Remove-Snapshot -Confirm:$False -Whatif



  • 8.  RE: Auto-delete snapshots via script

    Posted Nov 10, 2018 09:16 AM

    The path that you suggested does not work in my system, can we save the files in any folder other than Program Files? I am getting Printer Not Activated Error Code 20