none
Run a Powershell Script every xx minutes via a Windows Service?

    Question

  • Howdy,

    We have a server with a bunch of programs that start up automatically when the server boots up.  Problem is, each of the services takes a couple minutes to start and when they all try to go at once, it makes the machine unusable for 30+ minutes after a reboot.  So, I want to use a Powershell script (and/or a bat file to call the Powershell script) that will run a loop and start one up every minute or so.  I also want this to then run every minute or tow during the day in case a service stops for some reason that it will see it and start it back up again.

    I know I could do this via Task Manager with a task that runs every minute but is there a way to do the same thing via a service?  That way when we're in the Service window I can just Stop the "AutoStartup" service, do my work, and then restart it.

    Thanks.

    Wednesday, February 26, 2014 2:44 PM

Answers

  • Hi Kelemvor,

    Sorry for the delay reply.

    If you want to run powershell script every 2 minutes, please try the script bwlow, which can run the .ps1 file every 30 seconds within 2 minutes:

    $timeout = new-timespan -Minutes 2
    $sw = [diagnostics.stopwatch]::StartNew()
    while ($sw.elapsed -lt $timeout){
      d:\2.ps1
      start-sleep -seconds 30
    }
    
    write-host "stop"
    I hope this helps.

    Wednesday, March 05, 2014 2:32 AM

All replies

  • Hi Kelemvor33,

    If you want to loop several scripts in order, and want to quit at any time, please refer to the powershell script below:

    while ($true) {
    d:\test1.ps1
    d:\test2.ps1
    if ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
            Write-Host "Exiting now, don't try to stop me...." -Background DarkRed
            break;
        }
    }

    You can start the scripts with running the script above in powershell, and press "q" to quit.

    If I have any misunderstanding, please let me know.

    Best Regards,

    Anna

    Thursday, February 27, 2014 7:39 AM
  • Well, basically I will have a powershell script that searches for stopped services that meat a certain criteria and will start them if it finds them.  I just want this script to kick off every 2 minutes but I didn't know if I could do that via a Service instead of via a Scheduled Task.
    Thursday, February 27, 2014 3:15 PM
  • Hi Kelemvor,

    Sorry for the delay reply.

    If you want to run powershell script every 2 minutes, please try the script bwlow, which can run the .ps1 file every 30 seconds within 2 minutes:

    $timeout = new-timespan -Minutes 2
    $sw = [diagnostics.stopwatch]::StartNew()
    while ($sw.elapsed -lt $timeout){
      d:\2.ps1
      start-sleep -seconds 30
    }
    
    write-host "stop"
    I hope this helps.

    Wednesday, March 05, 2014 2:32 AM