Windows Scripting and OSQL for Vantage 8.03

Windows Scripting and OSQL for Vantage 8.03

Does anyone know the best way to run a script that does a query through OSQL and hit a server and then if that query returns a result, have it trigger a batch file to run?

The scenario is AppServer and SQL server are split, I need to check to see if there are tasks building up withImmediate Run Request as the situation, which means the TaskAgent has stopped working, I want to then automatically cycle the TaskAgent and the ProcessServer to restart them.

I have the osql part down to query the server to see if there are any tasks stacked up to more than 4 waiting to run, and I have the scripts to start and stop the TaskAgent and the Process server, but I am having trouble finding a way to combine the two.

osql -S ServerName -E -Q "(SELECT CURRENT_TIMESTAMP AS Date,count(agentschednum) AS CountImmed FROM Mfgsys803.dbo.sysagentsched WHERE scheddesc = 'Immediate Run Request' GROUP BY scheddesc HAVING count(agentschednum) > 4)"

That will let me know if there are 4 or more tasks sitting in the SystemTaskAgent schedule with Immediate Run Request as their status, I then want to call the script below if any record is returned from the above query.

call c:\epicor\Scripts\RestartTaskAgent.bat

The connection of the two different pieces is where I am having trouble.

I assume its an IF THEN ELSE type of code, but I am have not been able to come up with the code to do it yet, and I have done a lot of reading and googling but not coming up with much that will appear to work.