Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Last active October 28, 2023 02:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/ccb7771ac7353a691a7fd65b71e2aa57 to your computer and use it in GitHub Desktop.
Save IISResetMe/ccb7771ac7353a691a7fd65b71e2aa57 to your computer and use it in GitHub Desktop.
Task-based async Ping in PowerShell
# Define list of remote hosts (can be host names or IPs)
$RemoteHosts = @('www.google.com')
# Initiate a Ping asynchronously per remote host, pick up the result task objects
$Tasks = foreach($ComputerName in $RemoteHosts) {
(New-Object System.Net.NetworkInformation.Ping).SendPingAsync($ComputerName)
}
# Wait for all tasks to finish
[System.Threading.Tasks.Task]::WaitAll($Tasks)
# Output results
$Tasks |Select-Object -Expand Result
@scriptingstudio
Copy link

So $Tasks or $Task?
Secondly, to be asynchronous SendPingAsync requires timeout value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment