8

I am trying to print out information from the Task Scheduler from the local computer in a PowerShell script so other users can print out this information as well and not have to access the Task Scheduler. I need the script to print out

  • name,
  • status,
  • triggers,
  • next run time,
  • last run time,
  • last run result,
  • author,
  • created.

I can print out the information about the name, next run time, and last run time, but the rest won't print out when I run the script.

I have already got a little start on my script and got the fields down.

$schedule = new-object -com("Schedule.Service") 
$schedule.connect() 
$tasks = $schedule.getfolder("\").gettasks(0)

$tasks | select Name,Status,Triggers,NextRunTime,LastRunTime,LastRunResult,Author,Created | ft

foreach ($t in $tasks)
{
foreach ($a in $t.Actions)
{
$a.Path
}
}

Any help or suggestions would be appreciated.

Below is a screenshot with what i am looking for and what fields arent printing out data:

Below is a screenshot with what i am looking for and what fields arent printing out data:

6
  • 1
    It's blank b/c it's not available in the object. If you run $Tasks | Get-Member what do you get?
    – MDMoore313
    Jun 12, 2014 at 16:01
  • 1
    Look at what $tasks contains. It doesn't have all that you want to output: Name, Path, State, Enabled, LastRunTime, LastTaskResult, NumberOfMissedRuns, NextRunTime, Definition, Xml.
    – slybloty
    Jun 12, 2014 at 16:07
  • I've updated my answer. Still doesn't have what you need?
    – MDMoore313
    Jun 12, 2014 at 16:55
  • i see now with the get-members command. i didnt realize i wasnt calling the specific fields i was looking for data in. I am new to powershell scripting and thought it was a pre-determined field that all i had to do was call on it. So with the get-member command, in order for me to grab author, created, and what ever else i need...i need to specific those in my $task variable?
    – user38725
    Jun 12, 2014 at 17:12
  • by specifying "those" i mean for example : i need triggers so i do a: get.Triggers to pull the triggers for that task?
    – user38725
    Jun 12, 2014 at 17:19

2 Answers 2

5

This could be cleaned up a bit (i.e. mapping LastRunResult codes). Let me know if you need help. Triggers are a bit more difficult since I don't think the plain English representation you see when viewing a task in the GUI exists in the COM object. I believe it would have to be built from the TriggerCollection stored in RegisteredTask.Definition.Triggers

$sched = New-Object -Com "Schedule.Service"
$sched.Connect()
$out = @()
$sched.GetFolder("\").GetTasks(0) | % {
    $xml = [xml]$_.xml
    $out += New-Object psobject -Property @{
        "Name" = $_.Name
        "Status" = switch($_.State) {0 {"Unknown"} 1 {"Disabled"} 2 {"Queued"} 3 {"Ready"} 4 {"Running"}}
        "NextRunTime" = $_.NextRunTime
        "LastRunTime" = $_.LastRunTime
        "LastRunResult" = $_.LastTaskResult
        "Author" = $xml.Task.Principals.Principal.UserId
        "Created" = $xml.Task.RegistrationInfo.Date
    }
}

$out | fl Name,Status,NextRuNTime,LastRunTime,LastRunResult,Author,Created
5
  • This did exactly what i needed, only with the exception that triggers doesnt get called but i can research that some more and find something about that. Thank you very much!
    – user38725
    Jun 12, 2014 at 17:22
  • 1
    How do i use this to connect remotly to another server? (i am still on the same domain) use the invoke-command?
    – user38725
    Jun 12, 2014 at 18:50
  • 3
    Sure, invoke-command -ComputerName REMOTEPC -ScriptBlock { --Paste Entire Script Block Here-- }
    – pk.
    Jun 12, 2014 at 18:55
  • I added the following under "Status": "Actions" = ($xml.Task.Actions.Exec | % { "$($_.Command) $($_.Arguments)" }) -join "`n" In the $out statement, how do I only show those Actions that are not blank?
    – Jeremy F.
    Mar 12, 2018 at 21:04
  • You could do something like foreach ($o in $out) {$o.PSObject.Properties | ?{$_.Value -ne $null} | ft Name,Value} to only list properties that aren't null. Output looks a bit funky, but it should work.
    – pk.
    Mar 14, 2018 at 3:07
5

Server 2012 R2 and Windows 8.1 have Task Scheduler cmdlets, and this module can be copied and used on a Windows 7 machine, presumably it probably needs the latest .NET and the Windows Management Framework as well. I'm able to disable and re-enable scheduled tasks, as well as display task information. Currently I don't know of built-in cmdlets that give me this information or allow this control.

To list all the scheduled tasks on the machine:

Get-ScheduledTask

You can get the following members from the task sequence object:

PS C:\BigHomie> $A = Get-ScheduledTask | select -First 1

PS C:\BigHomie> $A

       TypeName: Microsoft.Management.Infrastructure.CimInstance#Root/Microsoft/Windows/TaskScheduler/MSFT_ScheduledTask

Name                      MemberType     Definition
----                      ----------     ----------
Clone                     Method         System.Object ICloneable.Clone()
Dispose                   Method         void Dispose(), void IDisposable.Dispose()
Equals                    Method         bool Equals(System.Object obj)
GetCimSessionComputerName Method         string GetCimSessionComputerName()
GetCimSessionInstanceId   Method         guid GetCimSessionInstanceId()
GetHashCode               Method         int GetHashCode()
GetObjectData             Method         void GetObjectData(System.Runtime.Serialization.SerializationInfo info, Sys...
GetType                   Method         type GetType()
ToString                  Method         string ToString()
Actions                   Property       CimInstance#InstanceArray Actions {get;set;}
Author                    Property       string Author {get;set;}
Date                      Property       string Date {get;set;}
Description               Property       string Description {get;set;}
Documentation             Property       string Documentation {get;set;}
Principal                 Property       CimInstance#Instance Principal {get;set;}
PSComputerName            Property       string PSComputerName {get;}
SecurityDescriptor        Property       string SecurityDescriptor {get;set;}
Settings                  Property       CimInstance#Instance Settings {get;set;}
Source                    Property       string Source {get;set;}
TaskName                  Property       string TaskName {get;}
TaskPath                  Property       string TaskPath {get;}
Triggers                  Property       CimInstance#InstanceArray Triggers {get;set;}
URI                       Property       string URI {get;}
Version                   Property       string Version {get;set;}
State                     ScriptProperty System.Object State {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes...


PS C:\BigHomie> $A.Triggers


Enabled            : True
EndBoundary        :
ExecutionTimeLimit :
Id                 :
Repetition         : MSFT_TaskRepetitionPattern
StartBoundary      :
PSComputerName     :
6
  • thank you for your response but i am looking more into the service "Task Scheduler" and the current tasks that are in the "Task Scheduler" service. I'll add a screen shot shortly to show what i need help with!
    – user38725
    Jun 12, 2014 at 15:55
  • Jimmyboy, how's that different than what I've given you?
    – MDMoore313
    Jun 12, 2014 at 16:06
  • Are you using certain modules which don't run as default?
    – slybloty
    Jun 12, 2014 at 16:09
  • 1
    Reason I'm asking is because I ran it on 2012R2 and it behaves totally different than what you've depicted in your post.
    – slybloty
    Jun 12, 2014 at 16:32
  • 1
    @slybloty yugh, what I'm using is actually a separate module from pre W8 days. Well, this is embarrassing, but that's what happens when you leave a project for several months and pick it up b/c someone asked a question on SF ;) I'll be editing this answer, but the good news is the 2k12 module actually gives more information, and the -Recurse parameter isn't needed. OP Should ideally use that instead of reinventing the wheel.
    – MDMoore313
    Jun 12, 2014 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .