0

I have a file named server1.txt and one more file named server1_copy.txt. In server1.txt, all logs will be added from the application. So whenever a new line is added in the server1.txt, we need to copy that and paste it in the server1_copy.txt. We can keep the code in powershell script and schedule it to run every 30 minutes to perform this action.

I dont have much experiance in Powershell script. But I gone through so many documents in online, could not find a right solution to this.

Does anyone have any idea how we can achieve this using Powershell scripts.

saffron
  • 143
  • 1
  • 3
  • 12

1 Answers1

1

This will copy new lines added to server1.txt to server1_copy.txt in real time.

 Get-Content -Path 'server1.txt' -Tail 0 -Wait | ForEach-Object { Add-Content -Value $_ -Path 'server1_copy.txt' }

If you need to bring the files in sync before running the above or that real time is simply unnecessary, a file copy can be done with the Copy-Item cmdlet

Copy-Item -Path 'server1.txt' -Destination 'server1_copy.txt'
jfrmilner
  • 476
  • 2
  • 6