Run batch file when startup on Windows Server 2008 - batch-file

I would like to start a powershell script if the Server turns on. No dependency on the user and if a certain user logs out the program should continue working.
I tried an entry in the registry with the HKey_localMachine but my program won't continue working if a user logs off. Maybe there is a solution with the Task Scheduler?

Task scheduler is an option. Maybe run it with system-rights instead of admin

One shot in the dark is to have a scheduled task run a program/cmd.exe/powershell script as system on startup or user logon.
The command for starting on startup should be this:
schtasks /Create /TN taskName /SC onstart /TR powerShellScript.ext /RU system
Where powerShellScript.ext is replaced by whatever your script or other file is.
That should run whatever replaces powerShellScript.ext as system, thus preserving the process even if no users are logged on.
Another option is to launch the script once using a service as soon as it's started, but I don't have experience with that.
This might not work depending on how the file is launched. the most reliable method is to launch an executable and set the launch options to load whatever it is that you actually want.

Related

How to Automate Command Line taskkill without admin rights?

Tricky question but here's the thing.
I'm at work with a heinous security tool that blocks pretty much any ability to do anything.
I DO have permission to run /taskkill /F /IM someprocess.exe on the command line normally.
I want to do this daily because a certain software program --- only one person can access it at a time --- we work in different time zones. I usually close it every day manually, but there are misses. So I want to automate a task that kills it every night.
Here's the thing. ALL .bat files are blocked from running.
So my typical thought --- use Windows Task Scheduler to run a .bat file --- doesn't work.
So how else can I daily run this 'command line' command automatically? Is there no way? I do have access to do it manually of course (via command line directly). I can also do the regular end-task on windows task manager. I even have local admin rights of some sort (initially given so I stop pestering them with installs every week that require IT approval). But those can't seem to get around the .bat thing or 'highest privileges' in Windows Task Scheduler.
Another question --- I never log out of my computer, but the computer of course 'locks' nightly. The script would need to run while the computer is logged on to my account, but locked, nightly.
The easiest way is to go to IT, which I will do, but ... permissions every time I need to run a .bat file? There has to be a better way ... Also I'm not so sure if they will take a week to approve each time ...
taskkill /im process /f
Just schedule this command.
There is absolutely no need for CMD.exe or a bat file.
You can schedule for your own user account by ticking run only when user logged in.
To schedule
SCHTASKS /Create /SC DAILY /it /TN taskkill /TR "Taskkill /f /im program.exe"

start a batch file on startup as administrator on win 10

I want to disable auto-updating on Windows 10.
I found out that disabling the service "WINDOWS UPDATE" will do this.
It did for some time, but when ever i restart my system it enables itself again and again. So I created batch file that disables it.
#echo off
NET stop "Windows Update"
I put it in startup folder but it worked only if it is executed as administrator. I tried to set properties of the shortcut to "RUN AS ADMINISTRATOR" but it stopped executing on startup.
Then I read about Task scheduler and added this task there but still its not working there even it just shows "The Directory Name is Invalid. (0x8007010B)

Using Task Scheduler to run a batch file that contains the start /w command

I have a Windows XP virtual machine that I am using to run a legacy app. I am using a limited user account.
I have created a batch file that I'd like to:
Start the legacy app
Wait for the program in step 1 to close
Shut down the computer
So this is the code I am using (in this example, I am attempting to open notepad):
#ECHO OFF
start /w notepad
%windir%\system32\shutdown.exe -f -s -t 1
Because the legacy app needs to be run under an account with administrator privileges, I then manually created a scheduled task in Task Scheduler to run the batch file. The scheduled task is set to run using an account named Admin that has administrator privileges.
I am having a bit of an issue. When the scheduled task runs, nothing appears on the screen. However, I did notice that both cmd.exe and notepad.exe appear in Task Manager as processes running as the user Admin.
What am I doing wrong? How can I accomplish what I'd like to do? Thanks!
Sorry all, it was an issue with me not having Task Scheduler set up properly. I found this post which suggests to use the AT command to run the task interactively. This allows the Cmd window to be displayed when a batch file is run as a Scheduled Task.

Task Scheduler status still shows as Running after a BAT completes its task

I'm attempting to create a batch file that I can use with Task Manager in Windows 7 to copy users' local Google Earth file to a network location where it can be backed-up. It appears that the BAT is running just fine, but the task does not complete or stop once it's done & continues to show a status of "running".
Thanks in advanced for the suggestions!
COPY/Y "C:\Users\lena.domain\AppData\LocalLow\Google\GoogleEarth\myplaces.kml" "\\server\u-users\Lena\system backup\Google Earth" cmd.exe /C
Running your single line in a command prompt window results in error message: syntax error
Reason 1: There is no space between command COPY and option /Y.
Reason 2: There is cmd.exe /C appended at end of the line.
Use in your batch file the line:
COPY /Y "C:\Users\lena.domain\AppData\LocalLow\Google\GoogleEarth\myplaces.kml" "\\server\u-users\Lena\system backup\Google Earth"
And additionally make sure that this job is executed from task scheduler with credentials (account) which has the permission to write something on server. The local SYSTEM account often used for scheduled tasks has surely no permissions to access files on the server at all. It looks like the best account to use for this scheduled task is the user account of lena.domain. See also Configure a Scheduled Task Item (At least Windows 7)

Batch runs manually but not in scheduled task

I don't generally write batches, but I currently have a batch that uses forfiles to copy my FLVs from one folder to another. When I run the batch manually it works every time, but from a scheduled task, it throws a (0x1) error.
forfiles -p "C:\Program Files\Adobe\Flash Media Server 4.5\applications\name\streams" -m *.flv -s -d -1 -c "cmd /c copy #file ^0x22C:\Program^ Files\Adobe\Flash^ Media^ Server^ 4.5\applications\name\output\"
Not sure what syntax the scheduled task doesn't like.
Update
Under my scheduled task, Actions I have the following:
Program/script: name.bat
Start in (optional): \\servername\file\to\batch
Hi might be this is helpful,
I also face the same issue.
Just set the startin path like:
Here start in path is the path of batch file:
like you have enter in program script:
"E:\program related files\demo.bat"
then in startin just pass:
E:\program related files & done!
When my Start in (optional): path was a UNC path, it wouldn't work. So I moved my batch on the server and everything worked correctly.
UNC in Windows Batch Files
forfiles with UNC path
On Server 2008 R2 when running the batch file under domain user credentials, with confirmed "log on as a batch job" security in the Local Security Policy>Local Settings>User Rights Assignment,
even then my batch (copying a log file to a network share) would not run as scheduled task, until I selected in tab General the option "Run with highest privileges" (default NOT checked!)
The option Run whether user is logged on or not was also selected, with radio-button, but I guess this is quite standard, when selecting to run the task using a domain user account.
For the tab Actions : specifying the entire batch file name including its path, directly in "Program/script:" works fine (with Server 2008 R2)
Using double quotes inside the batch file causes no problems.
See the screen shot bellow.
You need to change the user to system
Most common reason for such problems is permissions: scheduled tasks does NOT always run with your user credentials. If you want scheduled task to run as you you will have to set it up as you or alternative user.
Besides that I hope that your line of code is a content of your batch file, you are not trying to run this command directly. Or are you?
P.S. What are these ^0x22 and ^ doing in your code?
I know this is an old question, but just wanted to share some info.
The (0x01) error code can also refer to resources that are not found. Therefore:
all files/folders referd from within the batch file should be accessible to the user which account is being used to run that scheduled task;
pay attention when using network locations in combination with "Run whether user is logged on or not" option;
the above-mentioned option can be tricky to use because some resources may be available only after log on.
For .bat files to run inside your scheduled task, you need to specify your .bat file path inside the start option - despite the fact that your .bat file is at the same directory as your .exe. Also, I flagged it to run with highest privilege. After I have done those two things, the task suddenly takes off without any problem!

Resources