VMware Cloud Community
SF_Seagull
Contributor
Contributor

How can I remove snapshots on a schedule - Power Cli

Hi,

My Power Cli skills are limited please bear this in mind.

VSphere 6.5 running on Linux. Most of the VMs are Windows servers.

I have scheduled Snapshots in VSphere of servers just before the Monthly Windows updates are applied.

This is easy enough to do and the Snapshots are created using the naming convention e.g.:  "Monthly snapshot for ServerXYZ 3rd Monday at 01:00"

Management insist Snapshots are taken just before the Windows updates every month.

Of course I want to delete the snapshots pretty quickly and we have agreed that they can be deleted after three days.

I want my PowerCli script to run on a windows 2016 server and so I have installed PowerCli and I can manually run the following commands:

Connect-VIServer -Server myvcenterserver -User Blah -Password BlahBlahBlah

this connects me to the vcenter no problem

then I run

Get-VM | Get-Snapshot | Where {$_.Name -like "*Monthly*" -and $_.Created -lt (Get-Date).AddDays(-3)} | Remove-Snapshot -Confirm:$false

sure enough this runs and deletes any snapshot more than three days old with the word Monthly in the title. Hoorah!

Im trying to automate this using a Scheduled task but im stumped on how I can get it to connect and then to carry out the "remove snapshot" bit.

Alternatively I could  use SCCM to do this but I run into the same problem where regular Powershell does not seem to know about PowerCli for example:

Connect-VIServer : The term 'Connect-VIServer' is not recognized as the name of a cmdlet, etc etc

How do I get this simple command to run on a schedule?

0 Kudos
6 Replies
daphnissov
Immortal
Immortal

This needs to be moved to the PowerCLI subforum for proper visibility.

0 Kudos
DavidMorgan
Contributor
Contributor

This can be done in Windows. PowerCLI in Windows cause use the VICredentialStore to store credentials needed to connect to vSphere. Manually connect once, and the credential is stored in a file in that user's profile directory.

  • Install PowerCLI module on that Windows machine
  • Make sure the user account password does not change
  • Log into vSphere with that account and store that login in the VI credential store
  • Create your scheduled task (script) with your Connect-VIServer command at the start of your script, sans credential info as it will use what's in the credential store (as long as the server names match)
  • Bob's your uncle?
0 Kudos
IvarHome
Hot Shot
Hot Shot

This is also choice to do that - https://www.helpsystems.com/products/automate-enterprise

https://www.helpsystems.com/products/automate-enterprise

Altough last supported ESXi/vCenter version is 6.5. In future they make 6.7 support.

0 Kudos
SF_Seagull
Contributor
Contributor

Thank you David this looks like the answer. Yes, Robert does indeed appear to be ones avuncular relation...

0 Kudos
SF_Seagull
Contributor
Contributor

So, how do I get a scheduled task to run Power Cli instead of regular PowerShell? My schedule dtask fails because its just running Powershell.exe

0 Kudos
vijayku
VMware Employee
VMware Employee

You can setup powershell profile to load the powercli modules in powershell for the user which will be used to run the scheduled tasks. Follow Customizing your PowerShell Profile to setup the profile.

You can either load the whole powercli environment using below cmds, or you can load only the required modules.

cd "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\"

.\Initialize-PowerCLIEnvironment.ps1

0 Kudos