Posted On January 29, 2021

PowerShell: How to Quickly Ping a Target

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: How to Quickly Ping a Target

Method 1: Legacy ping command

# Legacy method that works without any fusses
$pingResult=ping google.com -n 1
return [bool]($pingResult -match '0% loss')

Method 2: Use WMI

# This ping command would limit the ttl to a certain value
# WMI must be healthy on the host system for this to work
$remoteHost='google.com'
$timeOut=200
[bool]$pingable=!(!(Get-WmiObject Win32_PingStatus -Filter "Address='$remoteHost' and Timeout=$timeout and ResolveAddressNames='true' and StatusCode=0"))
if($pingable){write-host "Ping $remotehost successful"}else{write-host "Ping $remoteHost failed"}

Method 3: Use PowerShell Native

# This works most of the time. On certain computers with corrupted WMI, this would freeze indefinitely
Test-connection google.com -Count 1 -Quiet

Leave a Reply

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

Related Post

Basic JavaScript: Manipulate Arrays With shift()

// Setupvar myArray = [["John", 23], ["dog", 3]];// Only change code below this line.var removedFromMyArray=myArray.shift();

Apache Multiple Domains Config

vim /ect/httpd/conf/httpd.conf <VirtualHost *:80>      ServerAdmin [email protected]      ServerName kimconnect.com      ServerAlias www.kimconnect.com      DocumentRoot…

PowerShell: Deploy Choco Apps

Deployment Instructions: Create a batch file with this content to call this PowerShell Script @echo…