如何在PowerShell脚本中打印有关任务计划程序的信息?

我试图从PowerShell脚本中的本地计算机的Task Scheduler打印出信息,以便其他用户也可以打印出这些信息,而不必访问Task Scheduler 。 我需要脚本打印出来

  • 名称,
  • 状态,
  • 触发器,
  • 下一次运行时间,
  • 上次运行时间,
  • 上次运行结果,
  • 作者,
  • 创build。

我可以打印出有关名称,下次运行时间和上次运行时间的信息,但其他运行时不会打印出来。

我已经从我的脚本开始了一点点,并得到了领域。

 $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 } } 

任何帮助或build议,将不胜感激。

下面是我正在寻找什么字段没有打印出数据的截图:

下面是我正在寻找什么字段没有打印出数据的截图:

这可以清理一下(即映射LastRunResult代码)。 让我知道你是否需要帮助。 触发器有点困难,因为我不认为在COM对象中存在在GUI中查看任务时看到的简单英语表示。 我相信它必须从存储在RegisteredTask.Definition.TriggersTriggerCollection构build

 $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 

Server 2012 R2和Windows 8.1都有Task Scheduler cmdlet,而且这个模块可以在Windows 7机器上复制和使用,据推测大概需要最新的.NET和Windows Management Framework。 我可以禁用和重新启用计划任务,以及显示任务信息。 目前我不知道内置的cmdlet,给我这个信息或允许这个控制。

列出机器上的所有计划任务:

 Get-ScheduledTask 

您可以从任务序列对象中获取以下成员:

 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 :