List Schedule Tasks


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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CaussyRAuthor Commented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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"

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


$tasks = @()

$schedule = New-Object -ComObject "Schedule.Service"

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

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

# Output all tasks

Open in new window

CaussyRAuthor Commented:
The script worked !!!

But how would I read in a text file with a list of servers to check their scheduled task ?
you need to modify the connect command according to

and then just use the loop to process the previous code multiple times
(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
CaussyRAuthor Commented:
Thanks BeCraig. The script works fine but I am  trying to return only the root folder.

I have tried different attributes but no joy.
CaussyRAuthor Commented:
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
If I am reading you correctly I think this should do it:

(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
You can also select the members you want to your:
List of available members:

Next Run Time
Logon Mode
Last Run Time

Last Result

Task To Run
Start In
Scheduled Task State
Idle Time
Power Management
Run As User
Delete Task If Not Rescheduled
Stop Task If Runs X Hours and X Mins
Schedule Type
Start Time
Start Date
End Date
Repeat: Every
Repeat: Until: Time
Repeat: Until: Duration
Repeat: Stop If Still Running

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.