-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCheckNetworkConnection.ps1
More file actions
176 lines (146 loc) · 6.8 KB
/
CheckNetworkConnection.ps1
File metadata and controls
176 lines (146 loc) · 6.8 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
$logTimeStamp = Get-Date -Format "yyyyMMdd_HHmmss"
$LogPath = "C:\Temp\NetworkTest_$logTimeStamp.txt"
$Target = "8.8.8.8"
$DomainsToCheck = @(
@{ Host = "eu.outlooksignatures.exclaimer.net"; Port = 443 },
@{ Host = "outlookclient.exclaimer.net"; Port = 443 },
@{ Host = "login.microsoftonline.com"; Port = 443 }
)
if (!(Test-Path "C:\Temp")) {
New-Item -Path "C:\Temp" -ItemType Directory | Out-Null
}
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - ### - Started Logging" | Out-File -FilePath $LogPath
Write-Host "Running: Check $LogPath" -ForegroundColor Yellow
$maxAttempts = 400 # NOTE: Set to 0 to run indefinitely
$iterationCounter = 0
while ($true) {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# --- Ping check ---
$pingSuccess = Test-Connection -ComputerName $Target -Count 1 -Quiet -ErrorAction SilentlyContinue
# --- Domain checks (run every iteration regardless of ping result) ---
$domainResults = @()
$anyDomainFailed = $false
foreach ($item in $DomainsToCheck) {
$domain = $item.Host
$port = $item.Port
$entry = [PSCustomObject]@{
Domain = $domain
Port = $port
DnsSuccess = $false
IPs = @()
DnsError = $null
TcpSuccess = $false
TcpError = $null
}
# --- DNS check ---
try {
$dnsResult = Resolve-DnsName -Name $domain -ErrorAction Stop
$ips = $dnsResult | Where-Object { $_.IPAddress } | ForEach-Object { $_.IPAddress }
if ($ips) {
$entry.DnsSuccess = $true
$entry.IPs = $ips
}
else {
$entry.DnsError = "No IP records returned"
$anyDomainFailed = $true
}
}
catch {
# Resolve-DnsName failed - try nslookup as fallback
$nsOutput = (nslookup $domain 2>&1) -join "`n"
$parsedIPs = $nsOutput | Select-String -Pattern '\b\d{1,3}(\.\d{1,3}){3}\b' -AllMatches |
ForEach-Object { $_.Matches.Value } |
Where-Object { $_ -notmatch '^(127\.|0\.)' } # strip loopback/unspecified
if ($parsedIPs) {
$entry.DnsSuccess = $true
$entry.IPs = $parsedIPs
$entry.DnsError = "Resolve-DnsName failed; nslookup succeeded"
}
else {
$entry.DnsError = "Resolve-DnsName failed; nslookup also failed: $($_.Exception.Message)"
$anyDomainFailed = $true
}
}
# --- TCP connectivity check ---
try {
$tcpResult = Test-NetConnection -ComputerName $domain -Port $port -WarningAction SilentlyContinue -ErrorAction Stop
if ($tcpResult.TcpTestSucceeded) {
$entry.TcpSuccess = $true
}
else {
$entry.TcpError = "TCP connection refused or timed out"
$anyDomainFailed = $true
}
}
catch {
$entry.TcpError = "Test-NetConnection failed: $($_.Exception.Message)"
$anyDomainFailed = $true
}
$domainResults += $entry
}
# --- Decide whether to log this iteration ---
$shouldLog = (-not $pingSuccess) -or $anyDomainFailed
if ($shouldLog) {
"`n$timestamp - ########################################" | Out-File -FilePath $LogPath -Append
if (-not $pingSuccess) {
"$timestamp - # - Ping to $Target FAILED" | Out-File -FilePath $LogPath -Append
}
else {
"$timestamp - # - Ping to $Target SUCCESS (domain failure triggered log)" | Out-File -FilePath $LogPath -Append
}
# Network adapter info
$netConfigs = Get-NetIPConfiguration -ErrorAction SilentlyContinue
if ($netConfigs) {
foreach ($config in $netConfigs) {
$adapter = $config.InterfaceAlias
"$timestamp - # - Adapter: $adapter" | Out-File $LogPath -Append
"$timestamp - IPv4: $($config.IPv4Address.IPAddress -join ', ')" | Out-File $LogPath -Append
"$timestamp - Gateway: $($config.IPv4DefaultGateway.NextHop)" | Out-File $LogPath -Append
"$timestamp - DNS: $($config.DnsServer.ServerAddresses -join ', ')" | Out-File $LogPath -Append
"$timestamp - ---" | Out-File $LogPath -Append
}
}
else {
"$timestamp - # - No adapter detected" | Out-File $LogPath -Append
}
# Domain check results
foreach ($result in $domainResults) {
"$timestamp - # - Check: $($result.Domain) (Port $($result.Port))" | Out-File $LogPath -Append
# DNS
if ($result.DnsSuccess) {
"$timestamp - DNS: OK -> $($result.IPs -join ', ')" | Out-File $LogPath -Append
if ($result.DnsError) {
"$timestamp - DNS Note: $($result.DnsError)" | Out-File $LogPath -Append
}
}
else {
"$timestamp - DNS: FAILED - $($result.DnsError)" | Out-File $LogPath -Append
}
# TCP
if ($result.TcpSuccess) {
"$timestamp - TCP: OK (port $($result.Port) reachable)" | Out-File $LogPath -Append
}
else {
"$timestamp - TCP: FAILED - $($result.TcpError)" | Out-File $LogPath -Append
}
}
"$timestamp - ########################################" | Out-File $LogPath -Append
}
# Console status line
$iterationCounter++
$pingLabel = if ($pingSuccess) { "Ping=OK" } else { "Ping=FAIL" }
$anyDnsFailed = $domainResults | Where-Object { -not $_.DnsSuccess }
$anyTcpFailed = $domainResults | Where-Object { -not $_.TcpSuccess }
$dnsLabel = if (-not $anyDnsFailed) { "DNS=OK" } else { "DNS=FAIL" }
$tcpLabel = if (-not $anyTcpFailed) { "TCP=OK" } else { "TCP=FAIL" }
$logLabel = if ($shouldLog) { "[LOGGED]" } else { "" }
Write-Host "`rIteration: $iterationCounter $pingLabel $dnsLabel $tcpLabel $logLabel " -NoNewline
if ($maxAttempts -gt 0 -and $iterationCounter -ge $maxAttempts) {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"`n$timestamp - ### - Max attempts reached ($maxAttempts). Exiting." | Out-File -FilePath $LogPath -Append
Write-Host "`nMax attempts reached ($maxAttempts). Exiting." -ForegroundColor Yellow
break
}
Start-Sleep -Seconds 10 # NOTE: Interval in seconds between checks
}