I created a .ps1 file that verify if a process is running and if not, it launches the program. This .ps1 file is launched every 10 minutes by the Windows Task Scheduler. I cannot use the Task Scheduler to launch directly the .exe, as for some reason it won't launch the GUI as well, which I absolutely need.
When I launch the .ps1 by right-clicking on it and selecting "Execute with PowerShell", everything works fine. If I copy/paste the content of my .ps1 within a PowerShell console, it launches my .exe whether it is an Administrator console or not.
When I launch the task in the Task Scheduler, the .ps1 runs, writes in the journal.txt file, but does not launch the executable. The task itself return the code 0x0, operation successful.
Here is the configuration of the Task Scheduler:
- General:
- Account used: User1 (my Windows account which have admin priviledges)
- Execute the task even if the user is not connected
- Execute with maximum priviledges
- Set for: Windows 10
- Trigger:
- Start the task: at the time programmed
- Every day at 00:00:00 repeting every 1 day
- Repeat the task every 10 minutes for 1 day
- Activated
- Actions
- Start a program
- Program/script: PowerShell
- Arguments: -ExecutionPolicy Bypass -File "C:\Users\User1\Desktop\Relancer Trust Zone VPN.ps1" Conditions
- Everything is unticked
- Parameters
- Autorise the execution of the task on demand
- Execute the task as soon as possible if a start is missed
- If the current task does not end on demand, force it to stop
- If the task is already running, the following rule apply: Stop the existing instance
Here is my .ps1 file:
$date = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$process = Get-Process trustzone_x64 -ErrorAction SilentlyContinue
if ($process -eq $Null) {
Start-Process "C:\Users\User1\Desktop\Relancer Trust zone VPN dc2024-03-20.exe"
"$date Trust Zone à l'arrêt. Démarrage du programme." | Out-File "C:\Program Files\Trust.Zone VPN Client\journalPowerShell.txt" -Append
} else {
"$date Trust Zone en fonctionnement." | Out-File "C:\Program Files\Trust.Zone VPN Client\journalPowerShell.txt" -Append
}
Location of the .ps1 file (copy/paste from the clipboard): C:\Users\User1\Desktop\Relancer Trust Zone VPN.ps1
Location of the .exe file (copy/paste from the clipboard): C:\Users\User1\Desktop\Relancer Trust zone VPN dc2024-03-20.exe
I also tried to add -NoExit as an argument in the Task Scheduler, with no success.
The GUI of your program can only appear if the current user is executing the program. The windows task sheduler has a different login user context and can't spawn the program in your logged in user context... Normally.
You can try to run it with superuser priviliges and spawn the process within a different user context i.e. within the current logged in user... This is some hacky stuff and considered bad practice. do you really need to spawn it from the task sheduler? Or can you change it to s single users context? Or can you just make a normal autostart service that runs on startup (in your user context) and check the current time and appear when needed? That would be more easy i suppose