title: windows scheduling of a power-shell script is unreliable
body: <p>I'm attempting to run an automated powershell script all day, everyday. But I'm have problems getting it to run consistently and reliably, the script itself runs fine, it's getting the windows scheduler to run it consistently that's the problem.</p>
  
  <p>The script is invoked every morning by windows scheduler at 1am as <code>powershell.exe</code> with the command arguments:</p>
  
  <pre><code>-windowstyle Normal  -NoExit -file "d:\work\PwrShellScripts\FlmToDb_010.ps1"
  </code></pre>
  
  <p>Once invoked, the script will run continuously until 11pm at night when it will exit.</p>
  
  <p>The script itself works reliably, but the scheduling only works nine times out of ten, once in a while it fails with the error:</p>
  
  <blockquote>
    <p>Task Scheduler did not launch task "\DailyFlm"  because instance "{aa18e048-d8b2-4e16-8737-fc7babbb609e}"  of the same task is already running.</p>
  </blockquote>
  
  <p>The question is, how to get the script to run reliably every day?</p>
  
  <p>Other info that may be relevant…....</p>
  
  <p>The arguments <code>-windowstyle Normal  -NoExit</code> mean that the powershell script runs in a command window (rather than as background process) and the window will remain open if the session ends.</p>
  
  <p>This is done for two reasons, firstly it provides a visual indication that the process is actually running, and secondly if the process fails, it allows the error message to be inspected. The powershell script doesn’t include any file logging, so running it in a command prompt also allows me to confirm that the previous days session made a clean exit when it stopped.</p>
  
  <p>One of the issues is that because the process works 90% of the time, if I make any tweaks I have to wait 10 days or more to confirm if they’ve really worked!</p>
  
  <p>I suspect that the issue may be related to the fact that the console remains open (<code>-NoExit</code>) when the script exits. Most of the time windows seems to recognise that although the console is still open, the associated script has exited.</p>
  
  <p>My guess is that occasionally it decides that since the console is open the process is still running. I'm unable to spot any difference between those occasions when the scheduling works fine and those when it doesn't.</p>
  
  <p>Any suggestions?</p>
  
  <p>Updates...
  The scheduler fails to start the job on average once every 10 days. I would prefer to keep the script running in the foreground, it makes monitoring it's progress so much easier, and makes it so obvious if it does fail.</p>