I'm hoping someone can tell me why my script won't complete. I'm trying to display and delete snapshots that are leftover by Commvault. I have it written so I can search by Name or Description by removing the marking or un-marking your preference. Everything works except for the section in "blue" below. My variable, $snaps, shows the desired output but the code below it won't delete the snapshots. It doesn't have any errors or give me a reason why it's not working.
* Parts of the script below came from other sources.
Hoping someone can assist me.
Thanks,
Charles
*********************************************************************************
## Loads VMWare PowerCLI ##
$ErrorActionPreference = "SilentlyContinue";
$ProgressPreference = "SilentlyContinue";
Add-PSSnapin VMWare* -ea SilentlyContinue
Import-Module VMWare* -ea SilentlyContinue
# Connects to the desired vCenter
Connect-VIServer My_vCenter_Server.fqdn.com
# Sets the maximum number of taks that can run in vCenter (don't want to overload the storage with too many IOPs)
$maxtasks = 4
#Clear Screen!
CLS
# Lists all snapshots based on the Name or Description and deletes them.
$snaps = get-vm | get-snapshot | where {$_.Name -match "__GX_BACKUP__"}| Format-Table -Property VM,Name,Created,Description, SizeMB
# $snaps = get-vm | get-snapshot | where {$_.Description -match "Snapshot created by Commvault"}| Format-Table -Property VM,Name,Created,Description, SizeMB
#Show the contents of the variable
$snaps
$i = 0
while($i -lt $snaps.Count){
Remove-Snapshot -Snapshot $snaps[$i] -RunAsync -Confirm:$false
$tasks = Get-Task -Status “Running” | where {$_.Name -eq “RemoveSnapshot_Task”}
while($tasks.Count -gt ($maxtasks-1)) {
sleep 30
$tasks = Get-Task -Status “Running” | where {$_.Name -eq “RemoveSnapshot_Task”}
}
$i++
}
Here's another way to do it.
$snaps = get-vm | get-snapshot | where {$_.Name -match "__GX_BACKUP__"}
$snaps | Format-Table -Property VM,Name,Created,Description, SizeMB
foreach ($snap in $snaps) {
$task = Remove-Snapshot -Snapshot $snap -RunAsync -Confirm:$false
while( (Get-Task -Id $task.id).state -eq "Running") {sleep 5}
"Removal of snapshot $($snap.name) on $($snap.vm.name) : $((Get-Task -Id $task.id).state)"
}
If it doesn't work, do you see any activity in the web client when running the remove in powercli?
Also you probably see no error because of $ErrorActionPreference = "SilentlyContinue";
Try removing the Format-Table
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Removing "Format-Table" had no bearing - same result. And, Format-Table isn't in the blue section of code. Is there something with that particular command that would break the remove snapshot commands?
Yes, due to the Format-Table, the objects you are passing are not Snapshot objects anymore.
Format-Table is intended to format output, and for that reason it converts the data to a format the Remove-Snapshot does not understand.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ahh - ok. Thx for explaining that.....good to know. I'll continue to look for a solution.
You are sure that there are snapshots?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes - see screenshot below.
Here's another way to do it.
$snaps = get-vm | get-snapshot | where {$_.Name -match "__GX_BACKUP__"}
$snaps | Format-Table -Property VM,Name,Created,Description, SizeMB
foreach ($snap in $snaps) {
$task = Remove-Snapshot -Snapshot $snap -RunAsync -Confirm:$false
while( (Get-Task -Id $task.id).state -eq "Running") {sleep 5}
"Removal of snapshot $($snap.name) on $($snap.vm.name) : $((Get-Task -Id $task.id).state)"
}
If it doesn't work, do you see any activity in the web client when running the remove in powercli?
Also you probably see no error because of $ErrorActionPreference = "SilentlyContinue";
Thx vXav - the code you provided allowed me to display the snaps and successfully remove them. I added the following line so I could search by Description. I can simply remark out the line I don't want to run.
$snaps = get-vm | get-snapshot | where {$_.Description -match "Dummy"}
Thanks again for the help. And, thx to LucD for his input as well.
Charles