Howto Create Scheduled Task on Remote Computer


To create Scheduled Task on Remote Windows PC.
You can use the following command.
[Note: You must belong to Administrators group to perform this action.]

1
schtasks /create /s computer_name /tn "Weekly Backup" /tr c:\Backup_folder\backup.bat /sc weekly  /D FRI /st 13:00:00 /sd 02/28/2012 /ru "domain\admin_user" /rp "admin_password"

The above command will create an scheduled task which will run on every Friday at 1:00pm and it will execute backup.bat  file.

You can copy backup.bat to remote pc using \\computer_name\c$ and paste it where ever you like.
backup.bat can contain any action like taking backup. For example

1
C:\windows\system32\xcopy.exe c:\Backup_folder\*.* d:\Autobackup /S /D /C /Y

The above command will copy all data from c:\Backup_folder to d:\Autobackup. Next time when this file will be executed it will run as incremental backup, means only files that have been changed or added will be copied to destination folder.
We used this method to copy user data file to remote File Serve

TASKLIST / TASKKILL on LOCAL PC


To view Tasks on Local PC.

tasklist

you will see running tasks and there PID
As shown in the image below
. . .

To kill any task on Local PC.

taskkill /F /IM notepad.exe

To kill multiple task on Local PC.

taskkill /F /IM notepad.exe /IM mspaint.exe

To kill task by PID on Local PC. First find out PID of your program by running
tasklist

then note down the PID, its the numeric number written just after application name, for example
notepad.exe                 5768 Console                 0      3,644 K
so 5768 is the PID number, to kill it by PID number, use the following

taskkill /F /PID 4556

TASKLIST / TASKKILL on REMOTE PC


To view Running Tasks on Remote PC.

tasklist /s remote_comp_name
(or ip

To kill any task on Local PC.

taskkill /F /S COMP_NAME /IM notepad.exe

To kill task by PID on Local PC. First find out PID of your program by running

tasklist /s remote_comp_name
(or ip
then note down the PID, its the numeric number written just after application name, for example
notepad.exe                 3624  Console                 0      3,644 K
so 3624 is the PID number, to kill it by PID number, use the following

taskkill /F /S COMP_NAME /PID 3624

To delete files olde then X days using FORFILES


forfiles /p “C:\test” /s /m *.* /c “cmd /c del @path” /d -15

Change /d -15 to match your requirements. you can change the del command to any other like to show the files, use echo. FORFILES is builtin command with Windwos 2003 /7. You can download it from
http://www.ipass.net/davesisk/forfiles.zip

Daily Backup of Required Folder using ROBOCOPY


Robocopy method which will copy only new file by matching source and destination folder/file timstamps. Its much better as it displays results in command prompt with percentage and all the necessary info any admin want.  its my favorite. You can downlod Robocopy by downloading Microsoft Resource Kit from following link.
http://www.microsoft.com/en-us/download/details.aspx?id=17657

1
2
3
4
5
6
@echo off
Robocopy /E /LOG+:h:\Softwares\Servers_Related\mail-backup-log.txt B:\archive\.  T:\backup\archive
Robocopy /E /LOG+:h:\Softwares\Servers_Related\mail-backup-log.txt T:\mailserver\c$\mt\.  T:\backup\mt
Robocopy /E /LOG+:h:\Softwares\Servers_Related\mail-backup-log.txt T:\mailserver\mail\.  T:\backup\mail

The above commands description is as follows.
/E  = Copy sub-directories, including Empty ones
/LOG+ = Output status to LOG file (append to existing log, so previous entries along with new one should be saved together, if you don’t use + , it will overwrite existing log-file, means delete old entries and overwrite new one)).

XCOPY Method


Following file will copy all the data from the source folder to target folder using windows builtin copy tool XCOPY.

1
2
3
4
5
6
7
8
9
@echo off
echo * * *  >> C:\backup-log.txt
echo Mail Backup Starts at %date% - %time%  >> C:\backup-log.txt
C:\windows\system32\xcopy.exe B:\source-folder\*.*  T:\targetfolder /S /D /C /Y
echo Mail Backup Ends at %date% - %time%  >> C:\backup-log.txt
echo * * * >> C:\backup-log.txt

The above commands description is as follows.

/S = Copies directories and sub-directories except empty ones.
/D =  If no date is given, copies only those files whose  source time is newer than the destination time.
/C = Continues copying even if errors occur.
/Y  = Overwrite existing files if any without prompting, its necessary when you are running xcopy via batch / script file.

%d bloggers like this: