Issue with Task Scheduler running one program

Nora Carlson 20 Reputation points
2023-12-16T02:21:31.18+00:00

I have 4 computers all with the same microsoft account. Though I set up C1 and C2 first, I set up C3 and C4 the same way.

I have set up a few tasks on Task Scheduler to run on all 4 computers.

  1. Open and start recording OBS studio on a repeating schedule every day.
  2. Kill OBS studio on a repeating schedule every day.
  3. Play a specific audio file in Media Player at a specific time repeating weekly.

In all computers both task 1 and 2 run just fine whether I can see the screen or not. On two computers task 3 runs just fine. On computers 3 and 4, the audio file will only start playing if I access the computers after the Task Scheduler starts (as they are not set up in an office with a screen I access them via team viewer and have to enter my password). If I am logged in via team viewer before the task triggers, it triggers just fine. I have sleep turned off so I am always logged in. I have tried killing MP before the next round of playing a file but that doesn't help. I cant figure out how to fix this task so it runs like it does in the other two computers and like task 1 and 2 run (with me not accessing the computer). Also this was running fine in May on all computers. I did make new tasks, but have also exported some of the tasks, imported and edited them for the correct function, and that didn't work either. Any help would be amazing.

Information I forgot to add initially: I turned on the history of each task in Task Scheduler, and it always shows 6 tasks as completed: Task triggered on schedule, Created task process, Task started, Action started, Action completed, Task completed. All of these are time stamped when they are supposed to trigger.

Additionally I use the 'start program' setting in Task Scheduler and call the following .bat files:

Open OBS and start recording:

del "C:\Users\norav\AppData\Roaming\obs-studio\safe_mode"
cd "C:\Program Files\obs-studio\bin\64bit\"
"C:\Program Files\obs-studio\bin\64bit\obs64.exe" --startrecording -m


Kill OBS

taskkill /f /im obs64.exe

Start playing a specific audio file


C:\Users\norav\OneDrive\Desktop>cd "C:\Users\norav\Music"
"C:\Users\norav\Music\Silence_6H.wav"

Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,401 Reputation points
    2023-12-17T21:15:52.05+00:00

    When you use this option, you don't have to enter a password when you save the task. But it only will run when you are logged on. And if the task launches a GUI program, then its window will be visible to the desktop user.

    User's image

    If you are not logged on, then the task won't launch.

    User's image

    If you set this option, then the task should run as soon as you log on to the desktop.

    User's image

    If you want the task to run even if you are not logged on, you need to set this option. When you save the task it will prompt you for a password. If the task runs a GUI program, the window will NOT be visible. That's why it's best to run command line based programs, because if a GUI displays a message box, there is no way to "click on OK to continue".

    User's image

    I tested playing a .wav file and found that just putting the file name in the bat file launches the GUI Media Player which plays it (on my Win11). But MP does not terminate. If the task runs "while logged on", that's not a problem because the desktop user can click on the X to close the window.

    But if the task runs whether the user is logged on or not, then you would need to use taskkill.exe to make Media Player terminate.

    I found a Powershell script to play a .wav/.mp3 file. I put your file name in it.

    Add-Type -AssemblyName presentationCore
    $mediaPlayer = New-Object system.windows.media.mediaplayer
    $mediaPlayer.open("C:\Users\norav\Music\Silence_6H.wav")
    $mediaPlayer.Play()
    "Playing wav"
    # wait for media player to finish  
    while ($mediaPlayer.Position.TotalMilliseconds -ne $mediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds) {
        start-sleep -Seconds 2
    }
    "Its finished."
    
    

    So if you save that as C:\Users\norav\Desktop\PlayWav.ps1, then in your .bat file you would call this.

    powershell.exe -executionpolicy bypass -file C:\Users\norav\Desktop\PlayWav.ps1
    

    Hope that helps you.


2 additional answers

Sort by: Most helpful
  1. MotoX80 36,401 Reputation points
    2023-12-16T15:05:18.3366667+00:00

    Configure the scheduled tasks to capture program output so that you have a log file to examine when something doesn't work.

    Create a bat file that executes your program. Echo %date% %time% as the first and last line the .bat so that you know when the task starts and ends.

    Add other troubleshooting steps, like running tasklist.exe to check to see if a program is running, to understand what is happening on each pc when the task runs.

    @echo %date% - %time% MyScript is starting.
    SomeProgram.exe  
    @echo %date% - %time% MyScript is ending. SomeProgram RC=%errorlevel% 
    

    Then change the scheduled task to execute program "cmd.exe".

    In the arguments field set it like this example:

    /c C:\Scripts\MyScript.bat  1>>"C:\Scripts\Logs\MyScript-%date:~10,4%-%date:~4,2%%date:~7,2%.log" 2>&1
    

    That will create a daily log file of all executions of the task. It will capture stdout and stderr for programs that get called. Be sure to include the double quotes around the file name because of the space in the hours when there is only a single digit.


  2. MotoX80 36,401 Reputation points
    2023-12-16T19:56:12.8533333+00:00

    You want to modify your bat files so that they look like this. All I've really done is to add a few statements to verify (via dir commands) that we are in the folder that we expect to be in and that the files we expect to be there are in fact there. By displaying the TOD you will be able to know when a certain command was executed. In case something hangs.

    @echo %date% - %time%  MyScript is starting.
    @echo %date% - %time%  Looking for safe mode 
    dir "C:\Users\norav\AppData\Roaming\obs-studio\safe_mode"
    @echo %date% - %time%  Issuing delete.  
    del "C:\Users\norav\AppData\Roaming\obs-studio\safe_mode"
    @echo %date% - %time%  Verifying that it's gone.  
    dir "C:\Users\norav\AppData\Roaming\obs-studio\safe_mode"
    @echo %date% - %time%  Verifying and starting obs64. 
    cd "C:\Program Files\obs-studio\bin\64bit\"
    dir obs64.exe 
    obs64.exe --startrecording -m
    @echo %date% - %time%  Obs64 has ended RC=%errorlevel% 
    @echo %date% - %time%  Looking for a running Obs64
    tasklist | findstr -i obs64 
    taskkill /f /im obs64.exe
    @echo %date% - %time%  Verifying that obs64 went away. 
    tasklist | findstr -i obs64 
    @echo %date% - %time%  Start playing a specific audio file
    cd "C:\Users\norav\Music"
    dir silence* 
    Silence_6H.wav
    @echo %date% - %time%  MyScript ending. 
    

    I would not put your log files into OneDrive. Create a separate folder on the C drive on each pc. Keep it simple, like C:\Logs. You will probably need to add "users full control" to the security permissions.

    A task might look like this. It runs the bat file on your desktop but logs to c:\Logs. If the task runs but the programs don't do what you expect, then check the log file. Add more troubleshooting to the bat if needed to figure out what it's doing.

    User's image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.