schtasks - checking for shutdown.exe and overwrite

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
mrrhtuner
Posts: 10
Joined: 09 Jan 2012 14:02

schtasks - checking for shutdown.exe and overwrite

#1 Post by mrrhtuner » 09 Jan 2012 14:09

Hey everybody, here is what I have so far:

schtasks /create /sc DAILY /ST 13:34 /SD 01/05/2012 /tn "Daily Reboot" /tr "C:\Windows\System32\shutdown.exe -r -f t 00" /ru System
pause

This batch file when run, creates a daily scheduled task for the computer to reboot at a specific time with force. This runs as the SYSTEM account.

To make this batch file more convenient, I want to write a statement(possibly IF statement?) that will check schtasks to see if a task has been created using the c:\windows\system32\shutdown.exe file. If so, the batch program will over write it immediatly. If not, the program will continue and create this task.


Any suggestions on how I can start this off? I was thinking of something along the lines of:

if schtasks /tr "c:\windows\system32\shutdown.exe"

but I know that seems pretty wrong


thank you

Squashman
Expert
Posts: 4176
Joined: 23 Dec 2011 13:59

Re: schtasks - checking for shutdown.exe and overwrite

#2 Post by Squashman » 09 Jan 2012 14:34

Use the Query option and pipe it to the findstr command.

schtasks /query /V /FO CSV | findstr "shutdown.exe"

Can probably put that into a for loop to get the correct token as well.

Squashman
Expert
Posts: 4176
Joined: 23 Dec 2011 13:59

Re: schtasks - checking for shutdown.exe and overwrite

#3 Post by Squashman » 09 Jan 2012 14:47

I believe this would work.

Code: Select all

for /f "skip=1 tokens=2 delims=," %%F in ('schtasks /query /v /FO CSV ^|findstr "shutdown.exe"') do schtasks /delete /TN %%F

mrrhtuner
Posts: 10
Joined: 09 Jan 2012 14:02

Re: schtasks - checking for shutdown.exe and overwrite

#4 Post by mrrhtuner » 10 Jan 2012 15:05

Squashman wrote:I believe this would work.

Code: Select all

for /f "skip=1 tokens=2 delims=," %%F in ('schtasks /query /v /FO CSV ^|findstr "shutdown.exe"') do schtasks /delete /TN %%F



Thank you, I've been trying to get it to work most of the day and I can't seem to get it to overwrite the current task (Ex: shutdown.exe), I've played around with batch files very briefly before, but nothing to this complex level lol

Squashman
Expert
Posts: 4176
Joined: 23 Dec 2011 13:59

Re: schtasks - checking for shutdown.exe and overwrite

#5 Post by Squashman » 10 Jan 2012 18:43

It would help to post the code you are using if you changed the original code I gave you.

mrrhtuner
Posts: 10
Joined: 09 Jan 2012 14:02

Re: schtasks - checking for shutdown.exe and overwrite

#6 Post by mrrhtuner » 11 Jan 2012 07:12

Squashman wrote:It would help to post the code you are using if you changed the original code I gave you.


sorry about the delay, spilt milk on my laptop last night so didnt have access to the online world lol

wel here is what I have in the batch file.

for /f "skip=1 tokens=2 delims=," %%F in ('schtasks /query /v /FO CSV ^|findstr "Shutdown.exe"') do schtasks /delete /TN %%F
schtasks /create /sc DAILY /ST 13:34 /SD 01/05/2012 /tn "Daily Reboot" /tr "C:\Windows\System32\shutdown.exe -r -f t 00" /ru System



To test it, I created a simple task in task scheduler that uses c:\windows\system32\shutdown.exe

Then I ran the batch file with the commands above. It added the scheduled task into task manager but the origional task is still there.

Basically I am trying to figure out a way for the batch file to recognize the shutdown.exe being used then it would just overwrite that task with the new one. If prompted to overwrite, it will automatically do Y without me setting it.

I've been looking at this site and a bunch of MS-DOS batch sites and I've done some basic/java programming before but haven't ever touched batch files to this level of complexity(well for me haha)

I was trying to figure out how to have it automatically overwrite without me pressing Y and I saw the XCOPY command but with xcopy it doesn't seem useful in this situation because it is used copying from one path to another...which I can't see it working for my batch above. I tried it and it didn't work obviously lol

Squashman
Expert
Posts: 4176
Joined: 23 Dec 2011 13:59

Re: schtasks - checking for shutdown.exe and overwrite

#7 Post by Squashman » 11 Jan 2012 07:55

Findstr is case sensitive.
Shutdown.exe is not the same as shutdown.exe
You need to add the /I switch to the findstr command.

While you could change the existing task with the /CHANGE option I don't think this would be as good of an approach as deleting what is there and then adding what you want. The Task name that is assigned to the existing scheduled task may not be that descriptive to your liking. The Task name could be " REAMDE" for all you know and that really isn't very descriptive of what the task actually does. The /CHANGE option doesn't give you the ability to change the TASK NAME. So you could be stuck with some obscure name for the task that isn't very descriptive that someone else put there instead of your nice descriptive name which clearly defines what the scheduled task is going to do.

mrrhtuner
Posts: 10
Joined: 09 Jan 2012 14:02

Re: schtasks - checking for shutdown.exe and overwrite

#8 Post by mrrhtuner » 11 Jan 2012 12:47

Thank you Squashman for the help! I really appreciate it.

I will probably need to put the statement into a loop...but before that is there a command I can use to automatically have the computer press Y-yes for overwrite ability?

Right now, if I run the batch script it will create the scheduled task. If I rerun it, it will remove the other 'shutdown.exe' task and overwrite the first one that was put in by the batch.

mrrhtuner
Posts: 10
Joined: 09 Jan 2012 14:02

Re: schtasks - checking for shutdown.exe and overwrite

#9 Post by mrrhtuner » 11 Jan 2012 15:32

Well I figured out how to do the overwrite...I used /F to force the process in the task:

Here is the code:

for /f "skip=1 tokens=2 delims=," %%F in ('schtasks /query /v /FO CSV ^|Findstr /I "shutdown.exe"') do schtasks /delete /f /TN %%F
schtasks /create /sc DAILY /ST 13:34 /SD 01/05/2012 /tn "Daily Reboot" /tr "C:\Windows\System32\shutdown.exe -r -f t 00" /ru System /f
pause


So this way I don't have to sit there and Press Y to overwrite, which is a nice thing.

Now to figure out the loop process of it

Squashman
Expert
Posts: 4176
Joined: 23 Dec 2011 13:59

Re: schtasks - checking for shutdown.exe and overwrite

#10 Post by Squashman » 11 Jan 2012 16:56

I finally understood what you meant by "OverWrite". It wasn't making any sense to me until you mentioned the /F switch with the delete option. I can see how you would think you are overwriting a task but you really are not. You are deleting an existing task and creating a whole new one. You were not getting the prompt to overwrite the task, you were getting the prompt to delete the existing task.

Squashman
Expert
Posts: 4176
Joined: 23 Dec 2011 13:59

Re: schtasks - checking for shutdown.exe and overwrite

#11 Post by Squashman » 11 Jan 2012 16:59

mrrhtuner wrote:Now to figure out the loop process of it

Not understanding why you think you need another loop?

mrrhtuner
Posts: 10
Joined: 09 Jan 2012 14:02

Re: schtasks - checking for shutdown.exe and overwrite

#12 Post by mrrhtuner » 11 Jan 2012 17:09

Squashman wrote:I finally understood what you meant by "OverWrite". It wasn't making any sense to me until you mentioned the /F switch with the delete option. I can see how you would think you are overwriting a task but you really are not. You are deleting an existing task and creating a whole new one. You were not getting the prompt to overwrite the task, you were getting the prompt to delete the existing task.


Sorry my mistake. I am rushed at work so I don't have much time to sit down and write these posts properly, sorry.

Squashman wrote:
mrrhtuner wrote:Now to figure out the loop process of it

Not understanding why you think you need another loop?


For some reason, if I have a task created in task scheduler with shutdown.exe selected in the action and I run the batch, it will just insert the new task above the old one. If I re-run the batch, then it will remove the old one and over-write the new one.

To me, it seems like the first time the batch is run, it only adds the task.
Run the batch again, it then recognizes the origional task using shutdown.exe and will ask you to remove it.

I would have tought that by creating a loop, I can have the batch first recognize the origional task and remove it then add the new task.

I hope I am not butchering this, I will re-clarify anything that I am not explaining properly.

mrrhtuner
Posts: 10
Joined: 09 Jan 2012 14:02

Re: schtasks - checking for shutdown.exe and overwrite

#13 Post by mrrhtuner » 16 Jan 2012 14:24

Here is somewhat of an update.

Well my code is still almost the same but I had to remove the skip=1 line.
Now, when I run the batch file, it will remove the previous tasks that use shutdown.exe and put in the new one...all in one shot.

Now here is something werid...the pc won't reboot anymore. I run the task and it wont reboot. I even used the previous exact same batch commands that I had working partially last week with it rebooting but still nothing.

I am stumped, everytime this batch takes 1 step forward, it takes 2 steps back

for /f "tokens=2 delims=," %%F in ('schtasks /query /v /FO CSV ^|Findstr /I "shutdown.exe"') do schtasks /delete /f /TN %%F
schtasks /create /sc DAILY /ST 15:12 /SD 01/05/2012 /tn "Daily Reboot" /tr "C:\Windows\System32\shutdown.exe -r -f t 00" /ru System /f
pause

mrrhtuner
Posts: 10
Joined: 09 Jan 2012 14:02

Re: schtasks - checking for shutdown.exe and overwrite

#14 Post by mrrhtuner » 16 Jan 2012 15:34

I tried to run the task as the basic command:

schtasks /create /sc DAILY /ST 16:33 /SD 01/05/2012 /tn "Daily Reboot" /tr "C:\Windows\System32\shutdown.exe -r -f t 00" /ru SYSTEM /RL HIGHEST /f
pause


and all I get from task scheduler is 0x1 and it thinks the batch ran.
I am guessing this could be some issue with task scheduler?

Squashman
Expert
Posts: 4176
Joined: 23 Dec 2011 13:59

Re: schtasks - checking for shutdown.exe and overwrite

#15 Post by Squashman » 16 Jan 2012 18:50

Did you read this article.
http://support.microsoft.com/kb/951246

Post Reply