Advertisement
tristanp

internet-monitor.ps

Dec 18th, 2020
1,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Start-ConnectionMonitoring
  2. {
  3.     param($isp, $gateway, $Logfile,[int]$Delay = 10,[Ipaddress] $adapter, [switch]$ispPopup, [switch]$gateWayPopup)
  4.     $spacer = '--------------------------'
  5.     while($true)
  6.     {
  7.         if(!(Test-Connection $gateway -source $adapter -count 1 -ea Ignore))
  8.         {
  9.             get-date | Add-Content -path $Logfile
  10.             "$gateWay Connection Failure" |add-content -Path $Logfile
  11.             $outagetime = Start-ContinousPing -address $gateway -adapter $adapter -Delay $Delay
  12.              "Total Outage time in Seconds: $outageTime" | Add-Content -path $Logfile
  13.              if($gateWayPopup)
  14.              {
  15.                 New-PopupMessage -location $gateway -outagetime $outagetime
  16.              }
  17.             $spacer |add-content -Path $Logfile
  18.         }
  19.         if((!(Test-Connection  $isp -Source $adapter -count 1 -ea Ignore)) -and (Test-Connection $gateway -count 1 -ea Ignore))
  20.         {
  21.             get-date | Add-Content -path $Logfile
  22.             "$isp Connection Failure" | Add-Content -Path $Logfile
  23.             $outagetime = Start-ContinousPing -address $isp -adapter $adapter -Delay $Delay
  24.             "Total Outage time in Seconds: $outageTime" | Add-Content -path $Logfile
  25.             if($ispPopup)
  26.              {
  27.                 New-PopupMessage -location $isp -outagetime $outagetime
  28.              }          
  29.             $spacer|add-content -Path $Logfile
  30.  
  31.            
  32.         }
  33.         Start-Sleep -Seconds $Delay
  34.     }
  35. }
  36.  
  37. function Start-ContinousPing
  38. {
  39.     param($address,[ipaddress] $adapter, [int]$Delay = 10)
  40.     $currentTime = get-date
  41.      While(!(Test-Connection $address -Source $adapter -count 1 -ea Ignore))
  42.             {
  43.               Start-Sleep -Seconds $Delay
  44.             }
  45.             $outageTime = ((get-date) - $currentTime).TotalSeconds
  46.          $outageTime
  47. }
  48. function New-PopupMessage
  49. {
  50.     param($location, $outagetime)
  51.     $Popup = New-Object -ComObject Wscript.Shell
  52.     $popup.popup("$location Failure - seconds: $outagetime ",0,"$location",0x1)
  53. }
  54.  
  55. $Logfile = "c:\temp\connection.log"
  56. $isp = 'www.google.com'
  57. if(!(test-path $Logfile))
  58. {
  59.     new-item -Path $Logfile
  60. }
  61. $IP = (Get-NetIPConfiguration -InterfaceAlias 'Ethernet').ipv4address.ipaddress
  62. $gateway = (Get-NetIPConfiguration).ipv4defaultGateway.nexthop
  63. Start-ConnectionMonitoring -isp $isp -gateway $gateway -Logfile $Logfile -adapter $IP -ispPopup -gateWayPopup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement