scheduled tasks

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sean
Posts: 2
Joined: 10 Sep 2011 12:43

scheduled tasks

#1 Post by sean » 10 Sep 2011 13:26

Hi,

I want to write the names of the scheduled tasks that failed (last result: 1,9 etc.) in a file.

I can write the names of all the scheduled tasks that have been created on the Windows Server 2003 :

Code: Select all

for /F "delims=, tokens=2" %%R in ('schtasks /query /fo csv /v') do call :Sub %%R
goto :eof

:Sub
FOR %%B IN (%*) DO set TaskName=%%B
   
echo name: %TaskName%

:eof


But, I can't show the names of the scheduled tasks that have a last result different from 0.

Do you know how I can do that, please ?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: scheduled tasks

#2 Post by aGerman » 10 Sep 2011 15:08

I hope your output is comparable to my German output. Are all values enclosed in quotes? Would you find the last result in the 7th column?
How ever, try that code and tell me if something's going wrong.

Code: Select all

@echo off
for /F "delims=, tokens=2,7" %%R in ('schtasks /query /fo csv /v^|findstr /vic:"Last Result"') do call :sub %%R %%S
pause
goto :eof

:Sub
if %2 neq "0" echo name: %1
goto :eof

I used the FINDSTR command to filter out all head lines.

Regards
aGerman

sean
Posts: 2
Joined: 10 Sep 2011 12:43

Re: scheduled tasks

#3 Post by sean » 11 Sep 2011 10:21

Hi aGerman,

Thank you for your response. Your code works very well on Windows Server 2003.

But I have a problem with a scheduled task :
I have a script that mounts a drive on each server start :

Code: Select all

net use P: \\server\share$

The person that has created this task is "sean". The Last result of this task is "0".

When I launch your script, the last result for this task is "sean". I don't know why I have this result for this task only ?


I have another problem on Windows Server 2008. I have a server without scheduled tasks created by me for the moment. But, when I launch the script, I find scheduled tasks of Windows :
- \Microsoft\Windows\Bluetooth\Uninstall Device Task
- \Microsoft\Windows\Active Directory
etc.
These tasks are in \Microsoft or \Microsoft\Windows. How can I ignore these directories in my script ?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: scheduled tasks

#4 Post by aGerman » 11 Sep 2011 12:25

sean wrote:But I have a problem with a scheduled task :
I have a script that mounts a drive on each server start :

Code: Select all

net use P: \\server\share$

The person that has created this task is "sean". The Last result of this task is "0".

When I launch your script, the last result for this task is "sean". I don't know why I have this result for this task only ?

I assume there is an additional comma somewhere in one of the values. The FOR loop does not differentiate between a comma in a value and a comma between two values.


sean wrote:I have another problem on Windows Server 2008. I have a server without scheduled tasks created by me for the moment. But, when I launch the script, I find scheduled tasks of Windows :
- \Microsoft\Windows\Bluetooth\Uninstall Device Task
- \Microsoft\Windows\Active Directory
etc.
These tasks are in \Microsoft or \Microsoft\Windows. How can I ignore these directories in my script ?

That could also be done using FINDSTR.

Code: Select all

for /F "delims=, tokens=2,7" %%R in ('schtasks /query /fo csv /v^|findstr /vic:"Last Result" /vic:"\\Microsoft\\"') do call :sub %%R %%S

Regards
aGerman

Post Reply