-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCheckNetworkConnection.ps1
More file actions
117 lines (92 loc) · 4.06 KB
/
CheckNetworkConnection.ps1
File metadata and controls
117 lines (92 loc) · 4.06 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
$logTimeStamp = Get-Date -Format "yyyyMMdd_HHmmss"
$LogPath = "C:\Temp\NetworkTest_$logTimeStamp.txt"
$Target = "8.8.8.8"
$DomainsToCheck = @(
"au.outlooksignatures.exclaimer.net",
"outlookclient.exclaimer.net"
)
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
$previousPingState = $null
$iterationCounter = 0
while ($true) {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$ping = Test-Connection -ComputerName $Target -Count 1 -Quiet -ErrorAction SilentlyContinue
if ($ping) {
if ($previousPingState -ne $true) {
"`n$timestamp - ########################################" | Out-File -FilePath $LogPath -Append
"$timestamp - Ping to $Target SUCCESS" | Out-File -FilePath $LogPath -Append
$state = "Success"
$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
}
}
foreach ($domain in $DomainsToCheck) {
"$timestamp - # - DNS lookup for $domain" | Out-File $LogPath -Append
try {
$dnsResult = Resolve-DnsName -Name $domain -ErrorAction Stop
foreach ($record in $dnsResult) {
if ($record.IPAddress) {
"$timestamp - $domain -> $($record.IPAddress)" | Out-File $LogPath -Append
}
}
}
catch {
"$timestamp - Resolve-DnsName failed for $domain, falling back to nslookup" | Out-File $LogPath -Append
(nslookup $domain 2>&1) | Out-Null
}
}
"$timestamp - ### Monitoring ###" | Out-File $LogPath -Append
}
$previousPingState = $true
}
else {
"`n$timestamp - ########################################" | Out-File -FilePath $LogPath -Append
"$timestamp - # - Ping to $Target FAILED" | Out-File -FilePath $LogPath -Append
$state = "Failed"
# Get adapters (modern method)
$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
}
foreach ($domain in $DomainsToCheck) {
"$timestamp - # - DNS lookup for $domain" | Out-File $LogPath -Append
try {
$dnsResult = Resolve-DnsName -Name $domain -ErrorAction Stop
foreach ($record in $dnsResult) {
if ($record.IPAddress) {
"$timestamp - $domain -> $($record.IPAddress)" | Out-File $LogPath -Append
}
}
}
catch {
"$timestamp - Resolve-DnsName failed for $domain, falling back to nslookup" | Out-File $LogPath -Append
(nslookup $domain 2>&1) | Out-Null
}
}
$previousPingState = $false
}
$iterationCounter++
Write-Host "`rIteration: $iterationCounter $state " -NoNewline
Start-Sleep -Seconds 5
}