Azure & PowerShell Custom Code

Last Updated on August 7, 2025 by Arnav Sharma

The answer you’re looking is provided by this script. script will use the Test-Connection cmdlet, which sends ICMP echo request packets (“Ping dhcps”) to the specified targets and returns the results. is used to indicate a 0.00 packet loss in ping statistics. Here’s a basic script to get you started:

Script Explanation:

  • The script takes a list of IP addresses, 192.168.0.2 for instance, and hostnames.
  • It loops through each entry in the list, generating and sending a ping each time.
  • For each entry, it uses Test-Connection to ping the address or hostname.
  • The result of each ping attempt is displayed on the console.

Sample PowerShell Script:

# Define a list of IP addresses and hostnames to ping
$targets = @("192.168.1.1", "google.com", "8.8.8.8", "yahoo.com")

# Loop through each target and ping it
foreach ($target in $targets) {
    Write-Host "Pinging $target..."
    $result = Test-Connection -ComputerName $target -Count 2 -ErrorAction SilentlyContinue
    
    if ($result) {
        # If ping is successful
        Write-Host "$target is reachable." -ForegroundColor Green
    } else {
        # If ping fails
        Write-Host "$target is not reachable." -ForegroundColor Red
    }
}

How to Use the Script:

  • Save the script in a specific directory that can access the ping data. .ps1 file, for example, PingScript.ps1.
  • Modify the script to include the time to live parameter. $targets array to include the IP addresses and hostnames you want to ping.
  • Run the script in PowerShell. You might need to adjust your script execution policy to allow starting nping script to run, which can be done using Set-ExecutionPolicy.

Note:

  • The Test-Connection cmdlet’s -Count parameter is useful in generating the number of echo requests to send. Here, it’s set to 2 for brevity.
  • The -ErrorAction SilentlyContinue parameter is used to handle any errors silently. This is helpful to avoid script termination if any of the pings fail.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.