【问题标题】:Show Task Scheduler jobs that contain 'Actions'显示包含“操作”的任务计划程序作业
【发布时间】:2018-03-13 14:42:26
【问题描述】:

我想查看“操作”不为空的所有任务计划程序作业。

我从这个链接得到了初始代码: Task Scheduler in powershell script

例如,我不想看到名称为“优化开始菜单缓存文件”的计划任务,因为“操作”为空白。

$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"}}
        "Actions" = ($xml.Task.Actions.Exec | % { "$($_.Command) $($_.Arguments)" }) -join "`n"
        "NextRunTime" = $_.NextRunTime
        "LastRunTime" = $_.LastRunTime
        "LastRunResult" = $_.LastTaskResult
        "Author" = $xml.Task.Principals.Principal.UserId
        "Created" = $xml.Task.RegistrationInfo.Date
    }
}

$out | fl Name,Status,Actions,NextRuNTime,LastRunTime,LastRunResult,Author,Created | Where-Object {($_.Actions -ne "" -and $_.Actions -eq "*powershell*" -or $_.Actions -ne $null)}

【问题讨论】:

  • ... | Where-Object { $_.Actions.Execute -ne $null }?
  • @AnsgarWiechers 我没有得到任何结果。
  • 我忽略了您正在使用 COM 接口。试试... | Where-Object { $_.Actions.Exec -ne $null }。
  • @AnsgarWiechers 同样的事情。没有结果。
  • 对不起,我真的应该停止评论,同时被其他事情分心。您的问题是由Format-List (fl) 引起的。该 cmdlet 纯粹用于输出格式,不应用于其他任何用途。 $out | Where-Object {$_.Actions} 应该做你想做的事。

标签: powershell scheduled-tasks


【解决方案1】:

这适用于比 Windows 7 更新的任何东西。

get-scheduledtask |Where-Object {$_.actions -NE $Null -OR $_.actions -NE "" }

【讨论】:

  • 那行不通。 “优化开始菜单缓存”任务仍然出现。
【解决方案2】:

我最终选择了以下内容,但是 where 语句中不能有多个子句。

$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"}}
        "Actions" = ($xml.Task.Actions.Exec | % { "$($_.Command) $($_.Arguments)" }) -join "`n"
        "NextRunTime" = $_.NextRunTime
        "LastRunTime" = $_.LastRunTime
        "LastRunResult" = $_.LastTaskResult
        "Author" = $xml.Task.Principals.Principal.UserId
        "Created" = $xml.Task.RegistrationInfo.Date
    }
}

$out |Where Actions -like "*powershell*"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2014-11-12
    • 2016-09-05
    相关资源
    最近更新 更多