Author Posts

January 1, 2012 at 12:00 am

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?

Thanks

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."
$Error[0]
}
$TaskData | Export-CSV c]