Public/Target-Class.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
class Target { [String]$TargetName [String]$DNS hidden [PSObject]$Job [Int]$PingCount [boolean]$Status [Int]$Latency [Int]$LatencySum [Int]$SuccessSum [DateTime]$LastSuccessTime Target() {} Target([String]$TargetName,[PSObject]$Job){ $this.TargetName = $TargetName $this.Job = $Job $this.PingCount = 0 $this.Status = $null $this.Latency = 0 $this.LatencySum = 0 $this.SuccessSum = 0 } [String]ToString() { Return ("[{0}] {1} {2}ms {3:0.0}ms (avg) {4} {5:0.0}%" -f $this.Status, $this.TargetName, $this.Latency, $this.AverageLatency(), $this.PingCount, $this.PercentSuccess() ) } [PSCustomObject]ToTable() { If ($Global:PSVersionTable.PSEdition -and $Global:PSVersionTable.PSEdition -eq 'Core') { if ($this.PingCount -eq 0) { $s = "`e[1;38;5;0;48;5;226m PEND `e[0m" # Yellow } elseif ($this.status) { $s = "`e[1;38;5;0;48;5;46m OKAY `e[0m" # Green } else { $s = "`e[1;38;5;0;48;5;196m FAIL `e[0m" # Red } } else { if ($this.PingCount -eq 0) { $s = " PEND " } elseif ($this.status) { $s = " OK " } else { $s = " FAIL " } } Return [PSCustomObject]@{ Status = $s TargetName = $this.TargetName ms = $this.Latency Avg = [math]::Round($this.AverageLatency(),1) Count = $this.PingCount Loss = $this.PingCount - $this.SuccessSum Success = [math]::Round($this.PercentSuccess(),1) LastSuccess = $this.LastSuccessTime } } [void]Update() { $Data=Receive-Job -Id $this.Job.Id # If data is newer than update attributes If ($Data.ping -gt $this.PingCount) { $last = $Data | Select-Object -Last 1 $this.PingCount=$last.Ping $this.Status=$last.Status -eq "Success" $this.SuccessSum+=($Data.Status | Where-Object {$_ -eq "Success"} | Measure-Object).Count if ($this.Status) { $this.Latency=$last.Latency $this.LatencySum+=($Data.Latency | Measure-Object -Sum).Sum $this.LastSuccessTime = Get-Date } } } [int]Count() { Return $this.PingCount } [float]PercentSuccess() { If (! $this.PingCount -eq 0) { Return $this.SuccessSum / $this.PingCount * 100 } Return 0 } [float]AverageLatency() { If (! $this.SuccessSum -eq 0) { Return $this.LatencySum / $this.SuccessSum } Return 0 } } |