如何获取使用 .NET Framework 触发 Windows 计划任务时显示的文本?

问题描述 投票:0回答:2

正如我今天了解到的,通过 PowerShell 中的

Get-ScheduledTask
命令,我可以检索有关计划任务的大量信息,以及根据以下模式打印出来的
Triggers
属性的触发器:

Enabled            : True
EndBoundary        : 
ExecutionTimeLimit : 
Id                 : 
Repetition         : MSFT_TaskRepetitionPattern
StartBoundary      : 2016-03-06T03:00:00
DaysOfWeek         : 16
RandomDelay        : 
WeeksInterval      : 1
PSComputerName     : 

但为了用户友好的输出消息,我希望收到您在 Windows 任务计划程序中观看触发器时可以看到的显示文本:

(来源:http://winaero.com/blog/wp-content/uploads/2016/08/Windows-10-the-list-of-triggers-600x344.png

那么如何通过 .NET Framework(在 PowerShell 中)提取“详细信息”文本,例如“任何用户登录时”?我无法找到

Get-ScheduledTask
返回类型的此类成员。 (我还查看了 .NET
TaskScheduler
,但不幸的是,它似乎只处理单个进程的内部结构。)

你知道有什么方法可以达到这个目的吗?或者我必须手动生成显示文本?

问候。

.net windows powershell scheduled-tasks
2个回答
0
投票

任务的详细信息以 XML 形式提供(返回的任务对象的 XLM 属性)。

$taskinfo = [xml](Get-ScheduledTask <taskname>).xml 
$taskinfo.task.triggers

应提供有关任务触发器的信息。


0
投票

我创建了一个 PowerShell 函数来为我们的模块处理这个问题。

function Get-WindowsScheduledTaskTriggerDescription
{
    <#
    .SYNOPSIS
    Converts the triggers of a scheduled task into human readable text
    #>
    param ([Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.Management.Infrastructure.CimInstance]$task)
    begin {
        $dayNames = @("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
    }
    process {
        if ($task.Triggers.Count -eq 0) {return}
        # Retrieve the first trigger (you can modify this if your task has multiple triggers)
        $triggers = $task.Triggers

        
        # Extract the friendly description
        foreach ($t in $triggers)
        {
            switch ($_.GetType().FullName) {
                "Microsoft.Management.Infrastructure.CimInstance" {

                    # Initialize an empty description
                    
                    $description = ""
                    if ($t.Id) {
                        $description += "ID=$($t.ID) "
                    }
                    
                    $description += "Enabled=$($t.Enabled) "

                    # Check if the trigger repeats
                    if ($t.Repetition) {
                        if ($t.Repetition.Interval) {
                            $description += "Repeats every";
                            $TimeSpan =  [System.Xml.XmlConvert]::ToTimeSpan($t.Repetition.Interval)
                            foreach ($p in @("Days","Hours","Minutes","Seconds"))
                            {
                                if ($TimeSpan.$p -gt 0) {
                                    #Write-Host " $($repetitionTimeSpan.$p) $p"
                                    $description += " $($TimeSpan.$p) $p"
                                }
                            }
                        }
    
                        if ($t.Repetition.Duration) {
                            $description += " for a duration of";
                            $TimeSpan =  [System.Xml.XmlConvert]::ToTimeSpan($t.Repetition.Duration)
                            foreach ($p in @("Days","Hours","Minutes","Seconds"))
                            {
                                if ($TimeSpan.$p -gt 0) {
                                    #Write-Host " $($TimeSpan.$p) $p"
                                    $description += " $($TimeSpan.$p) $p"
                                }
                            }
                        }
                        if ($repetitionInterval.TotalMinutes) {
                            $description += "Repeats every $($repetitionInterval.TotalMinutes) minutes"
                        }
                    }

                    # Check if the trigger has a specific days interval
                    if ($t.DaysInterval) {
    
                        $description += "Every $($t.DaysInterval) days"
                    }

                    # Check if the trigger specifies days of the week
                    if ($t.DaysOfWeek) {
                        $daysOfWeek = (1..7 | ForEach-Object {
                                            $dayIndex = ($_ + $t.DaysOfWeek - 1) % 7
                                            $dayNames[$dayIndex]
                                        } ) -join ','


                        $description += "Weekly on $daysOfWeek"
                    }

                    # Check if the trigger has a specific start time
                    if ($t.StartBoundary) {
                        $description += " at " + ([datetime]$t.StartBoundary).TimeOfDay.ToString()
                    }
                    
                    ## Output the final description
                    return $description
                }
                default {
                    # Fallback to displaying the trigger type
                    $_.GetType().Name
                }
            }
        }
        return $friendlyDescription
    }#process
}#function Get-WindowsScheduledTaskTriggerDescription

输出示例:

  • GoogleUpdateTaskMachineUA
    • Enabled=True 每 1 小时重复一次,持续 1 天每 1 天的 20:13:30
  • MicrosoftEdgeUpdateTaskMachineCore:
    • Enabled=True 每 1 天 06:12:00
  • MicrosoftEdgeUpdateTaskMachineUA:
    • Enabled=True 每 1 小时重复一次,持续 1 天每 1 天的 05:42:00
  • SCCM 客户端策略刷新:
    • Enabled=True 每周周六、周日、周一、周二、周三、周四、周五 03:00:00
  • SensorFramework-LogonTask-{d2bf6ee0-91cf-5437-e6a2-61286b7b7159}:
    • 启用=真
© www.soinside.com 2019 - 2024. All rights reserved.