Public/Test-InternetConnection.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 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
<#PSScriptInfo
.NAME Test-InternetConnection .VERSION 1.0.3 .DESCRIPTION Test-InternetConnection is a quick why to see if you are online utilizing DNS Lookup. .GUID 26e6e82b-7b29-4fa5-b56a-51ce0d1d1e79 .AUTHOR DecimalZero .COMPANYNAME NestLink .COPYRIGHT Copyright (C) NestLink. All rights reserved. .TAGS Test-InternetConnection InternetConnection Check Internet DNSLookup DNS Lookup PowerShell Console Install Module Parameters String Switch Function Help Error Azure REST API APIs DecimalZero .LICENSEURI https://github.com/DecimalZero/Dz.Test-InternetConnection/blob/main/LICENSE .PROJECTURI https://github.com/DecimalZero/Dz.Test-InternetConnection .ICONURI https://github.com/DecimalZero/Dz.Test-InternetConnection/tree/main/Images/Test-InternetConnection.png .RELEASENOTES https://github.com/DecimalZero/Dz.Test-InternetConnection/blob/main/README.md .REQUIRESMODULE N/A .EXTERNALMODULEDEPENDENCIES N/A .REQUIREDSCRIPTS N/A .EXTERNALSCRIPTDEPENDENCIES N/A .PRIVATEDATA N/A .PARAMETERS [[-DnsDomainName] <String>] [[-Help] <SwitchParameter>] [[-Debug] <SwitchParameter>] #> Param ( [String]$DnsDomainName, [Switch]$Help, [Switch]$Debug ) # Set powershell window title $Host.UI.RawUI.WindowTitle = 'TestInternetConnection -Help' # Check if dns domain name parameter is blank If ($DnsDomainName -eq ''){ # Set dns domain name variable $DnsDomainName = 'azure.microsoft.com' } Function WriteErrorDetails{# Write error details to powershell console # Write error message Write-Host ((' ') + ($error[0].ToString())) -ForegroundColor Red; Write-Host # Write powershell source code postition message Write-Host ((' ') + ($error[0].InvocationInfo.PositionMessage)) -ForegroundColor Red; Write-Host } Function Help-Message{# Custom help message # Display custom help message Write-Message 'TestInternetConnection is a quick way to see if you are online utilizing DNS Lookup.' -Indent2 -AddLineBefore Write-Message 'This function can be called by other PowerShell scripts to check if the Internet is' -Indent2 Write-Message 'accessible before performing Azure, GCP, or AWS REST API calls across the Internet.' -Indent2 -AddLineAfter Write-Message 'When using TestInternetConnection, if Successful, nothing is returned or displayed.' -Indent2 Write-Message 'If there is an error, a warning will display stating the Internet is unreachable.' -Indent2 -AddLineAfter Write-Message 'NOTE: To use this within a PowerShell console, check out DNSLookup.ps1 in the Example folder.' -Indent2 -AddLineAfter Write-Message '_____________________________________________________________________________________________' -Indent2 -AddLineAfter Write-Message 'New to PowerShell? This PowerShell example is a great way to learn the following...' -Indent2 -AddLineBefore -AddLineAfter Write-Message '1.) How to use Parameters' -Indent2 Write-Message '2.) How to use a PowerShell String' -Indent2 Write-Message '3.) How to use a PowerShell Switch' -Indent2 Write-Message '4.) How to Pass a Variable to a Function' -Indent2 Write-Message '5.) How to Write a PowerShell Help Function' -Indent2 Write-Message '6.) How to Write PowerShell Error Details' -Indent2 Write-Message '7.) How to Automatically Check for Installed Modules' -Indent2 Write-Message '8.) How to Automatically Install PowerShell Modules' -Indent2 Write-Message '9.) How to Perform an Internet DNS Lookup' -Indent2 -AddLineAfter Write-Message '_____________________________________________________________________________________________' -Indent2 -AddLineAfter Write-Message 'EXAMPLES:' -Indent2 -AddLineBefore Write-Message '.\TestInternetConnection.ps1' -Indent2 Write-Message '.\TestInternetConnection.ps1 -Help' -Indent2 Write-Message '.\TestInternetConnection.ps1 Google.com' -Indent2 Write-Message '.\TestInternetConnection.ps1 SomeFakeDomainName.com' -Indent2 Write-Message '.\TestInternetConnection.ps1 SomeFakeDomainName.com -Debug' -Indent2 -AddLineAfter # Break out of powershell script Break } Function Check-Modules{# Check if required powershell gallery modules are installed # Check to see if required powershell dz.write-message module is installed Write-Host;Write-Host ('Checking if Required PowerShell Module Dz.Write-Message is Installed...') -ForegroundColor Yellow If (Get-Module -ListAvailable -Name Dz.Write-Message) { # Import module dz.write-message Import-Module Dz.Write-Message # Write message utilizing the imported dz.write-message module Write-Message (('PowerShell Module |Dz.Write-Message v') + (((Get-Module -Name Dz.Write-Message -ListAvailable).ModuleBase).Split('\')[7]) + ('| is Installed...')) 'Cyan|Green|Cyan' -Indent1 }Else { Try {# Try installing the required powershell module Write-Host ' - Installing PowerShell Module Dz.Write-Message...' -ForegroundColor Cyan Install-Module -Name Dz.Write-Message -Force # Import module dz.write-message Import-Module Dz.Write-Message # Write message utilizing the imported dz.write-message module Write-Message (('PowerShell Module |Dz.Write-Message v') + (((Get-Module -Name Dz.Write-Message -ListAvailable).ModuleBase).Split('\')[7]) + ('| is Installed...')) 'Cyan|Green|Cyan' -Indent1 }Catch { # Display error message Write-Host Write-Host ' WARNING: Something Went Wrong. Unable to Install' -ForegroundColor Red Write-Host ' or Import PowerShell Module Dz.Write-Message!!' -ForegroundColor Red Write-Host # Check if debug switch equals true If ($Debug -eq $True){ # Call function to write error details to powershell console WriteErrorDetails } # Break out of powershell script Break } } } Function TestConnection{# Test internet connectivity Param ( [String]$DnsDomainName ) Try {# Test to see if dns domain name can be resolved Resolve-DnsName -Name $DnsDomainName -Type NS -DnsOnly -ErrorAction Stop | Where-Object Section -eq Additional Write-Message ((' Domain Name |') + ($DnsDomainName) + ('| Resolved Successfully!!')) 'Green|Yellow|Green' -AddLineBefore -AddlineAfter }Catch { # Display error message Write-Message ((' WARNING: Domain Name |') + ($DnsDomainName) + ('| Did Not Resolve Successfully!!')) 'Red|Yellow|Red' -AddlineBefore Write-Message ' Verify Internet Connectivity, Network Cable, or Wifi and Try Again.' 'Red' -AddLineAfter # Check if debug switch equals true If ($Debug -eq $True){ # Call function to write error details to powershell console WriteErrorDetails } # Break out of powershell script Break } } # Check for help switch If ($Help -eq $True){ # Call function to display custom help message Help-Message # Break out of powershell script Break } # Call function to check if required powershell modules are installed Check-Modules # Call function to test internet connection TestConnection $DnsDomainName |