【发布时间】: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