How to get path from activities from scheduled tasks using ComObject

I am trying to retrieve information about active scheduled tasks on Windows 2003 Server using PowerShell.

I found a useful script online for connecting to scheduled tasks via a more modern version of Windows:

$sch = New-Object -ComObject("Schedule.Service")
$sch.Connect("10.22.8.54")
$tasks = $sch.GetFolder("\").GetTasks(0)

      

I am looking at the $ tasks members and there is a Definition property:

Definition            Property   ITaskDefinition Definition () {get}

      

I assign this to another variable and look at its elements and see that there is an Actions property:

Actions          Property   IActionCollection Actions () {get} {set}

      

I assign this to another variable and see that there is a Path property:

Path             Property   string Path () {get} {set}

      

Actual line of code:

$tasks | %{if ($_.Enabled) {$z=$_.Definition;$y=$z.actions;$y.Path}}

      

It doesn't produce anything. How do I get the path?

I'm very new to PowerShell, so not too technical with me.

This works and shows the path (along with other information):

$tasks | %{if ($_.Enabled) {$z=$_.Definition;$z.actions}}

      

But how do I get just the path?

+3


source to share


1 answer


The path is inside the xml data of the scheduled task. Extracting an element from it would be a structured approach to get what you need.

$tasks | Where-Object{$_.Enabled} | 
    ForEach-Object{
        ([xml]$_.xml).Task.Actions.Exec.Command
    }

      

We use the offer Where-Object

to view only permitted tasks. Then we apply the property xml

as [xml]

. We can now access the command from the actions section.



The results you get may depend on the requested OS. This may explain the difference in the results we both got.

You can easily get this in a computed property if you need.

+1


source







All Articles