Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access an indexed property on a COM object from Powershell

Tags:

powershell

com

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?

like image 241
Charlie Avatar asked Nov 03 '09 17:11

Charlie


People also ask

How do I get the properties of an object in PowerShell?

To get the properties of an object, use the Get-Member cmdlet. For example, to get the properties of a FileInfo object, use the Get-ChildItem cmdlet to get the FileInfo object that represents a file. Then, use a pipeline operator ( | ) to send the FileInfo object to Get-Member .

How do I use ExpandProperty in PowerShell?

Run the Get-Process cmdlet below to retrieve all the modules the explorer process uses. Turn on the -ExpandProperty switch to display the properties of the modules. The output on your screen should resemble the following screenshot, automatically truncated to display only three properties.

How do you select an object in PowerShell?

The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters.

What does :: mean in PowerShell?

From the about_Operators help topic: :: Static member operator Calls the static properties operator and methods of a .NET Framework class. To find the static properties and methods of an object, use the Static parameter of the Get-Member cmdlet. [


1 Answers

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 }
like image 192
Keith Hill Avatar answered Oct 04 '22 22:10

Keith Hill