Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param
- (
- [Parameter(Mandatory = $true,HelpMessage="Please enter the network adapter name you want to test. ")][string] $networkAdapterName = "Wi-Fi",
- [Parameter(Mandatory = $true,HelpMessage="Please enter the interval in SECCONDS between next network connection tests")][int] $intervalBetweenNetworkConnectionTests = 10
- )
- function WaitBeforeNextConnectionTest
- {
- Write-Host "Sleep for $intervalBetweenNetworkConnectionTests secconds";
- Start-Sleep -Seconds $intervalBetweenNetworkConnectionTests;
- }
- function TestConnectionWithServer
- {
- param([Parameter(Mandatory = $true)][string]$uri)
- Write-Host "Connecting to : $uri";
- Test-Connection $uri -Quiet ;
- }
- function ResetNetWorkAdapter
- {
- $isAdapterOnHost = DoesNetworkAdapterExistOnCurrentHost;
- if($isAdapterOnHost -eq $FALSE)
- {
- 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";
- }
- Write-Host "Reseting $networkAdapterName";
- Restart-NetAdapter "$networkAdapterName";
- }
- function DoesNetworkAdapterExistOnCurrentHost
- {
- Get-NetAdapter | ForEach-Object {
- if($networkAdapterName -eq $_.Name)
- {
- return $TRUE;
- }
- }
- return $FALSE;
- }
- function IsUseInAdminRole
- {
- If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
- {
- Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!";
- break;
- }
- }
- function Main
- {
- IsUseInAdminRole;
- $DEFAULT_GATEWAY_ADDRESS = Get-NetIPConfiguration | Where-Object InterfaceAlias -eq $networkAdapterName |Select-Object -ExpandProperty IPv4DefaultGateway | Select-Object -ExpandProperty NextHop ;
- while($true)
- {
- Try
- {
- Write-Host "--------------------Starting Connection Test----------------------";
- Write-Host "Testing Gateway Connection";
- $isConectedToGateWay = TestConnectionWithServer -uri $DEFAULT_GATEWAY_ADDRESS;
- if($isConectedToGateWay)
- {
- Write-Host "GateWay Connection OK!" -foregroundcolor "green";
- Write-Host "Testing Internet Connection";
- $isConnectedToInternet = TestConnectionWithServer -uri "8.8.8.8";
- if($isConnectedToInternet)
- {
- Write-Host "Internet connection OK!" -foregroundcolor "green";
- }
- else
- {
- Write-Host "Internet connection FAILURE!" -foregroundcolor "red";
- ResetNetWorkAdapter;
- }
- }
- else
- {
- Write-Host "GateWay Connection FAILURE!" -foregroundcolor "red";
- ResetNetWorkAdapter;
- }
- }
- Catch
- {
- $exceptionLine = $_.InvocationInfo.ScriptLineNumber;
- Write-Host "Exception has occured during Script execution at line $exceptionLine terminating" -foregroundcolor "red";
- Write-Host "Exception Message : $_.Exception.Message" -foregroundcolor "red";
- break;
- }
- WaitBeforeNextConnectionTest;
- }
- }
- Main;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement