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
CaussyRAsked:
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.
0
brendanmeyerCommented:
runs very well though
0
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.

brendanmeyerCommented:
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

0
CaussyRAuthor Commented:
The script worked !!!

But how would I read in a text file with a list of servers to check their scheduled task ?
0
brendanmeyerCommented:
you need to modify the connect command according to http://msdn.microsoft.com/en-us/library/windows/desktop/aa383451(v=vs.85).aspx

and then just use the loop to process the previous code multiple times
0
becraigCommented:
(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
0
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.
0
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
0
becraigCommented:
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:
HostName
TaskName

Next Run Time
Status
Logon Mode
Last Run Time

Last Result

Author
Task To Run
Start In
Comment
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
Schedule Type
Start Time
Start Date
End Date
Days
Months
Repeat: Every
Repeat: Until: Time
Repeat: Until: Duration
Repeat: Stop If Still Running
0

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
Powershell

From novice to tech pro — start learning today.