Run Scheduled Task in Windows Batch File? - windows

My issue exists in another question here. I'm wondering whether it's possible to activate a scheduled task on one server from the command line of another server. I do not want to use psexec as it is known to be an enabled of malware and viruses. Does anyone know if this is possible?

You can use the the commands below to stop and start tasks remotely.
Stop:
schtasks /end /s <machine name> /U Username /P password /tn <task name>
Start:
schtasks /run /s <machine name> /U Username /P password /tn <task name>

Related

Remotely Starting a Batch File

SCHTASKS /create /S $strComputer /tn "David\Start Gecko" /tr
"'C:\eQube-Tools\Batch and Registry Files\gecko.bat'" /sc once /sd
01/01/2099 /st 01:00 /u Boss /p *password* /RU SYSTEM
pause
SCHTASKS /run /S $strComputer /tn "David\Start Gecko"
So, I have a problem with running the batch file this creates, it says it's running on the task scheduler on the remote pc, but nothing happens. But the odd thing is: if I locally create a "basic task" it runs fine both remotely and running it by hand at that pc.
My question is there any way to have it know you want to create a "basic task" I've looked at the switches but can't find anything for /create, I also tried the /xml but this is something I plan to create, use then destroy so simpler the better.
** the reason I'm using SCHtasks is because I had a lot of issues with redirect errors trying to run it directly using powershell.
Specs
All win 7 WORKGROUP
All have matching Admin Accounts with matching password
Firewall set to allow remote sch tasks
Batch file is on remote pc
the batch file I'm trying to run
#echo off
c:
cd \eQube-Tools\Batch and Registry Files
start virtualhelpscreen.exe
:top
c:
cd \gecko
timeout 12 /nobreak
gecko.exe
cls
goto :top
SCHTASKS /create /S $strComputer /xml "filesavedtomainpc" /tn "David\Start Gecko"
one issue left is it not forcing full screen like it does when you run the batch file but that might be down to the program, for now the question I asked is answered.

How to Schedule a task with command prompt in windows 7?

I am trying to schedule a task with cmd prompt.The command that I am running is:
C:\Windows\system32\schtasks.exe /CREATE /SC ONLOGON /TN "mytask" /TR "C:\samp.txt" /RU "NT AUTHORITY\SYSTEM" /RP /F
The task is showing in task manager with username as SYSTEM ,and its status is Running with code 0x41301.But it(samp.txt) is not showing in desktop.
The same command if I am replacing the "NT AUTHORITY\SYSTEM" with currently logged on user then it(samp.txt) is showing on desktop.
How can I make the task to display on desktop with /RU "NT AUTHORITY\SYSTEM"
Did you try to add /IT ?
This is the only option which may allow you to display something on the active session using scheduled tasks. However, I am not sure that Windows will allow to interact with the desktop of another user.
Edit : You may use /RU BUILTIN\Users /IT, so any user will be able to run it interactively. What is you constraint on /RU system ?
If your goal is to display the samp.txt file content when a user logs on, another solution is to put the samp.txt in the startup folder of the "all users" start menu : C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
could you try with psexec ?
your command will be something like :
PSEXEC -i -s -d -w c:\ notepad.exe samp.txt

Check if windows password is correct with batch

How can you check if a Windows password is correct with batch. Using runas you could check if you get an error after typing your password but I want to know how to do it automatically by adding the password into the batch file (I know its not secure and I know you can save credentials with runas). Thanks in advance!
Depending on what OS, user access, and how your machine is configured, you could issue a net use to the machine itself on a particular drive letter.
Eg:
Net use U: /delete /y
Net use U: \\machinename\c$ /user:machinename\username password
You could check if the U: drive ended up mapped to verify it worked.
This is not a good idea security-wise and also may not work on systems with simple file sharing enabled.
If you have permissions to create/delete windows scheduled tasks, you could always use SCHTASKS:
SCHTASKS /CREATE /RU "Username" /RP "Password" /TN CREDCHECK /TR NOTEPAD /SC ONSTART
IF %ERRORLEVEL%==0 (ECHO Valid) ELSE (ECHO Invalid)
SCHTASKS /DELETE /TN CREDCHECK /F
There's probably better values for the /TR and /SC parameters, but you get the idea

Windows XP batch command schtasks doesnt' run a remote script

I'm trying to create a batch file in order to run automatic scripts on several remote PCs.
My main machine should be able to connect to any remote PCs and set a local scheduled task.
The batch file uses these commands:
schtasks /delete <--- remove any previous version
/S \\10.1.2.3 <--- the remote PC's IP
/U theAdministrator <--- the username to access the PC
/P MyPassword <--- the password to access the PC
/TN MyTask <--- task name
/F <--- don't ask, just do it option
schtasks /create
/S \\10.1.2.3
/U theAdministrator
/P MyPassword
/RU theAdministrator <--- the username to execute the task
/RP MyPassword <--- the password to execute the task
/SC dayly /MO 1 <--- run every day
/TN MyTask
/TR C:\task.bat <--- the script to run on the remote PC
When I launch the first /delete command everything works, but the second returns a warning:
"task has been created but probably it will not run because it hasn't been possible to set the account information" (I'm sorry if this is not the exact error message but I have to translate it by myself)
I'm sure that username and password are correct because the /delete command is OK, and also /create one creates the task, even if it doesn't run.
Therefore the problem should be with /RU and /RP options...
Solution:
I wasn't able to execute the command itself without this error message, anyway I've reached my aim and found two different options:
The simplest way using the AT command:
AT \\10.1.2.3 12:00 C:\task.bat
It has no problem but needs to have specified an hour to run; this means if you want the task to be executed immediately you'll have to chatch %time% variable.
This option also doesn't allow to set an user to run the task (I've tested it as Administrator and the task was set to execute as NT AUTHORITY/SYSTEM)
The full featured way using PsTools:
Passing the schtasks /create command to PsExec
set command=schtasks /create /SC dayly /MO 1 /TN MyTask /TR C:\task.bat /RU theAdministrator /RP MyPassword
PsExec \\10.1.2.3 -u theAdministrator -p MyPassword %command%
NB.
The target IP, user and password to access the remote PC have to be set within PsExec command, therefore you don't need them on schtasks.
The script task.bat already exists on root C:\ of the target PC.
Are the PC's on a domain??
I've used schtasks on a domain before, and it works without the /ru and /rp. I just used /u and /p as the username and password of the machine that I was scheduling the task on.
Never tried it on standard workgroup/homegroup machines though.
You may also want to look at this, and make sure UAC is off on all remote machines, as thes could cause issues later.
Edit
I've just tried from one laptop to another (no domain involved).
These two lines work (in CMD, so should work through batch) for me
schtasks /delete /S \\xxx.xxx.xxx.xxx /U USERNAME /P PASSWORD /TN MyTask /F
schtasks /create /S \\xxx.xxx.xxx.xxx /U USERNAME /P PASSWORD /TN MyTask /SC once /st START_TIME /it /TR c:\test.bat
Things to note are;
- Obviously, replace the x's with the IP you need, and the USERNAME and PASSWORD with relevant data
- I used /sc once /st START_TIME, and not your /sc daily /mo 1, but that shouldn't make a difference
- I've added /it which (taken from CMD) "Enalbes the task to run interactively..." (in my experience, not using it has cause problems)
- The user on the laptop I was scheduling the task for does not exist on the laptop I ran schtasks from
- The user on the laptop I ran schtasks from does not exist on the laptop i was scheduling the task for (so users not existing on client/host does not matter)

How do I stop/start a scheduled task on a remote computer programmatically?

I want to write a script that will stop a scheduled task on a remote computer, do some stuff, and then start the schedule task back up.
How can I do it?
Here's what I found.
stop:
schtasks /end /s <machine name> /tn <task name>
start:
schtasks /run /s <machine name> /tn <task name>
C:\>schtasks /?
SCHTASKS /parameter [arguments]
Description:
Enables an administrator to create, delete, query, change, run and
end scheduled tasks on a local or remote system. Replaces AT.exe.
Parameter List:
/Create Creates a new scheduled task.
/Delete Deletes the scheduled task(s).
/Query Displays all scheduled tasks.
/Change Changes the properties of scheduled task.
/Run Runs the scheduled task immediately.
/End Stops the currently running scheduled task.
/? Displays this help message.
Examples:
SCHTASKS
SCHTASKS /?
SCHTASKS /Run /?
SCHTASKS /End /?
SCHTASKS /Create /?
SCHTASKS /Delete /?
SCHTASKS /Query /?
SCHTASKS /Change /?
Note:
"schtasks" (see the other, accepted response) has replaced "at". However, "at" may be of use if the situation calls for compatibility with older versions of Windows that don't have schtasks.
Command-line help for "at":
C:\>at /?
The AT command schedules commands and programs to run on a computer at
a specified time and date. The Schedule service must be running to use
the AT command.
AT [\\computername] [ [id] [/DELETE] | /DELETE [/YES]]
AT [\\computername] time [/INTERACTIVE]
[ /EVERY:date[,...] | /NEXT:date[,...]] "command"
\\computername Specifies a remote computer. Commands are scheduled on the
local computer if this parameter is omitted.
id Is an identification number assigned to a scheduled
command.
/delete Cancels a scheduled command. If id is omitted, all the
scheduled commands on the computer are canceled.
/yes Used with cancel all jobs command when no further
confirmation is desired.
time Specifies the time when command is to run.
/interactive Allows the job to interact with the desktop of the user
who is logged on at the time the job runs.
/every:date[,...] Runs the command on each specified day(s) of the week or
month. If date is omitted, the current day of the month
is assumed.
/next:date[,...] Runs the specified command on the next occurrence of the
day (for example, next Thursday). If date is omitted, the
current day of the month is assumed.
"command" Is the Windows NT command, or batch program to be run.
schtasks /change /disable /tn "Name Of Task" /s REMOTEMACHINENAME /u mydomain\administrator /p adminpassword
What about /disable, and /enable switch for a /change command?
schtasks.exe /change /s <machine name> /tn <task name> /disable
schtasks.exe /change /s <machine name> /tn <task name> /enable
Try this:
schtasks /change /ENABLE /tn "Auto Restart" /s mycomutername /u mycomputername\username/p mypassowrd

Resources