Category filter
Script to Ping a Website on Windows
Ping is a command-line utility that checks if an IP address or host is accessible over a network connection. To check the accessibility of a website, the Ping command sends data packets to the target address and waits for the response. A successful ping is registered if the data packets are returned without damage. IT admins can remotely troubleshoot network connectivity issues on their endpoint devices using the Ping command. This document covers the custom script to remotely ping a website on a Windows device using Hexnode UEM.
Batch Script
1 2 3 4 5 |
@echo off Ping –n 1 %1 |find “TTL=” > nul If errorlevel 1(echo The site is not reachable) Else (echo The site could be pinged) |
Pass the website to be pinged as the argument.
For example: https://www.hexnode.com
echo off
– To prevent displaying all commands in a batch filePing
– This command initializes the website ping request.–n
– This flag is a count of the number of echo requests to send.%1
– Represents the first argument passed by the admin, in this case, website URL or IP address.TTL
– Stands for Time to Live. Only if the packet’s time to live is greater than 0, theerrorlevel
is checked.errorlevel
– This command returns an integer. Any value other than 1 is considered a successful website ping, and the corresponding message is displayed.
PowerShell Script
1 2 3 4 5 6 7 8 9 10 11 12 |
Param($Hostname) Process { "Computer to ping: $Hostname" $ping = get-wmiobject -Query "select * from win32_pingstatus where Address='$Hostname'" if ($ping.statuscode -eq 0) { "Website is accessible and responded in: {0}ms" -f $ping.responsetime } else {"Website did not respond"} } |
Pass the website to be pinged as the argument.
For example: https://www.hexnode.com
$Hostname
– Contains the website passed as the argument.$ping
– Initializes the ping request for the target website.get-wmiobject
– Gets the instances of Windows Management Instrumentation (WMI) classes or information about the available classes.win32_pingstatus
– Gives the values returned by the$ping
command.$ping.Statuscode
– This returns an integer value. If the value is equal to 0, a success message is displayed. Any value other than 0 is considered an error.$ping.responsetime
– Displays the time taken by the website to respond.