pi/tests/ping2.ps1

176 lines
6.2 KiB
PowerShell

# Importieren der notwendigen .NET-Namespaces
Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase, System.Windows.Forms
Add-Type -AssemblyName System.Net.NetworkInformation
# Erstellen des Hauptfensters
$window = New-Object System.Windows.Window
$window.Title = "Ping Monitor"
$window.Width = 600
$window.Height = 400
# Erstellen des DataGrid
$dataGrid = New-Object System.Windows.Controls.DataGrid
$dataGrid.Margin = "10"
$dataGrid.AutoGenerateColumns = $false
$dataGrid.IsReadOnly = $true
# Definieren der Spalten für das DataGrid
$hostColumn = New-Object System.Windows.Controls.DataGridTextColumn
$hostColumn.Header = "Host"
$hostColumn.Binding = New-Object System.Windows.Data.Binding "TargetHost"
$dataGrid.Columns.Add($hostColumn)
$statusColumn = New-Object System.Windows.Controls.DataGridTextColumn
$statusColumn.Header = "Status"
$statusColumn.Binding = New-Object System.Windows.Data.Binding "Status"
$dataGrid.Columns.Add($statusColumn)
$roundtripColumn = New-Object System.Windows.Controls.DataGridTextColumn
$roundtripColumn.Header = "Roundtrip Time"
$roundtripColumn.Binding = New-Object System.Windows.Data.Binding "RoundtripTime"
$dataGrid.Columns.Add($roundtripColumn)
$timestampColumn = New-Object System.Windows.Controls.DataGridTextColumn
$timestampColumn.Header = "Timestamp"
$timestampColumn.Binding = New-Object System.Windows.Data.Binding "Timestamp"
$dataGrid.Columns.Add($timestampColumn)
# Hosts, die gepingt werden sollen
$hosts = @("ME0001", "ME0002", "8.8.8.8", "1.1.1.1")
# Freigegebene ObservableCollection für die Ping-Ergebnisse
$pingResults = New-Object System.Collections.ObjectModel.ObservableCollection[PSCustomObject]
# Funktion zum Durchführen des Pings
# Importieren der notwendigen .NET-Namespaces
Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase, System.Windows.Forms
# Erstellen des Hauptfensters
$window = New-Object System.Windows.Window
$window.Title = "Ping Monitor"
$window.Width = 600
$window.Height = 400
# Erstellen des DataGrid
$dataGrid = New-Object System.Windows.Controls.DataGrid
$dataGrid.Margin = "10"
$dataGrid.AutoGenerateColumns = $false
$dataGrid.IsReadOnly = $true
# Definieren der Spalten für das DataGrid
$hostColumn = New-Object System.Windows.Controls.DataGridTextColumn
$hostColumn.Header = "Host"
$hostColumn.Binding = New-Object System.Windows.Data.Binding "TargetHost"
$dataGrid.Columns.Add($hostColumn)
$statusColumn = New-Object System.Windows.Controls.DataGridTextColumn
$statusColumn.Header = "Status"
$statusColumn.Binding = New-Object System.Windows.Data.Binding "Status"
$dataGrid.Columns.Add($statusColumn)
$roundtripColumn = New-Object System.Windows.Controls.DataGridTextColumn
$roundtripColumn.Header = "Roundtrip Time"
$roundtripColumn.Binding = New-Object System.Windows.Data.Binding "RoundtripTime"
$dataGrid.Columns.Add($roundtripColumn)
$timestampColumn = New-Object System.Windows.Controls.DataGridTextColumn
$timestampColumn.Header = "Timestamp"
$timestampColumn.Binding = New-Object System.Windows.Data.Binding "Timestamp"
$dataGrid.Columns.Add($timestampColumn)
# Hosts, die gepingt werden sollen
$hosts = @("ME0002", "ME0001", "8.8.8.8", "1.1.1.1")
# Freigegebene ObservableCollection für die Ping-Ergebnisse
$pingResults = New-Object System.Collections.ObjectModel.ObservableCollection[PSCustomObject]
# Funktion zum Durchführen des Pings
function Start-PingMonitor {
param (
[string[]]$hosts,
[System.Collections.ObjectModel.ObservableCollection[PSCustomObject]]$results
)
foreach ($targetHost in $hosts) {
try {
$ping = New-Object System.Net.NetworkInformation.Ping
if ($ping) {
$reply = $ping.SendAsync($targetHost, 1000)
$reply.Wait()
write-host $reply
if ($reply.Status -eq "Success") {
$pingResult = [PSCustomObject]@{
TargetHost = $targetHost
Status = "Success"
RoundtripTime = $reply.Result.RoundtripTime
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
}
else {
$pingResult = [PSCustomObject]@{
TargetHost = $targetHost
Status = "Failed"
RoundtripTime = "N/A"
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
}
# Hinzufügen des Ergebnisses zur freigegebenen ObservableCollection
$results.Add($pingResult)
}
else {
Write-Host "Failed to create Ping object"
}
}
catch [System.TimeoutException] {
$pingResult = [PSCustomObject]@{
TargetHost = $targetHost
Status = "Timeout"
RoundtripTime = "N/A"
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
# Hinzufügen des Timeout-Ergebnisses zur freigegebenen ObservableCollection
$results.Add($pingResult)
}
catch {
$errorMessage = "Fehler bei der Ping-Anfrage: $($Error[0])"
Write-Host $errorMessage
$pingResult = [PSCustomObject]@{
TargetHost = $targetHost
Status = "Error"
RoundtripTime = "N/A"
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
# Hinzufügen des Fehler-Ergebnisses zur freigegebenen ObservableCollection
$results.Add($pingResult)
}
}
}
# Erstellen des DispatcherTimer
$timer = New-Object System.Windows.Threading.DispatcherTimer
$timer.Interval = [System.TimeSpan]::FromSeconds(1)
$timer.Add_Tick({
Start-PingMonitor -hosts $hosts -results $pingResults
})
# Hinzufügen des DataGrid zum Fenster
$window.Content = $dataGrid
# Setzen der ItemsSource des DataGrid auf die ObservableCollection
$dataGrid.ItemsSource = $pingResults
# Starten des Timers
$timer.Start()
# Anzeigen des Fensters
$window.ShowDialog() | Out-Null
# Beenden des Timers, wenn das Fenster geschlossen wird
$timer.Stop()