3

I want to use this Powershell script to automate some backups on Win7 machines. When run manually from the Powershell terminal the script runs as advertised and ends after a few seconds (it is a small backup).

The problem is when I try to automate the process with the Task Scheduler. When the time comes (or when I run the task manually) the script runs and performs the backup correctly. But it does not return to "Ready", even though I know it has ended. I have to stop the task manually each time.

Now, I have not waited to see what happens when the time comes for the task to run again, but I think it will not start a second time, because it is not "Ready".

What can I do about that?

UPDATE 1:

The Action I have chosen is "Start a program". In the "Program/script" box I have "powershell.exe" and as an argument I have the path to the script "*.ps1" file.

UPDATE 1:

I noticed that although the task does not return to "Ready", if I manually start it, it runs again.

  • What action do you have setup in the task? You might be running PowerShell with the script as a start-up script, with would run the script and then leave the CLI open indefinitely. – Chris S Nov 19 '14 at 15:10
  • @ChrisS Please see edit. – dsljanus Nov 19 '14 at 15:14
  • 1
    Press F5 in the scheduler window when you know it's done. Does the status refresh? – Colyn1337 Nov 19 '14 at 15:29
  • @Colyn1337 Refreshing does nothing. Also, see new edit. – dsljanus Nov 19 '14 at 15:33
  • 1
    the arg should be -File ".\pathtoscript.ps1" – Colyn1337 Nov 19 '14 at 15:34
7
+50

The last 2 lines of BackupScript.ps1 are

Write-Host "Press any key to close ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Comment these out (or delete them) and scheduled jobs should complete and exit cleanly. If you really want the pause at the end, replace the last line with something like this to wrap a timeout around it (each sleep is 1 second):

$counter = 0
while(!$Host.UI.RawUI.KeyAvailable -and ($counter++ -lt 600))
{
      [Threading.Thread]::Sleep( 1000 )
}
  • It does not work. Still getting the 'Running' thing perpetually. – dsljanus Nov 21 '14 at 8:10
  • That seems like it should sort it - have you both refreshed the jobs window and checked the logfile to be sure? – James Ruskin Nov 26 '14 at 17:40
  • @dsljanus Is the following option checked in the scheduled task properties: "If the running task does not end when requested, force it to stop"? – xXhRQ8sD2L7Z Nov 27 '14 at 4:44
  • @ST8Z6FR57ABE6A8RE9UF Yes, it is checked. – dsljanus Nov 27 '14 at 7:35
  • @JamesRuskin I have refreshed the windows, and the "running" state persists. In the log file it looks like all files have been copied but the test to see if they indeed have is not performed. Also, the message for the termination of the script has not been logged. Which means, the script does not terminate... – dsljanus Nov 27 '14 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.