Run a powershell script in the background once per minute

后端 未结 8 2147
攒了一身酷
攒了一身酷 2020-12-23 16:38

I want a powershell script to be run once per minute in the background. No window may appear. How do I do it?

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 17:22

    Perhaps this scenario will do. We do not start PowerShell executable every minute (this is expensive, BTW). Instead, we start it once by calling an extra script that calls the worker script once a minute (or actually waits a minute after the worker script exits or fails).

    Create the starting Invoke-MyScript.ps1:

    for(;;) {
     try {
      # invoke the worker script
      C:\ROM\_110106_022745\MyScript.ps1
     }
     catch {
      # do something with $_, log it, more likely
     }
    
     # wait for a minute
     Start-Sleep 60
    }
    

    Run this from Cmd (e.g. from a startup .bat file):

    start /min powershell -WindowStyle Hidden -Command C:\ROM\_110106_022745\Invoke-MyScript.ps1
    

    The PowerShell window appears for a moment but it is minimized due to start /min and just in a moment it gets hidden forever. So that actually only the task bar icon appears for a moment, not the window itself. It's not too bad.

提交回复
热议问题