Link to home
Create AccountLog in
Avatar of shong1997
shong1997

asked on

Get Scheduled Task Info from Remote Servers

I would like to get scheduled tasks on the remote servers.  Most of the remote servers are running Windows 2003 and the computer which I run the query is a Windows 2008 R2 server.  I would think this can be done by calling schtasks.exe /query /s $Computer /V /FO CSV from a Powershell script.  When I used a Powershell script from http://community.spiceworks.com/scripts/show/1586-get-scheduled-task-info, I received an error "The system cannot find the file specified".  Does anyone have a sample Powershell script with Schtasks.exe /Query I can reference?
Avatar of becraig
becraig
Flag of United States of America image

I do not have one but here is a simple way to go about this:


Simply do a gci for your serverlist

e.g.

(gci serverlist.txt) | foreach-object {Schtasks.exe /query /s $_  /v"}

You can then do whatever you need to with the output.
Avatar of shong1997
shong1997

ASKER

I received "the network path was not found" message.  In my lab, I created a servers.txt file off the root of C:\. Two lab servers are listed one per line.  The command I executed is as follows:

(gci servers.txt) | foreach-object {Schtasks.exe /query /s $_  /v"}
sorry typo

(gc serverlist) | foreach-object {Schtasks.exe /query /s $_  /v}
Here is what works in my lab:


PS C:\> (get-content servers.txt) | foreach-object {Schtasks.exe /Query /s $_ /V /FO CSV} | ConvertFrom-CSV |Where { $_.TaskName -ne "TaskName"} | Export-CSV c:\test.csv

There are servers on my list that are not available or the credentials I use may not allow the script to access the computer.  Can you show me the ways to add error handling to the script?
What specific errors do you want to catch and what actions do you want to perform.

Here is a good how to on error handling.

http://www.maxtblog.com/2012/07/using-powershell-error-variable/
The output CSV file captures all the properties of scheduled tasks on the remote computers.  As mentioned, there will be servers on the list that are either unavailable or the credentials used by the script cannot log on to the remote server.  The above Powershell script I ran in the lab only captures the remote servers that the script can authenticate but it skips over the ones that are either unavailable or Access denied.  Does anyone have a sample Powershell script to capture these errors that I can reference?  It does not have to be Scheduled Tasks, but the logic to run a task on remote servers, capture unavailable servers or the ones with Access denied.
you can use

if (!(Test-connection))
{$computer is unavailable}

To handle servers that are not pingable


you can also write

$scoutput = Schtasks.exe /query /s $_  /v  -errorvariable myerror

if ($myerror -contains "some value that you find in an error)
{write-host T"his server did not allow me to query scheduled tasks"}
elseif ($myerror -eq $null)
{
#write your output or send to csv as you do above
}
Can you show me how to use try.. catch statement and log the errors with the server name to a txt file?
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer