Query Scheduled Tasks using Powershell


powershell1

This is a slightly modified version of Aaron Dodd’s script. There are some known issues with this script and has not been tested in many environments.

To use this powershell script place the script, the config file, and a list as a cvs file with the servers to be queried all in the same directory.

Save below as QueryScheduledTasks.ps1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$Description="Generate CSV of scheduled tasks in the environment"

#------------------------------------------------------------------------------
# Settings / Variables
#------------------------------------------------------------------------------
If (Test-Path "QueryScheduledTasks.config") {
    $cfg=[xml](get-content "QueryScheduledTasks.config")
} Else {
    Write-Host "!! ERROR !! - Config file not found"
    Write-Host "A file with the same name as this script, ending in .config, must exist in the same directory as this script."
    exit
}

$ServerList = Import-Csv $cfg.configuration.ServerList.name
$FinalReport=$cfg.configuration.FinalReport.name
$TempDir=$cfg.configuration.TempFolder.name
$TempReport=$TempDir + "\temp.csv"
$ErrorActionPreference=$cfg.configuration.ErrorAction.value
#------------------------------------------------------------------------------

 
#------------------------------------------------------------------------------
# Process tasks
#------------------------------------------------------------------------------
ForEach ($Server in $ServerList) {
  Write-Host $Server.name
  schtasks /QUERY /S $Server.Name /FO CSV /V | Where {$_.Length -gt 0} | Set-Content $TempReport
    $TempCsv += Import-Csv $TempReport
}
Remove-Item $TempReport
$TempCsv | Export-Csv $FinalReport -notype
#------------------------------------------------------------------------------

Right-Click and “Save As” on the link below to download the script.
Download QueryScheduledTasks.ps1

***NEXT***

Edit and save below as QueryScheduledTasks.config:

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <serverList name="servers.csv" />
    <finalReport name="d:\temp\ScheduledTasksReport.csv" />
    <tempFolder name="d:\temp" />
    <errorAction value="SilentlyContinue" />
</configuration>

Right-Click and “Save As” on the link below to download the script.
Download QueryScheduledTasks.config


Then create a list of servers, one per line, and save it as servers.csv. The first line should have the word “Name” (or what ever you choose) then all lines under that should be the list of servers to be queried. The Quotes are not required. I personally take them out.

Example of the server.csv would be….
Name
Server1
Server2
Server3




You can leave a response, or trackback from your own site.


Further Reading
    None Found




2 Responses to “Query Scheduled Tasks using Powershell”

  1. Sean says:

    This would be a really cool script if I could get it to work. I’m receiving the error message:

    Cannot convert value “System.Object[]” to type “System.Xml.XmlDocument”. Error: “Unexpected XML declaration. The XML de
    claration must be the first node in the document, and no white space characters are allowed to appear before it. Line 9
    , position 3.”
    At C:\scripts2\QueryScheduledTasks.ps1:7 char:15
    + $cfg=[xml] <<<< (get-content "QueryScheduledTasks.config")
    + CategoryInfo : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

    Import-Csv : Cannot bind argument to parameter 'Path' because it is null.
    At C:\scripts2\QueryScheduledTasks.ps1:14 char:25
    + $ServerList = Import-Csv <<<< $cfg.configuration.ServerList.name
    + CategoryInfo : InvalidData: (:) [Import-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ImportCsvCo
    mmand

    ERROR: Invalid syntax. Value expected for '/S'.
    Type "SCHTASKS /QUERY /?" for usage.
    Export-Csv : Cannot bind argument to parameter 'Path' because it is null.
    At C:\scripts2\QueryScheduledTasks.ps1:30 char:22
    + $TempCsv | Export-Csv <<<< $FinalReport -notype
    + CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
    mmand

  2. itjohn says:

    Strange. The last time I used this script it work great but now I am also getting errors. I will look into it and see if I can possibly find what changed. My guess off the top of my head would be some sort of windows update is behind the error but that only a guess. Thanks for letting me know.

    UPDATE: I made a few small changes and appears to be working for most of the servers in my my environment. Still not able to get all the scheduled tasks from every server.

Leave a Reply

Anti-Spam Quiz:

Twitter Delicious Facebook Digg Stumbleupon Favorites More
342 queries in 6.868 seconds.