VMware Cloud Community
antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Remove old snapshots from txt file but only if older than X day

Hello guys,

I have this script to delete old snapshots, I use to put list of VM in a txt file and then run,

$logincred = Get-VICredentialStoreItem -Host 0.0.0.0 -File C:\Users\geme\my.xml

Connect-VIServer 0.0.0.0 -User $logincred.User -Password $logincred.Password

$vmlist = Get-Content C:\Users\geme\Desktop\delete1JUly.txt

foreach($VM in $VMlist) {

Get-Snapshot -VM $vm |

Remove-Snapshot -Confirm:$false }

Disconnect-VIServer -Confirm:$false

Actually I would like to use this but delete snapshots only if is older than X days, this is to avoid to delete recently snapshot in VM where somebody created more than one.

Thanks

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could try something like this.

Remove the WhatIf switch if you are sure the correct snapshots are removed.

$daysOld = 14

$logincred = Get-VICredentialStoreItem -Host 0.0.0.0 -File C:\Users\geme\my.xml

Connect-VIServer 0.0.0.0 -User $logincred.User -Password $logincred.Password


$vmlist = Get-Content C:\Users\geme\Desktop\delete1JUly.txt


Get-VM -Name $vmlist | Get-Snapshot |

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

Remove-Snapshot -Confirm:$false -WhatIf


Disconnect-VIServer -Confirm:$false

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

Was it helpful? Let us know by completing this short survey here.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

19 Replies
LucD
Leadership
Leadership
Jump to solution

You could try something like this.

Remove the WhatIf switch if you are sure the correct snapshots are removed.

$daysOld = 14

$logincred = Get-VICredentialStoreItem -Host 0.0.0.0 -File C:\Users\geme\my.xml

Connect-VIServer 0.0.0.0 -User $logincred.User -Password $logincred.Password


$vmlist = Get-Content C:\Users\geme\Desktop\delete1JUly.txt


Get-VM -Name $vmlist | Get-Snapshot |

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

Remove-Snapshot -Confirm:$false -WhatIf


Disconnect-VIServer -Confirm:$false

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

Was it helpful? Let us know by completing this short survey here.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Thanks a lot LucD, it work perfectly.

Reply
0 Kudos
cjabates
Contributor
Contributor
Jump to solution

Luc,

I have a very similar script, but it no longer runs correctly. It actually returns $logincred.user and $logincred.password to the 'Connect-ViServer' instead of the actual user and password.

PSVersion 5.1.16299.1146

PowerCLI 11.2.0-12483598 (same behavior on 11.4 as well)

This same script works with

PSVersion 5.1.14393.3053

PowerCLI 10.0.0-7895300

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you sure there is a VICredentialSToreItem for that host?
Can you attach your script (as a file - bottom right Attach button)?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
cjabates
Contributor
Contributor
Jump to solution

here's the credentialing part of the script:

$creds = Get-VICredentialStoreItem -host <vcenter name scheme beginning>* -file <credentials file>

foreach ($cred in $creds) {Connect-VIServer -Server $cred.host -user $cred.user -password $cred.password }

*all my vCenters have the same naming scheme.  It pulls in all of the vCenter credentials correctly, as verified by running '$creds' on a command line by itself.

This was working up until very recently, as I use those two lines in a lot of scripts that get me information across my environment.

I've removed sensitive information from the script.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The attached ZIP file seems to be corrupted.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
cjabates
Contributor
Contributor
Jump to solution

ok.  let's try as a plain text file, then instead of a powershell file.

I'll also mention that it's no longer working under

PSVersion 5.1.17763.771

PowerCLI 11.3.0-13990089

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's better.

What is the actual error you are seeing?

For me it looks and seems to run ok.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
cjabates
Contributor
Contributor
Jump to solution

I get the following error message:

Connect-VIServer     cannot complete login due to an incorrect username or password.

at <filepath>\SnapShotCommit.ps1:41 char:28

+ … in $creds) {Connect-VIServer -Server $cred.host -user $cred.user -Pas ...

+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

     + CategoryInfo     : NotSpecified: (:) [Connect-VIServer], InvalidLogin

     + FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_SoapException,VMware.VimAutomation.ViCore.Cmd

    lets.Commands.ConnectVIServer

I just revalidated that the credential file has correct information.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try to display the values you are getting from the VICredentialStoreItem?

Note that these are bound to the user who created them and only valid on the station where they were created.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
cjabates
Contributor
Contributor
Jump to solution

If I do the following

$creds=get-vicredentialstoreitem

$creds.host

$creds.user

$creds.password

I get the correct values for everything.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

How do you run the script?
As a scheduled task?
Under your user account?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
cjabates
Contributor
Contributor
Jump to solution

I'm currently running it under my login to make sure that it works before I set it up as a scheduled task

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And will the scheduled task run under your account as well?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
cjabates
Contributor
Contributor
Jump to solution

No, the task will run under a service account's context.  I'm just making sure it will run correctly before I set that up.

The working assumption is that if I get it working with my credentials under my account, it will work once I set up the credential file for the service account.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you create a new credentials file under your account, and then test it under your account?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
cjabates
Contributor
Contributor
Jump to solution

yeah, it's still not working.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange.

I'm still not too sure what you meant by "It actually returns $logincred.user and $logincred.password to the 'Connect-ViServer' instead of the actual user and password."

The error just seems to indicate that the credentials are not correct.

When you hard-code the user and password instead of using the VICredentialStoreItem, does it work in that case?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
cjabates
Contributor
Contributor
Jump to solution

nope. doesn't work then, either.

I think I found the issue.  My password contains special characters, and apparently the script is choking on it.  I put single quotes around the password and it connected properly with everything hard-coded, and after putting single quotes around it when doing the new-vicredentialstoreitem, it is working as variables as well.

I guess I just needed the extra prodding.

Thanks for the assist, Luc.

Reply
0 Kudos