StringFinder/Extractor.ps1

134 lines
3.7 KiB
PowerShell
Raw Normal View History

2024-02-08 22:08:33 +01:00
enum State {
Started
Finished
Failed
}
function GetVMName {
param (
[string]$Rest
)
$tokens = $Rest -split "'"
return $tokens[1]
}
function ConvertDate {
param (
[string]$DateString
)
return [DateTime]$DateString
}
function DeterminState {
param (
[String]$Rest
)
if($Rest.IndexOf("Erfolgreiche") -cge 0){
return 'Finished'
} elseif ($Rest.IndexOf("gestartet") -cge 0) {
return 'Started'
} else {
return 'Failed'
}
}
function ProduceHashMap {
param (
$Elements
)
$HashTable =@{}
foreach ($elem in $Elements){
$indexOfFirst = $elem.IndexOf(" ")
if($indexOfFirst -le 0){
continue
2024-02-08 22:08:33 +01:00
}
$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#>
}
}
2024-02-08 22:08:33 +01:00
}
return $HashTable
2024-02-08 22:08:33 +01:00
}
function getElems {
param (
$Path
)
$lines = Get-Content -Path $Path | ForEach-Object {$_.SubString($_.IndexOf(":")+1)}
$elems = $lines | Out-String -Stream | Select-String -Pattern "VMware-Maschine" | Out-String -Stream
return $elems
}
function ProcessLogToHash {
param (
$Path
)
ProduceHashMap -Elements $(getElems -Path $Path)
}
<#$lines = Get-Content -Path .\log.txt | ForEach-Object {$_.SubString($_.IndexOf(":")+1)}
2024-02-08 22:08:33 +01:00
$elems = $lines | Out-String -Stream | Select-String -Pattern "VMware-Maschine" | Out-String -Stream
ProduceHashMap($elems)#>
2024-02-08 22:08:33 +01:00
<#$HashTable =@{}
foreach ($elem in $Elems){
$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
}
$HashTable[$vmName]=$array
} else{
$HashTable += @{$vmName=@(New-Object psobject -Property @{
state = $backUpState
ts = $timeStamp
})}
}
}
$HashTable#>