StringFinder/Extractor.ps1

158 lines
4.3 KiB
PowerShell

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
}
$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]
$lastState=($array[$array.Count-1]).state
#Write-Host $lastState
#Write-Host $backUpState
if (($lastState -eq $backUpState) -or ($backUpState -eq 'Finished' -and $lastState -ne 'Started') -or ($backUpState -eq 'Failed' -and $lastState -ne 'Started')) {
#Write-Host "skipping"
continue
}
#Write-Host "continuing"
try {
$timeStamp = ConvertDate($date)
$array += New-Object psobject -Property @{
state = $backUpState
ts = $timeStamp
}
}
catch {
$timeStamp = ($array[$array.Count-1]).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#>
}
}
}
return $HashTable
}
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)
}
function buildIntervals {
param (
$hash
)
$array = @()
foreach ($key in $hash.Keys){
#Write-Host $key
$list = $hash.$key
#Write-Host $list
$lastState = $null
$lastTS = $null
foreach ($elem in $list){
#Write-Host $elem
if (($elem.state -eq 'Started') -and ($null -eq $lastState)) {
$lastState=$elem.state
$lastTS=$elem.ts
}
if (($elem.state -ne 'Started') -and ($lastState -eq 'Started')) {
$array += @(New-Object psobject -Property @{
begining=$lastTS
ending=$elem.ts
state=$elem.state
vm=$key
})
$lastState=$null
$lastTS=$null
}
#Write-Host "$array"
}
}
return $array
}
function GetAffected {
param (
$listOfIntervals,
[datetime]$point,
[switch] $verbose
)
$array = @()
foreach ($elem in $listOfIntervals){
if ($point -le $elem.ending -and $point -ge $elem.begining){
if ($verbose) {
Write-Host "$point : $($elem.vm) $($elem.state)"
}
if(!$array.Contains($elem.vm)){
$array += @($elem.vm)
}
}
}
return $array
}