From a71cf2f949f5db008faf845f321d455ce1829f18 Mon Sep 17 00:00:00 2001 From: Karl-Wilfried Zimmer Date: Thu, 8 Feb 2024 22:54:01 +0100 Subject: [PATCH] implemented try catch blocks on adding to hashmap to allow graceful handling of exceptions --- Extractor.ps1 | 69 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/Extractor.ps1 b/Extractor.ps1 index b1b298a..e8ce673 100644 --- a/Extractor.ps1 +++ b/Extractor.ps1 @@ -37,32 +37,53 @@ function ProduceHashMap { $Elements ) $HashTable =@{} -foreach ($elem in $Elements){ - $indexOfFirst = $elem.IndexOf(" ") - if($indexOfFirst -le 0){ - continue - } - $date = $elem.SubString(0,$elem.IndexOf(" ",($indexOfFirst + 1))) - $timeStamp = ConvertDate($date) - $rest = $elem.Substring($elem.IndexOf(" ",($indexOfFirst + 1))+1) - $vmName = GetVMName($rest) - $backUpState = DeterminState($rest) - - if($HashTable.ContainsKey($vmName)){ - $array = $HashTable[$vmName] - $array += New-Object psobject -Property @{ - state = $backUpState - ts = $timeStamp + foreach ($elem in $Elements){ + $indexOfFirst = $elem.IndexOf(" ") + if($indexOfFirst -le 0){ + continue } - $HashTable[$vmName]=$array - } else{ - $HashTable += @{$vmName=@(New-Object psobject -Property @{ - state = $backUpState - ts = $timeStamp - })} + $date = $elem.SubString(0,$elem.IndexOf(" ",($indexOfFirst + 1))) + $rest = $elem.Substring($elem.IndexOf(" ",($indexOfFirst + 1))+1) + $vmName = GetVMName($rest) + $backUpState = DeterminState($rest) + $timeStamp + if($HashTable.ContainsKey($vmName)){ + $array = $HashTable[$vmName] + try { + $timeStamp = ConvertDate($date) + $array += New-Object psobject -Property @{ + state = $backUpState + ts = $timeStamp + } + } + catch { + foreach ($i in (($array.Count-1)..0)){ + Write-Host $array[$i] + if ($array[$i]["state"] -eq 'Started') { + Write-Host "found started" + $timeStamp = $array[$i]['ts'] + } + } + $array += New-Object psobject -Property @{ + state = $backUpState + ts = $timeStamp + } + } + $HashTable[$vmName]=$array + } else { + try { + $timeStamp = ConvertDate($date) + $HashTable += @{$vmName=@(New-Object psobject -Property @{ + state = $backUpState + ts = $timeStamp + })} + } + catch { + <#Do nothing as key is empty and failed state#> + } + } } -} -$HashTable + return $HashTable } function getElems {