VMware Cloud Community
BBB36
Enthusiast
Enthusiast
Jump to solution

Delete snapshot and monitor progress and display success if 100% successful or error for each VM.

I have this script with the intention of monitoring the snapshot deletion progress and stating if it was successful or not for each VM.
Right now when I run it, it doesn't do anything. Can someone please help modify it so that it can output the progress and success of a snapshot deletion for each VM? Thanks.

$vCenterCredential = Get-Credential -Message "Enter account with access to the vCenter(s)" -ErrorAction SilentlyContinue;
$vCenters = Get-Content -Path "D:\Scripts\vclist.txt"
Connect-VIServer -Server $vCenters -Credential $vCenterCredential -Protocol https | Out-Null
$vmlist = Get-Content "D:\Scripts\vmlist.txt"
foreach($vm in $vmlist) {
Get-VM -Name $vm | Get-Snapshot |
   ForEach-Object -Process {
   Remove-Snapshot -Snapshot $_ -Confirm:$false -RunAsync
   $current = Get-Task | where {$_.Name -eq 'RemoveSnapshot_Task' -and 'Running','Queued' -contains $_.State}
   $runningTasks = $taskTab.Count
	while ($runningTasks -gt 0) {
	Get-Task | % {
      if ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success") {
         $vm = Get-VM $taskTab[$_.Id]
         Write-Host $vm snapshot is being deleted. -foregroundcolor red
		$taskTab.Remove($_.Id)
         $runningTasks--
       }
        elseif ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error") {
        $taskTab.Remove($_.Id)
        $runningTasks--
       }
      }
    }
  }
}

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You didn't specify that $taskTab is a hash table, and there are a couple of other things missing.
Try something like this

$vCenterCredential = Get-Credential -Message "Enter account with access to the vCenter(s)" -ErrorAction SilentlyContinue;
$vCenters = Get-Content -Path "D:\Scripts\vclist.txt"
Connect-VIServer -Server $vCenters -Credential $vCenterCredential -Protocol https | Out-Null

$taskTab = @{}

$vmlist = Get-Content "D:\Scripts\vmlist.txt"

Get-VM -Name $vmlist -PipelineVariable vm |
Get-Snapshot -PipelineVariable snap |
ForEach-Object -Process {
  $task = Remove-Snapshot -Snapshot $_ -Confirm:$false -RunAsync
  $taskTab.Add($task.Id, "VM: $($vm.Name) - Snapshot: $($snap.Name)")
}

$runningTasks = $taskTab.Count
while ($runningTasks -gt 0) {
  Get-Task | ForEach-Object {
    if ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success") {
      Write-Host "$($taskTab[$_.Id]) was deleted." -ForegroundColor red
      $taskTab.Remove($_.Id)
      $runningTasks--
    } elseif ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error") {
      $taskTab.Remove($_.Id)
      $runningTasks--
    }
  }
}


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You didn't specify that $taskTab is a hash table, and there are a couple of other things missing.
Try something like this

$vCenterCredential = Get-Credential -Message "Enter account with access to the vCenter(s)" -ErrorAction SilentlyContinue;
$vCenters = Get-Content -Path "D:\Scripts\vclist.txt"
Connect-VIServer -Server $vCenters -Credential $vCenterCredential -Protocol https | Out-Null

$taskTab = @{}

$vmlist = Get-Content "D:\Scripts\vmlist.txt"

Get-VM -Name $vmlist -PipelineVariable vm |
Get-Snapshot -PipelineVariable snap |
ForEach-Object -Process {
  $task = Remove-Snapshot -Snapshot $_ -Confirm:$false -RunAsync
  $taskTab.Add($task.Id, "VM: $($vm.Name) - Snapshot: $($snap.Name)")
}

$runningTasks = $taskTab.Count
while ($runningTasks -gt 0) {
  Get-Task | ForEach-Object {
    if ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success") {
      Write-Host "$($taskTab[$_.Id]) was deleted." -ForegroundColor red
      $taskTab.Remove($_.Id)
      $runningTasks--
    } elseif ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error") {
      $taskTab.Remove($_.Id)
      $runningTasks--
    }
  }
}


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

BBB36
Enthusiast
Enthusiast
Jump to solution

Excellent as always. Thank you. 

0 Kudos