Powershell script scheduled task will not end
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 )
}
Related videos on Youtube
dlyk1988
Updated on September 18, 2022Comments
-
dlyk1988 over 1 year
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.
-
dlyk1988 over 9 years@ChrisS Please see edit.
-
Colyn1337 over 9 yearsPress F5 in the scheduler window when you know it's done. Does the status refresh?
-
dlyk1988 over 9 years@Colyn1337 Refreshing does nothing. Also, see new edit.
-
Colyn1337 over 9 yearsthe arg should be
-File ".\pathtoscript.ps1"
-
dlyk1988 over 9 years@Colyn1337 Maybe you are right about the correct syntax, but it works as it is right now. Also, I do not feel it would have any impact on my issue.
-
xXhRQ8sD2L7Z over 9 yearsLooks like the script is waiting for you to press a key at the end. See last 2 lines of code
-
austinian over 9 yearsWhen you run the task, do you see the backup start/finish? This'll tell us where the "running" is coming from. Something of note: the default behavior in powershell is to block scripts from running because the default execution policy is restricted. You should test that your powershell script will actually run in the context you're running it from. What user does the task run as? Is it running with elevated permissions? If you run the script using elevated permissions, you can use
-executionPolicy unrestricted -command ". '[pathtoscript.ps1]'"
as your argument without changing system policy.
-
-
dlyk1988 over 9 yearsIt does not work. Still getting the 'Running' thing perpetually.
-
James Ruskin over 9 yearsThat seems like it should sort it - have you both refreshed the jobs window and checked the logfile to be sure?
-
xXhRQ8sD2L7Z over 9 years@dsljanus Is the following option checked in the scheduled task properties: "If the running task does not end when requested, force it to stop"?
-
dlyk1988 over 9 years@ST8Z6FR57ABE6A8RE9UF Yes, it is checked.
-
dlyk1988 over 9 years@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...
-
James Ruskin over 9 yearsIs this line in the log? >> "Check if there are more than $Versions Directories in the Backupdir"