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
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
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.
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
ASKER
The script worked !!!
But how would I read in a text file with a list of servers to check their scheduled task ?
But how would I read in a text file with a list of servers to check their scheduled task ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(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
This lets you pipe in your server names
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.
I have tried different attributes but no joy.
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
(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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://stackoverflow.com/questions/15439542/how-to-use-powershell-to-inventory-scheduled-tasks