@echo off :: Application Name: Defrag_Task.cmd :: Author: Jason P. Morris :: Created: 10/08/2008 :: Description: This is used to help push Defrag as a scheduled task on to XP machines using :: the SCHTASKS command line. It runs as NT AUTHORITY\SYSTEM (so it does not require :: additional credentials). It is designed to account for this as a WEEKLY task. This :: batch file requires that one parameter be supplied via the command line. That :: parameter gets recognized as a variable %1 and is to be the computer name or IP :: Address of the PC requiring the Defrag scheduled task. :: %SYSTEMDRIVE%\Defrag_Task_Deployment.csv is created as a log file. :: Variable Definitions: :: Defrag_Cmd - Defines the command line to execute Defrag-ing the C: Drive :: Defrag_Name - The name to be used for this task (seen in Scheduled Tasks on the PC) :: Defrag_Name_Old - In the event the old task was not the same name as the new one, :: This variable allows specifying the previous name to remove it. :: Run_Day - The day Defrag should run. :: Run_Time - The time of day Defrag should run. :: Remove_Task - Based on %ERRORLEVEL%, stores if removing the old task was successful. :: Create_Task - Based on %ERRORLEVEL%, stores if creating the new task was successful. :: *** Sets the Title Bar of the Command window *** Title Defrag Task Deployment - DO NOT CLOSE! :: **************************** :: *** Initialize Variables *** :: **************************** Set Defrag_Cmd="c:\windows\system32\defrag.exe C:" Set Defrag_Name="Weekly Defrag" Set Defrag_Name_Old="Weekly Defrag" Set Run_Day=SUN Set Run_Time=02:00:00 Set Remove_Task="n/a" Set Create_Task="n/a" :: *** Checks that a PC Name was passed *** IF [%1]==[] goto Batch_Error1 :: *** Checks if the PC is online *** :: *** Uses PING to determine if the PC is online. *** PING -n 1 %1 if %errorlevel%==1 Goto Batch_Error2 :: *** Checks if the WINNT folder appears, assume it is Pre-XP *** IF EXIST "\\%1\C$\WINNT" goto Batch_Error3 :: ************************** :: *** Start Main Process *** :: ************************** :: *** Working with Scheduled Tasks *** Echo === Working on PC Name %1 === :: *** Deletes existance of an old task *** :: *** This allows for a new one with updated options to be installed *** schtasks /Delete /S %1 /TN %Defrag_Name_Old% /F :: *** Sets Remove_Task based on %ErrorLevel% *** if %errorlevel%==0 set Remove_Task=Success if %errorlevel%==1 set Remove_Task=Fail :: Creates new scheduled task schtasks /Create /S %1 /RU "NT AUTHORITY\SYSTEM" /SC WEEKLY /D %Run_Day% /TN %Defrag_Name% /TR %Defrag_Cmd% /ST %Run_Time% :: *** Sets Create_Task based on %ErrorLevel% *** if %errorlevel%==0 set Create_Task=Success if %errorlevel%==1 set Create_Task=Fail Goto End_of_Batch :: ************************ :: *** End Main Process *** :: ************************ :: *** Missing PC Error Handler *** :Batch_Error1 Echo Missing PC Name! set Remove_Task=No PC Name set Create_Task=No PC Name Goto End_of_Batch :: *** PC Not Online *** :Batch_Error2 set Remove_Task=Offline set Create_Task=Offline Goto End_of_Batch :: *** Non-XP Error Handler *** :Batch_Error3 IF EXIST "\\%1\C$\WINNT" Echo This is not an XP PC!! Set Remove_Task=Non-XP Set Create_Task=Non-XP Goto End_of_Batch :End_of_Batch :: *** Outputs the status of the Defrag Task Deployment *** echo %date%, %time%, %1, %Remove_Task%, %Create_Task% >> %SYSTEMDRIVE%\Defrag_Task_Deployment.csv Echo.