Link to home
Start Free TrialLog in
Avatar of CaussyR
CaussyR

asked on

List Schedule Tasks

Anyone,

I am reading a list of servers in the $Server variable, but I am struggling to list the tasks :

$Server = Get-Content 'C:\temp\servers.txt'
$Command = "schtasks /query /tn 'TopArcadeHits' "

Foreach-object ($Server)
{
    Write-Host $Command
    invoke-expression -command $Command
}

Could anyone correct me - thanks
Avatar of Brendan M
Brendan M
Flag of Australia image

Avatar of CaussyR
CaussyR

ASKER

That does look overly complecated to list a few tasks.

When I run :

schtasks /query /tn 'TopArcadeHits'

Gives me the details I need.  There must be a way to use the above.
runs very well though
with status added

function getTasks($path) {
    $out = @()

    # Get root tasks
    $schedule.GetFolder($path).GetTasks(0) | % {
        $xml = [xml]$_.xml
        Switch($_.State) { 
            0 {$Status = "Unknown"} 
            1 {$Status = "Disabled"} 
            2 {$Status = "Queued"} 
            3 {$Status = "Ready"} 
            4 {$Status = "Running"} 
        }
        $out += New-Object psobject -Property @{
            "Name" = $_.Name
            "Path" = $_.Path
            "Status" = $Status
            "LastRunTime" = $_.LastRunTime
            "NextRunTime" = $_.NextRunTime
            "Actions" = ($xml.Task.Actions.Exec | % { "$($_.Command) $($_.Arguments)" }) -join "`n"
        }
        #$xml.Task.Actions.Exec
    }

    # Get tasks from subfolders
    $schedule.GetFolder($path).GetFolders(0) | % {
        $out += getTasks($_.Path)
    }

    #Output
    $out
}

$tasks = @()

$schedule = New-Object -ComObject "Schedule.Service"
$schedule.Connect() 

# Start inventory
$tasks += getTasks("\")

# Close com
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null
Remove-Variable schedule

# Output all tasks
$tasks

Open in new window

Avatar of CaussyR

ASKER

The script worked !!!

But how would I read in a text file with a list of servers to check their scheduled task ?
SOLUTION
Avatar of Brendan M
Brendan M
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(get-content C:\temp\servers.txt) | foreach-object {Schtasks.exe /Query /s $_ /V /FO CSV} | ConvertFrom-CSV | Where { $_.TaskName -eq "TopArcadeHits"} | Export-CSV TaskData.csv

Open in new window


This lets you pipe in your server names
Avatar of CaussyR

ASKER

Thanks BeCraig. The script works fine but I am  trying to return only the root folder.

I have tried different attributes but no joy.
Avatar of CaussyR

ASKER

I have tried :

(get-content C:\temp\servers.txt) | foreach-object {Schtasks.exe /Query /s $_ /V /FO CSV} | ConvertFrom-CSV | Where { $_.TaskName -eq '\'} | Export-CSV c:\temp\TaskData.csv

I just changed the taskname to "\", hoping it would return the root folder jobs
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial