JFLarvoire / SysToolsLib

A library of Windows and Linux system management tools
Apache License 2.0
314 stars 93 forks source link

PSService - confusion #13

Closed hou8182 closed 6 years ago

hou8182 commented 6 years ago

I am confused on exactly which lines I am supposed to drop my code that I want executed when the service starts. I am testing having a powershell script file ran every 30 min using this service but can't figure out how to edit the code to do so. Can someone tell me which lines need to be edited to accomplish this?

Thanks

omrsafetyo commented 6 years ago

Essentially all of your logic should be inside the TimerTick condition of the switch statement inside the if($service) block:

"TimerTick" { # Example. Periodic event generated for this example
          Log "$scriptName -Service # Timer ticked"
 }

You would essentially replace Log $scriptname with your application code.

For me, I wrote a series of external functions, and I have this block process one of those functions, which checks a database for a queued item. If there is a queued item, it then executes the queued item:

       "TimerTick" {
          # WHAT TO DO ON EACH TICK
          # LETS CHECK THE DB FOR SCHEDULED JOBS
          $JobToRun = Get-JobExecutionTask
          if ($JobToRun -ne $null) {
            $jobOutput = Start-TaskFrameworkJob $JobToRun
            Log "$scriptName -Service # $jobOutput"         
          }
        }

All of the application code is inside the Get-JobExecutionTask function and the Start-TaskFrameworkJob function. This is just the control point into that application logic.

So this is set as a service, and really all this service is doing is sleeping and waiting for some event. And that is what the TimerTick block is. For every TimerTick, do something. In my case, check for a task, and if one exists, run it.

JFLarvoire commented 6 years ago

Thanks omrsafetyo for this clarification.