问题
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
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.
回答1:
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 :
Edit: Another way of doing this, using a ComObject connection instead
You could do it something like this:
$taskService = New-Object -ComObject "Schedule.Service"
$taskService.Connect($env:COMPUTERNAME)
$rootTaskFolder = $taskService.GetFolder("\")
$task = $rootTaskFolder.GetTask("Adobe Flash Player Updater")
$task
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.
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?
回答2:
I think what you need is "basically" ;) an export/import function of your tasks.
Here is a sample code :
#connect to scheduler of you master server
$sch = New-Object -ComObject("Schedule.Service")
$sch.connect("$computername")
$root=$sch.GetFolder("\")
$folder =$sch.GetFolder("\subfolder") #if you tasks are defined in a subfolder
#Export all tasks in the subfoder to $path folder in xml format
$folder.getTasks(0) | % {
$path="c:\temp\tasks\$($_.name).xml"
New-Item -ItemType file -Path $path
Set-Content -Path $path -Value $_.xml
}
#connect to scheduler of you backup server
$sch.connect("$backupcomputername")
$folder =$sch.GetFolder("\subfolder")
#import .xml from $task_path
$cred=get-credential # will ask for the credential of the user who run the tasks
Get-childItem -path $task_path -Filter *.xml | %{
$task_name = $_.Name.Replace('.xml', '')
$task_xml = Get-Content $_.FullName
$task = $sch.NewTask($null)
$task.XmlText = $task_xml
$folder.RegisterTaskDefinition($task_name, $task, 6, $cred.UserName, $cred.GetNetworkCredential().password, 1, $null)
}
来源:https://stackoverflow.com/questions/32731366/how-to-get-trigger-details-associated-with-a-task-in-task-scheduler-from-powersh