-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkDownTest.ps1
30 lines (27 loc) · 1.6 KB
/
NetworkDownTest.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
[CmdletBinding()]
$Count = 1
$Computer = Read-Host -Prompt "Enter IP Address or Hostname to monitor"
$LogPath = Read-Host -Prompt "Enter filepath for saving log file - use .csv"
$TimePing = Read-Host -Prompt "Enter time in seconds between pings"
$Ping = @()
#Test if path exists, if not, create it
If (-not (Test-Path (Split-Path $LogPath) -PathType Container))
{ Write-Verbose "Folder doesn't exist $(Split-Path $LogPath), creating..."
New-Item (Split-Path $LogPath) -ItemType Directory | Out-Null
}
#Test if log file exists, if not seed it with a header row
If (-not (Test-Path $LogPath))
{ Write-Verbose "Log file doesn't exist: $($LogPath), creating..."
Add-Content -Value '"TimeStamp","Source","Destination","IPV4Address","Status","ResponseTime"' -Path $LogPath
}
#Log collection loop
Write-Verbose "Beginning Ping monitoring of $Comptuer every $TimePing seconds:"
While ($Count -gt 0)
{ $Ping = Get-WmiObject Win32_PingStatus -Filter "Address = '$Computer'" | Select @{Label="TimeStamp";Expression={Get-Date}},@{Label="Source";Expression={ $_.__Server }},@{Label="Destination";Expression={ $_.Address }},IPv4Address,@{Label="Status";Expression={ If ($_.StatusCode -ne 0) {"Failed"} Else {""}}},ResponseTime
$Result = $Ping | Select TimeStamp,Source,Destination,IPv4Address,Status,ResponseTime | ConvertTo-Csv -NoTypeInformation
$Result[1] | Add-Content -Path $LogPath
Write-verbose ($Ping | Select TimeStamp,Source,Destination,IPv4Address,Status,ResponseTime | Format-Table -AutoSize | Out-String)
$Count ++
Write-Verbose $Count
Start-Sleep -Seconds $TimePing
}