Link to home
Create AccountLog in
Microsoft Legacy OS

Microsoft Legacy OS

--

Questions

--

Followers

Top Experts

Avatar of shong1997
shong1997

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?

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of becraigbecraig🇺🇸

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 shong1997shong1997

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"}

Avatar of becraigbecraig🇺🇸

sorry typo

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

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


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?

Avatar of becraigbecraig🇺🇸

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.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of becraigbecraig🇺🇸

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 becraigbecraig🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account
Microsoft Legacy OS

Microsoft Legacy OS

--

Questions

--

Followers

Top Experts

The Microsoft Legacy Operating System topic includes legacy versions of Microsoft operating systems prior to Windows 2000: All versions of MS-DOS and other versions developed for specific manufacturers and Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions, and Windows Mobile.