Reputation: 55
I have a task that is not working properly and I would like to have it run in the foreground so I can see its progress. My task is a powershell script and it works perfectly when I manually run it but, some parts fail when it's ran by the task.
Thanks!
Upvotes: 4
Views: 6410
Reputation: 95
Below is a complete standalone working example Powershell script which launches a scheduled task which appears as a foreground console window. This script creates a scheduled task which runs the same example script every minute. It works well in Windows 10, Windows 11 in Powershell version 5.1.
This approach has an advantage that creation of scheduled tasks can be done entirely programmatically without the need of making any changes in Task Scheduler App manually.
The most important command of this script is schtasks /create
A lot of interesting information about this command along with many examples can be found in a website below
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/schtasks-create
For example using below command would launch a scheduled task which runs weekly only in Fridays, Saturdays and Sundays at 22:00 (10:00 PM) instead of every minute like in the script.
schtasks /create /tn $scheduled_task_name /sc weekly /st 22:00 /d FRI,SAT,SUN /rl 'Highest' /tr $scheduled_task_command;
In this example script, scheduled task is set to have elevated administrator privileges with option /rl 'Highest'
, which can be useful to run some scheduled tasks which require administrator rights. To run scheduled task without administrator privileges option /rl 'Limited'
can be used, which is default option.
-NoExit
parameter makes console window not quit after task finishes all its instructions. This parameter can be removed if console window has to disappear after task finishes all its instructions.
[string] $script_path = $MyInvocation.MyCommand.Definition; # extract the full path and name of this script
[string] $scheduled_task_name = "ScheduledTask";
#[string] $powershell_executable = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\Powershell.exe"; # full path to Powershell executable
[string] $powershell_executable = "Powershell.exe";
[string] $parameter_list = " -NoLogo -NoExit -ExecutionPolicy Bypass -File `'" + $script_path + "`'";
[string] $scheduled_task_command = $powershell_executable + $parameter_list;
$scheduled_task_exists = $null;
$scheduled_task_exists = Get-ScheduledTask -ErrorAction SilentlyContinue | Where-Object {$_.TaskName -ceq $scheduled_task_name };
if(-not ($scheduled_task_exists)) {
schtasks /create /tn $scheduled_task_name /sc minute /mo 1 /rl 'Highest' /tr $scheduled_task_command;
}
Write-Host "Foreground scheduled task script output"
Upvotes: 0
Reputation: 966
You did not state your OS, the exact location of the buttons change depending on OS.
For Windows 7 / 2008 R2:
Double click the task. Select the checkbox next to "Do not store password" in the task window and click ok. Right click the task and select run.
Upvotes: 0
Reputation: 29479
You might try Start-Transcript instead. That will capture all the output into a text file.
Upvotes: 4