The following script can be used to perform a simple PING test to a list of computers using PowerShell.
Simply create the script (I call mine PingTest.ps1″) and in the same folder create a Computers.txt with a list of devices you want to ping. All of your core SCCM Site Systems is a good example.
Script:
$Computers = Get-Content "$PSScriptRoot\Computers.txt"
forEach ($comp in $Computers) {
if (Test-Connection -ComputerName $comp -Count 1 -Quiet) {
$comp | Out-File -FilePath $PSScriptRoot\ONLINE.txt -Append
Write-Host "$comp`tONLINE" -ForegroundColor Green
}
else {
$comp | Out-File -FilePath $PSScriptRoot\OFFLINE.txt -Append
Write-Host "$comp`tOFFLINE" -ForegroundColor Red
}
}
Update
I decided to use ChatGPT to help me make the script more efficient using PowerShell Runspaces, which allow you to carry out commands in parallel rather than serially. After some tweaks, I got it working and this runs much faster when working with larger lists.
#-----------------------------------------------------------------------------------------------------
#
# ScriptName: PingTest_Runspaces.ps1
#
# Description: Pings a list of devices from a file using Runspaces for parallel processing
#
# Version History: 1.0
#
#-----------------------------------------------------------------------------------------------------
# Define File Paths
$inputFilePath = "$PSScriptRoot\computers.txt"
$onlineFilePath = "$PSScriptRoot\ONLINE.TXT"
$offlineFilePath = "$PSScriptRoot\OFFLINE.TXT"
$allResultsFilePath = "$PSScriptRoot\AllResults.txt"
# Remove Previous Log Files if they exist
Remove-Item $onlineFilePath, $offlineFilePath, $allResultsFilePath -ErrorAction SilentlyContinue
# Read the list of device names
$deviceNames = Get-Content $inputFilePath
# Define the script block to be run in parallel
$scriptBlock = {
param (
[string]$deviceName
)
$ping = Test-Connection -ComputerName $deviceName -Count 1 -Quiet
$result = if ($ping) {
"ONLINE"
} else {
"OFFLINE"
}
[PSCustomObject]@{
DeviceName = $deviceName
Status = $result
}
}
# Create runspace pool and initialize the collection for runspaces
$runspacePool = [runspacefactory]::CreateRunspacePool(1, [Environment]::ProcessorCount)
$runspacePool.Open()
$runspaces = @()
# Create a runspace for each device and start it
foreach ($device in $deviceNames) {
$runspace = [powershell]::Create().AddScript($scriptBlock).AddArgument($device)
$runspace.RunspacePool = $runspacePool
$runspaces += [PSCustomObject]@{
Pipe = $runspace
Runspace = $runspace.BeginInvoke()
DeviceName = $device
}
}
$i = 1
# Wait for all runspaces to complete and process results
$results = foreach ($runspace in $runspaces) {
$output = $runspace.Pipe.EndInvoke($runspace.Runspace)
$runspace.Pipe.Dispose()
$deviceName = $output.DeviceName
$status = $output.Status
if ($status -eq "ONLINE") {
Write-Host "$i. $($runspace.DeviceName)`t`tONLINE" -ForegroundColor Green
Add-Content -Path $onlineFilePath -Value $runspace.DeviceName
}
elseif ($status -eq "OFFLINE") {
Write-Host "$i. $($runspace.DeviceName)`t`tOFFLINE" -ForegroundColor Yellow
Add-Content -Path $offlineFilePath -Value $runspace.DeviceName
}
else {
Write-Host "$($runspace.DeviceName) could not be pinged" -ForegroundColor Red
}
# Consolidate all results
$resultString = "$($runspace.DeviceName)`t$status"
Add-Content -Path $allResultsFilePath -Value $resultString
$resultString
$i++
}
# Clean up
$runspacePool.Close()
$runspacePool.Dispose()
Thanks!! Was able to get this to work right away (brand new to PS).
Great post! The step-by-step guide on using PowerShell for a simple ping test is incredibly helpful. I appreciate the clear explanations and examples. This will definitely streamline my work with SCCM. Thanks for sharing!