Get Scheduled Tasks on remote servers

by sh1997 at 2013-02-19 14:12:21

Below is the script I use to get scheduled tasks running on remote servers:

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

the problem is that I received “A network path is not found” for servers that are not available on the network and “Access is denied” for the servers that the script cannot authentiate. I would like to see how the above script with the try…catch statement to capture these errors with the server name. Can someone help?

by ArtB0514 at 2013-02-20 08:29:32
Not tested, but I’d start with something like this:
$TaskData = @()
foreach ($Server in (get-content servers.txt)) {
Try {
Test-Connection $Server -Count 1 -Quiet -ErrorAction Stop
$TaskData += {Schtasks.exe /Query /s $Server /V /FO CSV} | ConvertFrom-CSV |Where { $
.TaskName -ne “TaskName”}
Catch {
“An error occurred connecting to $Server.”
$TaskData | Export-CSV c]