8

I'm using Powershell to talk to the Windows 7 task scheduler service via COM through the Task Scheduler 2.0 interfaces (e.g. ITaskDefinition). I want to pull out a particular trigger from the Triggers collection on ITaskDefinition. It appears that the proper way to extract a particular trigger is through the Item property, which is an indexed property.

My first try looks something like this:

$sched = New-Object -Com "Schedule.Service"
$sched.Connect()
$folder = $sched.GetFolder('\')
$task = $folder.GetTask("some task")
$triggers = $task.Definition.Triggers
$trigger = $triggers[0]

However, the last line fails with this message:

Unable to index into an object of type System.__ComObject.

I've tried some other variations on this theme, e.g. $triggers.Item(0), all with no luck. I'm guessing this has to do with $trigger being a COM object, because I think indexed properties work fine on other types.

Does anyone know the correct way to do this?

2 Answers 2

8

Item does work. You have to use Item() instead of Item[] and indices are 1-based.

$sched = New-Object -Com "Schedule.Service"
$sched.Connect()
$folder = $sched.GetFolder('\')
$task = $folder.GetTask("Update Bin Dir")
$triggers = $task.Definition.Triggers
$triggers.Item(1)

Type               : 2
Id                 : 67a9fad4-462f-43d9-ab71-6e9b781966e6
Repetition         : System.__ComObject
ExecutionTimeLimit : 
StartBoundary      : 2007-07-02T05:30:00
EndBoundary        : 
Enabled            : True
DaysInterval       : 1
RandomDelay        : 

Using an enumerator also works if you don't need to access by index:

foreach ($trigger in $triggers) { $trigger }
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, this seems to work also. Still hoping someone knows of a more direct way, though.
Yeah this is bugging me now. :-)
You know, along this same note the following seems to work: foreach($trigger in $task.Definition.Triggers) {$trigger}
Very nice; that's what I get for not reading the docs closely enough. Thanks for the effort in tracking this down.
Been doing .NET for so long, the 1-based index thing stumped me for a while. Thanks to x0n for help on that aspect of the problem.
|
3

On my system, it looks like there is only one trigger being returned for some tasks. You might try forcing it to return in an array.

$sched = New-Object -Com "Schedule.Service"
$sched.Connect()
$folder = $sched.GetFolder('\')
$task = $folder.GetTask("some task")
$triggers = @($task.Definition.Triggers)
$trigger = $triggers[0]

2 Comments

Cool, this seems like a good workaround. I'm hoping someone will know how to do it "properly", but this will get me going for now.
Awesome workaround. I was trying to cast it to an array, but totally forgot this is the right way to do it for objects like these. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.