- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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