Reputation: 8151
I'm trying to extract information about the active scheduled tasks on a Windows 2003 Server, using PowerShell.
I found a useful script online for connecting to the scheduled tasks via a more up to date version of Windows:
$sch = New-Object -ComObject("Schedule.Service")
$sch.Connect("10.22.8.54")
$tasks = $sch.GetFolder("\").GetTasks(0)
I look at the members of $tasks and there is a Definition property:
Definition Property ITaskDefinition Definition () {get}
I assign that to another variable and look at the members on that and see that there is an Actions property:
Actions Property IActionCollection Actions () {get} {set}
I assign that to another variable and see that there is a Path property:
Path Property string Path () {get} {set}
The actual line of code:
$tasks | %{if ($_.Enabled) {$z=$_.Definition;$y=$z.actions;$y.Path}}
This produces nothing. How do I get the path?
I'm very new to powershell, so don't get too technical with me.
This works and shows the path (along with other info):
$tasks | %{if ($_.Enabled) {$z=$_.Definition;$z.actions}}
But how do I get just the path?
Upvotes: 3
Views: 1806
Reputation: 46710
The path is located inside the xml data of the scheduled task. Extracting the element from there would be a structured approach to get what you need.
$tasks | Where-Object{$_.Enabled} |
ForEach-Object{
([xml]$_.xml).Task.Actions.Exec.Command
}
We use a Where-Object
clause to only look at enabled tasks. Then we cast the xml
property as [xml]
. Now we can access the command from within the actions section.
The results you get might be dependent on the OS you are querying. It might explain the difference in the results we were both getting.
You could easily get this in a calculated property if you needed to.
Upvotes: 1