如何从powershell获取与任务调度程序中的任务相关的触发器详细信息 [英] How to get trigger details associated with a task in task scheduler from powershell
问题描述
所以,基本上我需要获取与在任务调度程序中创建的任务相关联的触发器详细信息.
So, basically i need to get the trigger details associated with a task which is created in task scheduler.
所以,基本上我想要这些我将在此触发窗口中设置的信息,例如每日或每周重复任务持续时间以及持续时间等.
So, basically I want these information which i am going to be set in this trigger window such as its daily or weekly and repeat task duration as well as for a duration of etc.
现在可以得到以下信息.
Right now am able to get following information.
Name : LastTaskResult
Value : 0
CimType : UInt32
Flags : Property, ReadOnly, NotModified
IsValueModified : False
Name : NextRunTime
Value : 23-09-2015 11:26:56
CimType : DateTime
Flags : Property, ReadOnly, NotModified
IsValueModified : False
Name : NumberOfMissedRuns
Value : 0
CimType : UInt32
Flags : Property, ReadOnly, NotModified
IsValueModified : False
Name : TaskName
Value : test_Task
CimType : String
Flags : Property, Key, NotModified
IsValueModified : False
Name : TaskPath
Value :
CimType : String
Flags : Property, Key, NotModified, NullValue
IsValueModified : False
所以,基本上我的要求是我有两台服务器.一个是主要的,另一个是备份的.我已经在主服务器上安排了任务,并定期将这些任务镜像(robocopy)到备份服务器,这绝对正常.
So, basically my requirement is i have two servers. One is primary and other one is backup. I have scheduled the tasks in primary servers and periodically mirroring(robocopy) these tasks to backup server which works absolutely fine.
但是当我在操作选项卡中更改触发器详细信息或参数时,它不会出现在备份服务器中,因为我只是检查任务名称是否已经存在于备份服务器中,如果不是正在创建这些任务.
But when i change the trigger details or arguments in action tab it does not appear in backup server as i am just checking the task name is already present or not in backup server, if not am creating those tasks.
那么有什么方法可以检查有关触发器(每天或每周等,重复详细信息)或操作(脚本和参数详细信息)的详细信息,以便我可以在我的辅助服务器中相应地更新任务.
So is there any way to check the details regarding trigger(Daily or weekly etc, repetition details) or action(script and argument details) so that i can update the tasks accordingly in my seconadary server.
推荐答案
是不是你想要的东西是这样的:
Is something like this what you are after:
$task = Get-ScheduledTask -TaskName "Adobe Flash Player Updater"
$taskTrigger = $task.Triggers[0]
$taskTrigger
它应该给你类似的输出:
It should give you an output similar to:
Enabled : True
EndBoundary :
ExecutionTimeLimit :
Id :
Repetition : MSFT_TaskRepetitionPattern
StartBoundary : 2000-01-01T09:58:00+09:30
DaysInterval : 1
RandomDelay :
PSComputerName :
另一种方法,使用 ComObject 连接代替
Another way of doing this, using a ComObject connection instead
你可以这样做:
$taskService = New-Object -ComObject "Schedule.Service"
$taskService.Connect($env:COMPUTERNAME)
$rootTaskFolder = $taskService.GetFolder("\")
$task = $rootTaskFolder.GetTask("Adobe Flash Player Updater")
$task
这将返回任务的定义.然后,您可以使用 Compare-Object
来查看它在备份服务器上是否相同,如果不同,则导出/导入任务.
This will return the definition of the task. You could then use Compare-Object
to see if it's the same on the backup server, and if not, export/import the task.
如果您想解析 XML,您可以执行以下操作:
If you wanted to parse the XML you could do something like:
$parsedXML = [xml]$task.xml
然后您可以通过执行以下操作来比较触发器:
You can then compare triggers by doings something like:
Compare-Object -DifferenceObject $remoteServerParsedXML.GetElementsByTagName("Triggers") -ReferenceObject $parsedXML.GetElementsByTagName("Triggers")
这是否更接近您想要实现的目标?
Does this get closer to what you are trying to achieve?
这篇关于如何从powershell获取与任务调度程序中的任务相关的触发器详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!