Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  param
  2.  (
  3.   [Parameter(Mandatory = $true,HelpMessage="Please enter the network adapter name you want to test. ")][string] $networkAdapterName = "Wi-Fi",
  4.   [Parameter(Mandatory = $true,HelpMessage="Please enter the interval in SECCONDS between next network connection tests")][int] $intervalBetweenNetworkConnectionTests = 10
  5.  )
  6.  
  7.  function WaitBeforeNextConnectionTest
  8.  {
  9.       Write-Host "Sleep for $intervalBetweenNetworkConnectionTests secconds";
  10.      
  11.       Start-Sleep -Seconds $intervalBetweenNetworkConnectionTests;
  12.  }
  13.  
  14.  function TestConnectionWithServer
  15.  {
  16.      param([Parameter(Mandatory = $true)][string]$uri)
  17.      
  18.      Write-Host "Connecting to : $uri";
  19.      Test-Connection $uri -Quiet ;
  20.  }
  21.  
  22.  function ResetNetWorkAdapter
  23.  {
  24.     $isAdapterOnHost = DoesNetworkAdapterExistOnCurrentHost;
  25.    
  26.     if($isAdapterOnHost -eq $FALSE)
  27.     {
  28.         throw "Adapter $networkAdapterName does not exist on the current Host make sure to use a correct name. You can list network adapters by using Get-NetAdapter";
  29.     }
  30.        
  31.    
  32.     Write-Host "Reseting $networkAdapterName";
  33.    
  34.     Restart-NetAdapter "$networkAdapterName";
  35.  }
  36.  
  37.  function DoesNetworkAdapterExistOnCurrentHost
  38.  {
  39.     Get-NetAdapter | ForEach-Object {
  40.         if($networkAdapterName -eq $_.Name)
  41.         {
  42.             return $TRUE;
  43.         }
  44.     }
  45.    
  46.     return  $FALSE;
  47.  }
  48.  
  49.  function IsUseInAdminRole
  50.  {
  51.     If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
  52.     {
  53.         Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!";
  54.         break;
  55.     }
  56.  }
  57.  
  58.  function Main
  59.  {   
  60.     IsUseInAdminRole;
  61.  
  62.     $DEFAULT_GATEWAY_ADDRESS = Get-NetIPConfiguration | Where-Object InterfaceAlias -eq $networkAdapterName |Select-Object -ExpandProperty IPv4DefaultGateway | Select-Object -ExpandProperty NextHop ;
  63.  
  64.     while($true)
  65.     {
  66.         Try
  67.         {
  68.        
  69.             Write-Host "--------------------Starting Connection Test----------------------";
  70.        
  71.             Write-Host "Testing Gateway Connection";
  72.        
  73.             $isConectedToGateWay = TestConnectionWithServer -uri $DEFAULT_GATEWAY_ADDRESS;
  74.        
  75.             if($isConectedToGateWay)
  76.             {
  77.                 Write-Host "GateWay Connection OK!" -foregroundcolor "green";
  78.            
  79.                 Write-Host "Testing Internet Connection";
  80.            
  81.                 $isConnectedToInternet = TestConnectionWithServer -uri "8.8.8.8";
  82.        
  83.        
  84.                 if($isConnectedToInternet)
  85.                 {
  86.                     Write-Host "Internet connection OK!" -foregroundcolor "green";
  87.                
  88.                 }
  89.                 else
  90.                 {
  91.                     Write-Host "Internet connection FAILURE!" -foregroundcolor "red";
  92.                     ResetNetWorkAdapter;
  93.                 }
  94.            
  95.             }
  96.             else
  97.             {
  98.                 Write-Host "GateWay Connection FAILURE!" -foregroundcolor "red";
  99.                 ResetNetWorkAdapter;
  100.             }
  101.         }
  102.         Catch
  103.         {
  104.             $exceptionLine = $_.InvocationInfo.ScriptLineNumber;
  105.             Write-Host "Exception has occured during Script execution at line $exceptionLine terminating" -foregroundcolor "red";
  106.             Write-Host "Exception Message :  $_.Exception.Message" -foregroundcolor "red";
  107.             break;
  108.         }
  109.        
  110.  
  111.         WaitBeforeNextConnectionTest;
  112.     }
  113.  }
  114.  
  115.  Main;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement