Cómo mantener un script de PowerShell ejecutándose las 24 horas del día, los 7 días de la semana

I have a PowerShell script that needs to be restarted when it dies for whatever reason, be it a crash, a self exit or after a system reboot...

How can I, from a bat or another powershell script, see to it that if it is not running, it will be started again...

i.e. how can I find out if it is already running from another script?

I know I can make one powershell script start the active one and simply have it loop a new start as long as it doesnt exit with a specific error... but then THAT scripts need to be seen to :D So we are back to the original quesiton, how do I keep THAT script running 24/7?

  $date = Get-Date -format "yyyy-MM-ddTHH:mm:ss"
  "$($date) Repeat : Restarting Worker, LastExitCode $($LastExitCode)." | Out-File D:\IDM\Worker\Worker.LOG -width 180 -append
  powershell -File "D:\IDM\Scripts\Worker.ps1"
  sleep 10
while ($LastExitCode -ne $null)

preguntado el 31 de julio de 12 a las 13:07

Consider powershell workflows. -

1 Respuestas

I would just use scheduled tasks. There are plenty of options in there to help you do what you want. You can run the script every five minutes and have it do enough loops to take up that time and quit:

$now = Get-Date
while ($now.AddMinutes(5) -lt (Get-Date)){

Or you could even have it write a flag file every time the loop works and have any new process check that file to see if there hasn't been activity on it. If there's been no activity:

$workFlag = Get-Item C:\work.flg
$cutOff = (Get-Date).AddMinutes(-5)
if ($workFlag.LastWriteTime -gt $cutOff){
    New-Item -force -path C:\work.flg
    ...work loop..

Respondido 31 Jul 12, 14:07

The Scheduled task is what I do today, but there I hit a small problem... If the task is already running, I dont want duplicate tasks working the same file so i checkmarked the "dont start if already running" box. That generates an event every time, no real trouble but very annoying... I like the file write idea though, since I keep a logfile that woule be a good one to test... - johan andersson

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.