Cheashirecat
Cheashirecat

Reputation: 21

Windows Task Scheduler - is it possible to distinguish manual and time-based triggered run of task?

I have a task in my Task Scheduler, which simply runs some Powershell script every night. Sometimes I need to manually run this task, and it would be great if inside my script it was possible to determine, whether this was a manual or trigger-based run.

Is there any way to do it?

Upvotes: 2

Views: 2028

Answers (2)

Raf
Raf

Reputation: 10107

$t = Get-ScheduledTask "YourTaskName"

if($t.State.ToString() -eq "Running"){
    # scheduled task code
}
else{
    # manual run code
}

Upvotes: 0

mjolinor
mjolinor

Reputation: 68273

You could check the schduler event log:

get-winevent Microsoft-Windows-TaskScheduler/Operational -MaxEvents 3

The 100 ID events will have the user name that launched the task, along with an instance ID (guid) for that instance. That will be followed by a 200 event that has the instance ID and the process ID if you want to match that to $PID in the script to verify it is current process.

Upvotes: 3

Related Questions