MortenB
MortenB

Reputation: 3529

Get more info from Scheduled Tasks in powershell

Is there a way to view all ScheduledTask properties from PowerShell without having to open up the not so user friendly taskschd.msc:

I manage to create the job fine with New-ScheduledTask and Register-ScheduledTask, but I'm unable to view the parameters like Action, Trigger, Principal and Settings, that I used in New-ScheduledTask

Below is what I manage to get:

Get-ScheduledTask -TaskName "test22 BAM-AT" -Verbose

TaskPath                                       TaskName                          State
--------                                       --------                          -----
\                                              test22 BAM-AT                     Ready


Get-ScheduledTaskInfo -TaskName "test22 BAM-AT" -Verbose


LastRunTime        : 2/23/2021 1:44:44 PM
LastTaskResult     : 0
NextRunTime        : 2/23/2021 4:00:00 PM
NumberOfMissedRuns : 0
TaskName           : test22 BAM-AT
TaskPath           :
PSComputerName     :

Upvotes: 2

Views: 27890

Answers (4)

Daniel Wiltshire
Daniel Wiltshire

Reputation: 59

There are several ways as below..

Local Machine;

 $task = Get-ScheduledTask -TaskName 'My Task'
 $task.Actions.Execute

Remote Machine;

$task = Get-ScheduledTask -TaskName 'My Task' -CimSession 'REMOTE_SERVER'
$task.Actions.Execute

Lots of Remote Machines;

    $servers = Get-ADComputer -f * -Properties OperatingSystem | Where OperatingSystem -like '*Server*'

$TaskReport = ForEach($s in $servers){
    IF(Test-Connection -ComputerName $s.name -Count 3 -ErrorAction SilentlyContinue){
    $task = Get-ScheduledTask -CimSession $s.Name  -ErrorAction Ignore | Where {$_.Author -Like 'MYDOMAIN*'}
    IF($Task){
    ForEach($t in $task){
            $info = Get-ScheduledTask -CimSession $s.Name -TaskPath $t.TaskPath -TaskName $t.TaskName | Get-ScheduledTaskInfo -ErrorAction Ignore
            $MyTaskObj = [PsCustomObject]@{
                TaskName = $t.TaskName
                State = $t.State
                Author = $t.Author
                Action = $t.Actions.execute
                LastRunTime = $info.LastRunTime
                LastTaskResult = $info.LastTaskResult
                NextRunTime = $info.NextRunTime
                Server = $t.PsComputerName
            }
        $MyTaskObj
        } 
    } 
}
}
$TaskReport | Export-Csv .\Desktop\tasks.csv -nti

Upvotes: 1

Looong oneliner that gives one some info :)

(Get-ScheduledTask -TaskName "Test_Script") | select TaskName,TaskPath,State,@{n='LastRunTime';e={($_ | Get-ScheduledTaskInfo).LastRunTime}},@{n='NextRunTime';e={($_ | Get-ScheduledTaskInfo).NextRunTime}},@{n='Triggers';e={$_.Triggers.StartBoundary}},@{n='DaysOfWeek';e={$_.Triggers.DaysOfWeek}},@{n='WeeksIntervall';e={$_.Triggers.WeeksInterval}},@{n='Execute';e={$_.Actions.Execute}},@{n='Arguments';e={$_.Actions.Arguments}}

TaskName : Test_Script

TaskPath : \Scripts\

State : Ready

LastRunTime : 2024-09-02 06:30:30

NextRunTime : 2024-09-09 06:30:30

Triggers : 2023-08-30T06:30:00

DaysOfWeek : 2

WeeksIntervall : 1

Execute : powershell

Arguments : -file "c:\ps\Test_Script.ps1"

Upvotes: 1

GaryG
GaryG

Reputation: 13

If you get a list of all the taskNames you're interested in, then Get the Property set for one, you can get more info than you want:

$names = Get-ScheduledTask -TaskPath \ | select -exp TaskName

$TaskProps = Get-ScheduledTask -TaskName $names[0] | gm | ? { $_.memberType -EQ "Property" | select -exp Name}

foreach ($name in $names) {
    write $name
    $TaskProps | % { (get-ScheduledTask -TaskName $name).$_ }
}

Upvotes: 0

Steven
Steven

Reputation: 7057

The Cim base ScheduledTasks module doesn't expose that level of detail up front. However, you can use another of it's cmdlets Export-ScheduledTask to get the XML task definition and parse it like an object.

Example:

$TaskXML = [XML]((Get-ScheduledTask)[0] |Export-ScheduledTask)
$TaskXML.Task.Triggers

Obviously you may have to poke around to extract the information you want etc...

Export-ScheduledTask returns all the XML you would need to import the task on another system, so it should have all the details, it's just a matter of extracting the XML.

Update Addressing Comments:

I apparently didn't realize there's more information returned and accessible from Get-ScheduledTask. However, Select-Object (select * above) isn't needed to access that information. Doing so will dehydrate the returned object, which may affect secondary use of it. The objects returned after Select are [PSCustomObjects] as opposed to a typical [CimInstance].

Thanks to @CraftyB's comments, here are some examples without the unnecessary Select-Object:

(ScheduledTask -TaskName "test22 BAM-AT").Actions

OR:

(ScheduledTask -TaskName "test22 BAM-AT").Triggers

Reference properties as needed.

Without more work, it is unclear to me if there are advantages or disadvantages to exporting the XML versus just referencing the properties. It's possible information is available in one approach that isn't in another.

Upvotes: 3

Related Questions