I have below script which will delete snapshot older than 15 days, what i need is to add a parameter like get-snapshot or something to exclude snaphsot with name "donotdelete"
# vCenter Server configuration
$vcenter = "xxxxxxxx"
$vcenteruser = "administrator@vsphere.local"
$vcenterpw = "xxxxxxx"
#Connect to the vCenter server defined above. Ignore certificate errors
connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'
Clear-Host
Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-15)} | Remove-Snapshot -Confirm:$false
Try like this.
Btw why do you still have a Add-PSSnapin in there?
Which PowerCLI version are you using?
$vcenter = "xxxxxxxx"
$vcenteruser = "administrator@vsphere.local"
$vcenterpw = "xxxxxxx"
#Connect to the vCenter server defined above. Ignore certificate errors
connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'
Clear-Host
Get-VM | Get-Snapshot |
Where {$_.Created -lt (Get-Date).AddDays(-15) -and $_.Name -notmatch "donotdelete"} |
Remove-Snapshot -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this.
Btw why do you still have a Add-PSSnapin in there?
Which PowerCLI version are you using?
$vcenter = "xxxxxxxx"
$vcenteruser = "administrator@vsphere.local"
$vcenterpw = "xxxxxxx"
#Connect to the vCenter server defined above. Ignore certificate errors
connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'
Clear-Host
Get-VM | Get-Snapshot |
Where {$_.Created -lt (Get-Date).AddDays(-15) -and $_.Name -notmatch "donotdelete"} |
Remove-Snapshot -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD! working as expected.
It works but not able to filter based on description "-and $_.Name -notmatch "donotdelete"}