Powershell/CMD continuous ping to file which is overwritten after X days

I've found a lot of examples of continuous ping and saving output to a file. But what i was hoping to find was how do I have the script rewrite the output file after every 7 days, thereby eliminating the need for intervention.

Purpose: The idea is to run this script from a windows server and have it ping 8.8.8.8 every 30 seconds to check internet connectivity and log it to a file which is stored for 7 days and on the 8th day the script should start rewriting from line 1.

I only have the basic script:

Powershell:

ping -t 8.8.8.8|Foreach{"{0} - {1}" -f (Get-Date),$_} > c:\ping\test.txt

CMD:

ping 8.8.8.8 –t |cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 8.8.8.8>nul" >>"C:\ping\TEST.txt"
3

1 Answer

A simple solution will be to use the Task Scheduler to put this script into a task that is scheduled to run weekly.

The task should start with deleting the file, then creating a new one.

Some small points:

  • Usesleepfor waiting 30 seconds inside the loop
  • Use the >> indirection
  • Use an account for the task that can use the network.

enter image description here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like