Watch file for changes and run command with powershell

前端 未结 7 850
终归单人心
终归单人心 2020-11-28 07:01

Is there any simple way(i.e., script) to watch file in Powershell and run commands if file changes. I have been googling but can\'t find simple solution. Basically I run scr

7条回答
  •  隐瞒了意图╮
    2020-11-28 07:45

    I had a similar problem. I first wanted to use Windows events and register, but this would be less fault-tolerant as the solution beneath.
    My solution was a polling script (intervals of 3 seconds). The script has a minimal footprint on the system and notices changes very quickly. During the loop my script can do more things (actually I check 3 different folders).

    My polling script is started through the task manager. The schedule is start every 5 minutes with the flag stop-when-already-running. This way it will restart after a reboot or after a crash.
    Using the task manager for polling every 3 seconds is too frequent for the task manager. When you add a task to the scheduler make sure you do not use network drives (that would call for extra settings) and give your user batch privileges.

    I give my script a clean start by shutting it down a few minutes before midnight. The task manager starts the script every morning (the init function of my script will exit 1 minute around midnight).

提交回复
热议问题