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

use*_*725 8 powershell windows-server-2008 task-scheduler windows-server-2012

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

  • 姓名,
  • 地位,
  • 触发器,
  • 下次运行时间,
  • 上次运行时间,
  • 上次运行结果,
  • 作者,
  • 创建。

我可以打印出有关名称、下次运行时间和上次运行时间的信息,但在运行脚本时不会打印出其余的信息。

我已经对我的脚本有了一个小小的开始,并把字段放下了。

$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
}
}
Run Code Online (Sandbox Code Playgroud)

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

以下是我正在寻找的内容以及哪些字段未打印出数据的屏幕截图:

以下是我正在寻找的内容以及哪些字段未打印出数据的屏幕截图:

MDM*_*313 5

Server 2012 R2 和 Windows 8.1 有 Task Scheduler cmdlet,这个模块可以在 Windows 7 机器上复制和使用,想必它可能还需要最新的 .NET 和 Windows Management Framework。我能够禁用和重新启用计划任务,以及显示任务信息。目前,我不知道提供此信息或允许此控制的内置 cmdlet。

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

Get-ScheduledTask
Run Code Online (Sandbox Code Playgroud)

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

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     :
Run Code Online (Sandbox Code Playgroud)


pk.*_*pk. 5

这可以稍微清理一下(即映射 LastRunResult 代码)。如果您需要帮助,请告诉我。触发器有点困难,因为我不认为您在 GUI 中查看任务时看到的简单英语表示存在于 COM 对象中。我相信它必须从TriggerCollection存储在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
Run Code Online (Sandbox Code Playgroud)

  • 当然,`invoke-command -ComputerName REMOTEPC -ScriptBlock { --Paste Entire Script Block Here-- }` (3认同)