Sometimes you need to check if there is a working internet connection. You can use theping
command for that. But this command does not work if ICMP echo request have disabled on the firewall. If this is the case we have to check the status of a website. If the status is 200 then there is a working internet connection.
In this example I will use both vbscript and powershell to check if there is a working internet connection.
The basics
- The command
Invoke-WebRequest -Uri www.google.com -TimeoutSec 1 -UseBasicParsing
gives a lot of internet about the webpage. Pay attention to the statuscode: - The command
(Invoke-WebRequest -Uri www.google.com -TimeoutSec 1 -UseBasicParsing).StatusCode
returns only the statuscode. - There is an error message if there is no working internet connection.
- If there is a working internet connection then the script returns 200:
- In this case there is no working internet connection, so an error message is shown.
Further testing with PowerShell
CheckIfWebsiteIsReachable.ps1:
<#
.NOTES
===========================================================================
Created with: Windows PowerShell ISE
Created on: 21-October-2017
Created by: Willem-Jan Vroom
Organization:
Filename: CheckIfWebsiteIsReachable.ps1
===========================================================================
.DESCRIPTION:
It checks if there is a working internet connection by reading the webpage.
This is very usefull in case the ping command does not work when ICMP echo
requests are disabled on the firewall.
#>
param ($URL)
Function Get-StatusCodeFromWebsite
{
param($Website)
Try
{
(Invoke-WebRequest -Uri $Website -TimeoutSec 1 -UseBasicParsing).StatusCode
}
Catch
{
}
}
$URL = "www.google.com"
$Result = Get-StatusCodeFromWebsite -Website $URL
if($Result -eq 200)
{
Write-Host "The website $URL is reachable."
}
else
{
Write-Host "The website $URL is not reachable."
}
The next step: use vbscript and powershell to detect if there is a working internet connection
As far as I know vbscript has not the option to detect the status from a website. So we use the powershell function with vbscript to detect if there is a working internet connection.
The powershell script has changed a little bit:
CheckIfWebsiteIsReachable_with_vbscript.ps1:
<#
.NOTES
===========================================================================
Created with: Windows PowerShell ISE
Created on: 21-October-2017
Created by: Willem-Jan Vroom
Organization:
Filename: CheckIfWebsiteIsReachable_with_vbscript.ps1
===========================================================================
.DESCRIPTION:
It checks if there is a working internet connection by reading the webpage.
This is very usefull in case the ping command does not work when ICMP echo
requests are disabled on the firewall.
#>
Param ($URL)
Function Get-StatusCodeFromWebsite
{
param($Website)
Try
{
(Invoke-WebRequest -Uri $Website -TimeoutSec 1 -UseBasicParsing).StatusCode
}
Catch
{
}
}
$Result = Get-StatusCodeFromWebsite -Website $URL
if($Result -eq 200)
{
Exit 0
}
else
{
Exit 1
}
It exits with an exit code:
0 = success
1 = failure
And we use vbscript to check:
CheckIfWebsiteIsReachable_v100.vbs:
' ================================================================================================
' Check if a there is a working network connection. Do not use ping, as the ping command cannot
' bu used in case ICMP echo requests have been disabled on the firewall.
' Created by Willem-Jan Vroom
' Version history:
'
' 0.0.1
' - Initial version
'
' 1.0.0
' - Final version
' ================================================================================================
' ------------------------------------------------------------------------------------------------
' Declare the most variables.
' ------------------------------------------------------------------------------------------------
Option Explicit
Dim objShell : set objShell = WScript.CreateObject("WScript.Shell")
Dim strWebsite : strWebsite = "http://www.vroom.cc"
Dim strCurrentDir : strCurrentDir = Left(Wscript.ScriptFullname, InstrRev(Wscript.ScriptFullname, "\"))
Dim strCommand : strCommand = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File " & chr(34) & strCurrentDir & "CheckIfWebsiteIsReachable_with_vbscript.ps1" & chr(34) & " -URL "& strWebsite
Dim strText : strText = ""
Dim valResult : valResult = 0
' ------------------------------------------------------------------------------------------------
' Check the website that is mentioned in the string strWebsite.
' Use the exitcode from the powershell script:
' 0 = Success
' 1 = Failure
' ------------------------------------------------------------------------------------------------
valResult = objShell.Run(strCommand, 6, True)
if valResult = 0 Then
strText = "The website '" & strWebSite & "' could be reached"
else
strText = "The website '" & strWebSite & "' could not be reached"
end if
' ------------------------------------------------------------------------------------------------
' Display a message box with the result.
' ------------------------------------------------------------------------------------------------
msgbox strText,0,"The result."
In this example we try the webpage http://www.vroom.cc/.