Last active
October 28, 2023 02:55
-
-
Save IISResetMe/ccb7771ac7353a691a7fd65b71e2aa57 to your computer and use it in GitHub Desktop.
Task-based async Ping in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So
$Tasks
or$Task
?Secondly, to be asynchronous
SendPingAsync
requires timeout value