-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping_status.ps1
More file actions
141 lines (106 loc) · 4.34 KB
/
Copy pathping_status.ps1
File metadata and controls
141 lines (106 loc) · 4.34 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<#
Script Name: Teams Ping Status Script
Description: This PowerShell script periodically pings a list of servers, reports their status, and posts updates to a Teams channel via a webhook. It runs in an infinite loop, with configurable ping intervals.
Author: Matthew Wicks
Date: 10/09/2023
# Usage Instructions:
1. Configure the $pingWebhookUrl variable with your Teams webhook URL.
2. Customize the list of servers and their details in the $ipAddresses array.
3. Adjust the $pingIntervalSeconds and $batchSize variables to control the script's behavior.
4. Save this script and run it to start monitoring and reporting server status.
Note: This script uses Test-Connection to ping servers and posts HTML-formatted updates to Teams.
# Configuration Variables:
$pingWebhookUrl - Teams webhook URL for posting updates.
$ipAddresses - An array containing server details, including Name, Address, and Site.
$pingIntervalSeconds - The interval (in seconds) at which the script checks server status.
$batchSize - The number of servers to ping in each batch.
$offlineStatus - A hashtable to track offline status and calculate downtime.
$logFilePath - Path to the log file for recording server status updates.
# Output:
The script periodically sends server status updates to the specified Teams channel using a webhook.
Server status information is also logged to the specified log file.
# Important:
Make sure to secure and protect sensitive information, such as webhook URLs.
#>
$pingWebhookUrl = ""
$ipAddresses = @(
@{ Name = "service"; Address = "ip"; Site = "local" },
)
$pingIntervalSeconds = 600
$batchSize = 8
$offlineStatus = @{}
$logFilePath = "ping_status_log.txt"
# Initialize $sites dynamically from $ipAddresses
$sites = $ipAddresses.Site | Sort-Object -Unique
while ($true) {
$pingMessages = @()
$currentDateTime = Get-Date
$formattedMessage = @"
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
}
h2 {
text-align: center;
}
p {
margin: 5px;
}
h3 {
margin-top: 15px;
margin-bottom: 5px;
}
</style>
</head>
<body>
<h2> Ping Server Status Report</h2>
<Br>
<p>Date: $($currentDateTime.ToString("yyyy-MM-dd HH:mm:ss"))</p>
<Br>
"@
foreach ($site in $sites) {
$pingMessages += "<h3>$site</h3>"
foreach ($device in ($ipAddresses | Where-Object { $_.Site -eq $site })) {
$pingResult = Test-Connection -ComputerName $device.Address -Count 1 -ErrorAction SilentlyContinue
$status = if ($pingResult) { "Online" } else { "Offline" }
$pingMessage = "Device: $($device.Name) | IP: $($device.Address) | Status: $status"
if ($status -eq "Offline") {
# Calculate offline duration
if (-not $offlineStatus.ContainsKey($device.Name)) {
$offlineStatus[$device.Name] = Get-Date
}
$offlineDuration = (Get-Date) - $offlineStatus[$device.Name]
$pingMessage += " | Offline Duration: $($offlineDuration.ToString())"
} else {
$offlineStatus.Remove($device.Name)
$responseTime = if ($pingResult) { $pingResult.ResponseTime } else { "N/A" }
$packetLoss = if ($pingResult -and $pingResult.PacketLoss) { $pingResult.PacketLoss } else { "0%" }
$pingMessage += " | Response Time: $responseTime ms | Packet Loss: $packetLoss"
}
$pingMessages += "<p>$pingMessage</p>"
# Save to the log file as a plain text line
$logLine = "$site,$($device.Name),$($device.Address),$status,$($offlineDuration.TotalSeconds),$responseTime,$packetLoss"
$logLine | Out-File -Append -FilePath $logFilePath
}
$pingMessages += "<br />"
}
$formattedMessage += "$($pingMessages -join '')</body></html>"
Write-Host $formattedMessage
$messageBody = @{
"text" = $formattedMessage
"contentType" = "html"
}
# Invoke the Teams webhook
Invoke-RestMethod -Uri $pingWebhookUrl -Method Post -Headers @{
"Content-Type" = "application/json"
} -Body ($messageBody | ConvertTo-Json)
$countdown = $pingIntervalSeconds
while ($countdown -gt 0) {
Write-Host "Next ping update in $countdown seconds..."
Start-Sleep -Seconds 1
$countdown--
}
}