Scheduling job on windows server 2008 - windows-server-2008

I have a file located in c:\sample\file.exe. Now i want to execute file.exe every night.How can i achive this using batch file?
Please help me out

Under the Control Panel, there's a applet called Scheduled Tasks. This is usually how you set up, um, what was it again, yes, that's it, scheduled tasks :-)
If you really want to do this from a batch file rather than a nice GUI (e.g., you're doing it as a part of some silent install), you use either at or schtasks. See here for details, or use this as a template:
schtasks
/create # Create a task.
/tn SampleFile # This is its name.
/tr c:\sample\file.exe # This is the file to run.
/sc daily # Every day.
/st 23:55 # At five minutes to midnight.
/ru User # User name to use.
/rp Password # Password of that user.
See the afore-mentioned page for even more options.

Save this command in batch file and execute it.
%SYSTEMDRIVE% SCHTASKS.EXE /CREATE /SC DAILY /TN "Name of task" /ST 12:00:00 /TR "c:\sample\file.exe" /RU ""

Related

Create scheduled task with administrative rights running from normal user account

I am creating an scheduled task during installation of an application. The installer itself is running with administrator permissons:
SchTasks /F /create /tn "MyApp Start" /XML "D:\MyApps\start.xml" /ru "System"
This task is intended to start during system startup, which is working fine as long as the user who is logging in is the one who created the task.
In my special case the task should also run if another non-admin-user is logging in.
Currently the task is not running, if the non-admin-user is logging in. Even more, the task ist not visible to him at all.
The question is: How can I create a scheduled task as administrator
using DOS or PowerShell-comnmands
that runs with System priviliges
that starts even if a normal non-admin-user logs into Windows 7/8
Here is my xml-description of the task.
<?xml version="1.0"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2015-03-02T22:54:11</Date>
<Author>foobar</Author>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<StartBoundary>2015-03-02T22:54:11</StartBoundary>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal>
<UserId>S-1-5-18</UserId>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<Duration>PT10M</Duration>
<WaitTimeout>PT1H</WaitTimeout>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions>
<Exec>
<Command>D:\MyApps\start.bat</Command>
</Exec>
</Actions>
</Task>
Do you have any suggestions?
Best Regards
Tobias
Tobias,
I actually use the built in Windows Task Scheduler to set up these types of operations. I find it alot easier than using CMD and it has all the options, features, triggers, etc that you may be looking for. I use it to draft tasks and eventually push them onto our network. Not to mention it can be accessed under normal and admin user rights by default.
Hope this points you in the right direction.
Mike.

Stop a recurring trigger in Windows Task Scheduler until the next day

We have scheduled a Windows Task to kick off some custom code (as an executable) that checks for existence of a file every 15 minutes.
If the file is eventually found, our service does some processing on it. Here's the rub: after the file is processed, the business requires that Task Scheduler stops these 15-minute checks until the next day.
Is there a way to insert logic into Windows Task Scheduler to stop running its trigger once some condition is met? Or is there a better way to architect this process?
We are using Windows Server 2008 R2 Standard SP1 to run this.
How to end a scheduled task:
A. To terminate a currently running task (i.e. one that shows up in Task Scheduler -> "Display All Running Tasks) from the command line:
schtasks.exe /End /TN "My task name"
B. To stop a scheduled task from being triggered to run in the future:
schtasks.exe /Change /TN "My task name" /DISABLE
C. To re-enable the task:
schtasks.exe /Change /TN "My task name" /ENABLE
More details are available by typing: schtasks.exe /Change /?
D. To ask a process to terminate (here: Notepad, but use the name displayed in Task Manager -> Processes\Image Name) that does not terminate when you run A. above:
taskkill.exe /IM Notepad.exe
E. To forcefully terminate a process that does not terminate when you run D. above:
taskkill.exe /F /IM Notepad.exe /T
Note: Using taskkill isn't a clean way of ending a process. You can read more in this post and in the article it links to.
How to set this up in Task Scheduler for your described situation:
Instead of having your task run the custom .exe directly, change it to run a script (e.g. a batch file, PowerShell script, Python script, etc.) which in turn:
Triggers the custom exe, then
Tests whether the file was found and "processed", then
Invokes command B. above to stop it from running again.
Add a second task that runs every morning (check "Run whether user is logged on or not) that re-enables the scheduled task by invoking command C. above.
Note: To invoke schtasks.exe requires elevated privileges, so set the task that executes that command to "Run with highest privileges".
How to detect that the file has been processed:
Have your custom .exe to add a Registry entry when it ran successfully. Your script can look for that entry using e.g. reg.exe (type REG QUERY /? for details).
There are other ways (e.g. posting a Windows event, sending a message, etc.), but using the Registry is an easy mechanism for a simple script to use.
Don't disable the task from directly your .exe. Keep the application and its invocation separate. That will also save you from having to recompile if you want to disable the task differently later.

Running a batch file from task scheduler and getting access denied

I'm trying to run a batch file from Windows 8 Task scheduler and I'm getting an access denied error on the command prompt for the following command line.
echo "Hello World" >> C:\File.txt
However, if I tick the checkbox "Run with higest privileges" under the task scheduler then it works fine.
The question is why do I require to tick the checkbox when I can run the batch manually without the "Run as administrator"?
A couple possibilities:
If a task is registered using the Administrators group for the security context of the task, then you must also make sure the Run with highest privileges check box is checked if you want to run the task.
If you are using the S4U functionality, make sure the Logon as batch job policy is set for the user.
Make sure the user account the task is using has write access to the C:\ directory
By default in modern Windows you cannot write to the root directory. Use "%temp%\file.txt" instead.

Remotely installing patches

I need to know how to run a batch script that will go to a shared location on the network, put the user name and password in and run a specified file. I'm not great with scripts and I can't find any coherent answers so far. I'm trying to run software patches on MANY systems and trying to save a little time with either going to each box individually and tossing in the patch disk or using Languard to click each patch (up to 155) individually.
So far I've got the 'net use' command to connect to the share but can't get it to use the user name or password, let alone get it to execute the patches. Could someone put this in a coherent format please? I am getting alot of the syntax, which is great but they always use extra symbols and asterisks which can be a little confusing. Perhaps an example with example names? Thanks!
PsExec can do what you want
http://technet.microsoft.com/en-us/sysinternals/bb897553
usage: psexec \\computer [-u username [-p password]] [-s] [-c [-f]] [-d] program [arguments]
-u Specifies optional user name for login to remote computer.
-p Specifies optional password for user name. If you omit this you will be prompted to enter a hidden password.
-s Run remote process in the System account .
-c Copy the specified program to the remote system for execution. If you omit this option then the application must be in the system's path on the remote system.
-f Copy the specified program to the remote system even if the file already exists on the remote system.
-d Don't wait for application to terminate. Only use this option for non-interactive applications.
Examples
The following command launches an interactive command prompt on \\marklap:
psexec \\marklap cmd
This command executes IpConfig on the remote system with the /all switch, and displays the resulting output locally:
psexec \\marklap ipconfig /all
This command copies the program test.exe to the remote system and executes it interactively:
psexec \\marklap -c test.exe
Specify the full path to a program that is already installed on a remote system if its not on the system's path:
psexec \\marklap c:\bin\test.exe
src: http://www.governmentsecurity.org/forum/index.php?showtopic=1030
I'm on linux right now so I can't test this.
for /l %%c in (1,1,254) do start psexec \\192.168.1.%%c -d -u administrator -p pass "net use Z: \\yourserver\user$ && Z:\update.exe && net use Z: /delete"
You could schedule the PSEXEC commands on the 70+ machines using scheduled tasks. The command line interface for that is SCHTASKS, so you could schedule them all at once from your administrator machine.
C:\Windows\system32>schtasks /?
SCHTASKS /parameter [arguments]
Description:
Enables an administrator to create, delete, query, change, run and
end scheduled tasks on a local or remote system.
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 on demand.
/End Stops the currently running scheduled task.
/ShowSid Shows the security identifier corresponding to a scheduled task name.
/? Displays this help message.
Examples:
SCHTASKS
SCHTASKS /?
SCHTASKS /Run /?
SCHTASKS /End /?
SCHTASKS /Create /?
SCHTASKS /Delete /?
SCHTASKS /Query /?
SCHTASKS /Change /?
SCHTASKS /ShowSid /?

Script to end process with a specific name

I have to kill two processes with the same image name (system and my local user name) every day. Is there a script that I could write or do I have to go to the task manager every time windows starts up?
Taskkill is the command you're looking for. It can even take pretty specific filtering arguments.
A bat file would do it, and could say
taskkill /im regedit.exe
(taskkill /? has other examples)
a vbs file with this line does it too
CreateObject("Wscript.Shell").Run "taskkill /im regedit.exe", 0, False
more invisibly done than just having windows just normally running a batch file