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
You could try something like this.
Remove the WhatIf switch if you are sure the correct snapshots are removed.
$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
You could try something like this.
Remove the WhatIf switch if you are sure the correct snapshots are removed.
$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
Thanks a lot LucD, it work perfectly.
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
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
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.
The attached ZIP file seems to be corrupted.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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.
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
If I do the following
$creds=get-vicredentialstoreitem
$creds.host
$creds.user
$creds.password
I get the correct values for everything.
How do you run the script?
As a scheduled task?
Under your user account?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I'm currently running it under my login to make sure that it works before I set it up as a scheduled task
And will the scheduled task run under your account as well?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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
yeah, it's still not working.
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
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.