正如我今天了解到的,通过 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
类,但不幸的是,它似乎只处理单个进程的内部结构。)
你知道有什么方法可以达到这个目的吗?或者我必须手动生成显示文本?
问候。
任务的详细信息以 XML 形式提供(返回的任务对象的 XLM 属性)。
$taskinfo = [xml](Get-ScheduledTask <taskname>).xml
$taskinfo.task.triggers
应提供有关任务触发器的信息。
我创建了一个 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
输出示例: