Going to try something different here, I notice that I am not paying a lot of attention to my site lately, and there isn’t a good reason not to. So for the next 30 days, until July 8th, I am going to attempt to do a post a day. I am also going to try weekends as well. The reason for this is I have a lot of things that I want to share, and I have been meaning to share from basic tech knowledge and into cars. This will also be an exercise in brushing up on my writing skills, I seem to be lacking a little bit there.
For the first post lets do a scripts to remove all the scheduled tasks on servers. We want to list all the scheduled tasks on a server, ok not a problem if you have one server you can login and view them, or you can list them remotely with
schtasks /query /s ServerName
This will return a result such as
TaskName Next Run Time Status
============================================================
FileScript1 23:59:00, 1/5/2010 Running
MyTasksForStuff2 07:00:00, 1/6/2010 Running
Now, you want to check the tasks on 15 servers, this gets more cumbersome and a little time consuming. So lets say you have this neat little shell script that helps you loop items so that you can one script multiple times
@echo off
if x==%1x goto MyServers1
if x==%2x goto MyServers2
for /f %%a in (%1) do call %2 %%a
goto end
:MyServers1
echo.
echo.
echo.
echo Run.bat syntax:
echo ---------------
echo.
echo Example: run.bat list.txt program.cmd
echo.
echo 'list.txt' should contain a list of computers/IPs
echo (one to each line) to each execute 'program.cmd'
echo.
echo The names of files are not important as long as they
echo are specified after the run command.
echo.
echo.
echo.
echo.
:end
Save this as run.bat
Next we want to take our 1 line from above and put it into its own bat file with 1 change.
echo “Server Name: %1”
schtasks /query /s %1
Save that as “ViewTasks.bat”
Next we need to setup a text file that contains all the servers we want to view the scheduled task on.
Now put it all together and you will see your results
run.bat list.txt ViewTakss.bat
"Server Name:" Server1
TaskName Next Run Time Status
============================================================
FileScript1 23:59:00, 1/5/2010 Running
MyTasksForStuff2 07:00:00, 1/6/2010 Running
ServerName: Server2
TaskName Next Run Time Status
============================================================
FileScript1 23:59:00, 1/5/2010 Running
MyTasksForStuff2 07:00:00, 1/6/2010 Running
And you can see your tasks running. Now lets say you want to delete them. Again test with the schtasks command
schtasks /delete /s ServerName /TN * /f
This will remove the tasks with only a complete as the last line.
So if you save this as “DeleteTasks.bat”
schtasks /delete /s %1 /TN * /f
You can now run it as a script on all your servers with
run.bat list.txt DeleteTasks.bat
And it will remove all the scheduled tasks from your servers that you have in your list.txt