Run a powershell script in the background once per minute

后端 未结 8 2028
攒了一身酷
攒了一身酷 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:17

    Schedule your task to be run as System. The command below will schedule a script to be run in the background without showing powershell console window:

    schtasks /create /tn myTask /tr "powershell -NoLogo -WindowStyle hidden -file myScript.ps1" /sc minute /mo 1 /ru System

    /ru switch lets you change the user context in which the scheduled task will be executed.

    0 讨论(0)
  • 2020-12-23 17:17
    #   Filecheck.ps1
    
    #   Version 1.0
    
    #   Use this to simply test for the existance of an input file... Servers.txt.
    
    #   I want to then call another script if the input file exists where the
    
    #   servers.txt is neded to the other script.
    
    #
    
    
        $workpath=".\Server1\Restart_Test"
    
    #   Created a functon that I could call as part of a loop.
    
    
        function Filecheck
    
        {
        if (test-path $workpath\servers.txt)
    
            {
    
            rename-item $workpath\servers.txt servers1.txt
    
            "Servers.txt exist... invoking an instance of your script agains the list of servers"
    
    
            Invoke-Expression .\your_Script.ps1
    
            }
    
        Else
            {
    
                "sleeping"
    
                Start-Sleep -Seconds 60
            }
    
         }
    
        Do
            {
            Filecheck
    
            $fred=0
            # Needed to set a variabe that I could check in the while loop.
    
            # Probably a better way but this was my way.
    
            }
    
            While( $fred -lt 1 )
    
    0 讨论(0)
  • 2020-12-23 17:19

    Use the Windows Task Scheduler and run your script like this:

    powershell -File myScript.ps1 -WindowStyle Hidden
    

    Furthermore create the script that it runs under a specific user account and not only when that user is logged on. Otherwise you'll see a console window.

    0 讨论(0)
  • 2020-12-23 17:19

    These answers are histerical! If you want to run a powershell script as a background job try start-job .\script from the CLI within the folder you house scripts in.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-23 17:23

    If you want to run script automatically in time interval. (for windows os)

    1)  Open Schedule tasks from control panel.
    2)  Select Task Scheduler Library from left side window.
    3)  select Create Task from right side window.
    4)  Enter Name in General tab (any name).
    5)  Open Triggers tab -> New
    6)  Select interval as needed.Use Advanced settings for repeat task. 
    7)  Open Actions tab ->New
    8)  Copy/Paste following line in Program/script *
    
    * Here D:\B.ps1 in code is path to my B.ps1 file
    

    C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe D:\B.ps1

    9)  ok and apply changes
    10) Done!  
    
    0 讨论(0)
提交回复
热议问题