Last active
October 30, 2018 08:28
-
-
Save victortorrescosta/2b5fdf65253c8d983b969425c642f567 to your computer and use it in GitHub Desktop.
Powershell script to start Ping jobs on the background. Each ping job pings a target IP and streams the result to a separate file in the log folder. Each record in the log file has the date and time prepended, for easy audit.
This file contains hidden or 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 the list of target IP addresses | |
$pingTargetIps = "10.20.255.254", "10.20.2.1" | |
# Define the folder where the log files will be generated | |
$logFolder = "C:\Users\my_user\ping_logs" | |
echo "Prearing ping jobs..." | |
$scriptBlock = { | |
param ($ipAddress,$logFolder) | |
$ipAddressReplaced = $ipAddress.Replace(".","_") | |
# Define a filter to easily add date/time to our ping results | |
filter timestamp {"$(Get-Date -Format G): $_"} | |
# The log file will be like pingAAA_BBB_CCC_DDD.txt | |
ping $ipAddress -n 9999999 | timestamp | Out-File $logFolder\ping$ipAddressReplaced.txt -append | |
} | |
# Create an empty array to contain the job names | |
$jobNameList = [System.Collections.ArrayList]@() | |
Foreach($ipAddress in $pingTargetIps) | |
{ | |
echo "Starting JOB to ping $ipAddress saving to $logFolder" | |
$jobName = "ping$ipAddress" | |
$jobNameList.add($jobName) | |
Start-Job -Name $jobName -ScriptBlock $scriptBlock -ArgumentList $ipAddress,$logFolder | Out-Null | |
} | |
while($true) | |
{ | |
# Ask user for input in case we need to terminate | |
# if the character q is input, the ping jobs are terminated and the script terminates | |
$keyInput = Read-Host "Press the key q and enter to quit" | |
if ($keyInput -eq "q") | |
{ | |
Foreach($jobName in $jobNameList) | |
{ | |
Stop-Job -Name $jobName | |
} | |
# End program | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment