Code Release
MTPing (or Multi-Threaded Ping) is a powershell script designed to quickly ping a large list of machines.
Parameters
MachineList
This parameter should be a string that points to the location of a list of machines. The list should be formatted so that there is one machine per line.
Example:
192.168.1.1 127.0.0.1 www.google.com microsoft.com ::1
LogLocation
LogLocation should be a string path representing where the log file should be saved. The log file will be written in a simple tabular format. The first column contains the machine-name or ip as retrieved from the machine list. The second column contains the ping result. “True” meaning connection successful.
Example:
192.168.1.1 False 127.0.0.1 True www.google.com True microsoft.com True ::1 True
Usage
Inside of a powershell console enter the following:
&"" -MachineList "" -LogLocation ""
Example:
&"C:\Scripts\MTPing.ps1" -MachineList "C:\Scripts\Machines.txt" -LogLocation "C:\Logs\PingResults.txt"
Script
Param
(
[Parameter(Mandatory=$True)]
$MachineList,
[Parameter(Mandatory=$True)]
$LogLocation
)
$ToProcess = @()
$StreamReader = [System.IO.StreamReader] $MachineList
while (-not $StreamReader.EndOfStream)
{
$ToProcess+=$StreamReader.ReadLine();
}
$StreamReader.Close();
$ScriptBlock =
{
param
(
[PSObject]$InputObject
)
return @((Test-Connection $InputObject -quiet), $InputObject)
}
$Stream = [System.IO.StreamWriter] $LogLocation
$Pool = [RunspaceFactory]::CreateRunspacePool(1, 10)
$Pool.ApartmentState = "STA"
$Pool.Open()
$Pipes = @()
foreach($Object in $ToProcess)
{
$PipeLine = [System.Management.Automation.PowerShell]::create()
$PipeLine.RunspacePool = $Pool
$Mute=$PipeLine.AddScript($ScriptBlock).AddArgument($Object)
$Pipes+= @{
Pipe = $PipeLine
AsyncHandle = $PipeLine.BeginInvoke()
}
}
$Complete=$false;
$Completed=0;
while(-not $Complete)
{
$Found = $false;
for ($Index=0; $Index -lt $Pipes.Count; $Index++)
{
if ($Pipes[$Index] -ne $null)
{
$Found=$true;
if ($Pipes[$Index].AsyncHandle.IsCompleted)
{
$Result = $Pipes[$Index].Pipe.EndInvoke($Pipes[$Index].AsyncHandle)
$Pipes[$Index].Pipe.Dispose()
if ($Result[0])
{
Write-Host $Result[1] -ForegroundColor Green -NoNewline
Write-Host "`t"($Completed/$Pipes.Count*100)"%"
}
else
{
Write-Host $Result[1] -ForegroundColor Red -NoNewline
Write-Host "`t"($Completed/$Pipes.Count*100)"%"
}
$Stream.WriteLine($Result[1]+"`t"+$Result[0].ToString());
$Pipes[$Index] = $null;
$Completed++;
}
}
}
if (-not $Found)
{
$Complete = $true;
}
}
$Stream.Close();
$Pool.Close();