I would like my batch script to start automatically when my victim starts their computer. But it does not work when I put the file under "Start Menu".
So how do I do?
Put the batch script (or a .lnk shortcut to it) under (for a specific user)
%AppData%\Microsoft\Windows\Start Menu\Programs\Startup
or (for all users, putting it here may require Admin privilege)
%ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup
Or you can edit the registry to add an item or use Windows Task Scheduler. These two methods allow you to put your batch script anywhere.
FYI, I used to deploy my script1 by putting it somewhere and adding an entry (the type should be REG_SZ) under this registry key:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
(replace HKLM with HKCU to add an item for a specific user instead of all users)
1: Link goes to my GitHub repo
Related
I run into problems with VM's where i have to manually startup the jobs everytime after the reboot.
I would like a startup bat script to perform the following everytime upon system reboot-
set a network drive as drive s:
(something like what i manually do - "net use s: \network dir name")
and then I call a bat script within the s to do the rest( lets say S:\test\test.bat)
How can i create it all as one bat script? and steps on how can I set it as a startup script on system reboot?
you ask two questions in one.
what is a BAT script?
in its simplest form, a BAT script is just a text file that contains every instruction you type in the command line as a line in the file. So, you just need to create a text file with notepad, fill it with your commands
NET USE s: \\server\sharedfolder
CALL S:\test\test.bat
call it s.bat, place it somewhere in your PATH, and simply invoke it as s in the command line.
how to run such a BAT script at boot?
the simplest (among many others) solution is to create a shortcut to the s.bat file inside the startup folder.
a. Click Start, click All Programs, right-click Startup, click Open.
b. Then locate your s.bat file, ctrl-shift-drag to Startup folder.
You say you are wanting a local batch script to do nothing but NET USE a network share as S:\, then run a remote batch script, e.g., S:\test\test.bat, and you want the local batch script to run every time on system startup?
The share mapping part is actually done for you by the NET USE command - type the following once to have S: available as a mapped drive from now on:
NET USE S: \\server\share /PERSISTENT:YES
Mapped drive S: will be available from now on at startup, without running any other batch script: immediately run your S:\test\test.bat.
To run this batch script - or any other executable - at startup, right-click the script/executable, and drag it to Start Menu, All Programs, Startup folder, and drop it (not on the folder, but in the expanded space just below it where other startup programs are): because you right-clicked, Windows Explorer will prompt you to "Copy here", "Move here", or "Create shortcut here" for the file. Select "Create shortcut here" - the file will run at startup from now on.
Create a new file test.bat, the content is as follows:
echo aa;
and execute it in the dos command line,
test.bat
there are no output in the window. But when I tested it in another computer, it outputs
aa
When I double click the bat file, it always disappeared suddenly even I add a "pause" clause line in the test.bat file. Both the systems are Window XP. Can sb tell me where is the problem, please?
I have checked all the things Mofi suggested, but the problem remains.
Now, I divscover when reboot to the system using another Administrator account. All is fine.
So, whether the problem comes from the registry record of the Windows XP?
See my answer on Iterating over files in directory prepends extra character to filename where I have already explained what to check if batch files are not working as expected.
The environment variables ComSpec, PATHEXT and PATH should be verified by you first. And check also the values of registry key Command Processor as explained in referenced answer. Also Autorun value of key Command Processor is very critical.
Further, start Regedit and check standard value of registry key HKEY_CLASSES_ROOT\.bat displayed on right side. It should be batfile. Then go to HKEY_CLASSES_ROOT\batfile, open it and navigate to subkey open - command. The standard value is "%1" %*. It could be that your Windows registry is partly corrupt which makes it not possible to run a batch file by double click.
A check for a problem with registry would be opening a command prompt window by running
C:\Windows\System32\cmd.exe
via Run entry in start menu of Windows. Then run your batch file from within the command prompt window.
You should also run from a command prompt window
C:\Windows\System32\chkdsk.exe C: /F
and confirm running the check disk tool of Windows on next boot of Windows. Then restart Windows to let this tool check the file system for errors resulting in 1 or more corrupt files. If an error was really detected in file system, run after Windows finished starting from a command prompt window
C:\Windows\System32\chkdsk.exe C: /F /R
Confirm running this tool once again on next boot of Windows, but this time with read/write tests on all sectors of your hard disk and repairing sectors if that is possible at all. It can take several hours depending on size of drive C: to finish this task.
Added on 2014-09-28:
Anti-virus application
Do you have checked already the configuration of the anti-virus application?
It could be that it blocks the execution of all *.bat files.
Windows System Restore
Do you have already tried to use Windows System Restore for the account on which batch file execution fails and go back to a restore point of a date on which you are sure that batch file execution worked?
If ntuser.dat or ntuser.dat.log (= registry of current user) is partly corrupt or something is misconfigured in Windows registry for this user account, this would be the easiest method to get back a working environment for the account.
Process Monitor Analysis
As execution of batch files does not work only with one account, the reason must be a corrupt or wrong set key or value in registry of this account.
I suggest to download and run free tool Process Monitor from Sysinternals (Microsoft) using first the account on which batch file execution does not work as expected.
The first displayed on start of this tool is a dialog to configure a filter. Select from first list Process Name, let is selected on second list, enter cmd.exe in edit field, and let Include selected in last list. Click next on button Include and then on OK.
Now in main application window of Process Monitor click on last 3 symbols in toolbar to toggle off those features resulting in monitoring only registry and file system accesses. Clear current list by clicking on fifth symbol in toolbar or by pressing Ctrl+X.
Now double click on your sample batch file in Windows Explorer. Then switch batck to Process Monitor, scroll up to beginning and look line by line what happens on executing of a batch file. Perhaps you can see something which you think is not correct.
The column Result should show only SUCCESS and NAME NOT FOUND and rarely NO MORE ENTRIES for registry entries in log. INVALID in column Result is not good. The log can be saved as CSV file with Ctrl+S or by clicking on disk symbol in toolbar.
Exit Process Monitor, logout from this account, logon on other account, start here also Process Monitor, configure the same filter, execute same batch file and save also this log into a CSV file. Compare the 2 CSV files and look for differences (other than time and line number).
Windows Registry Compare
Another method to find out what is different between the 2 accounts resulting in batch file execution not working is comparing the registry hives of those 2 accounts. In both accounts start Regedit and export entire key HKEY_CURRENT_USER to a *.reg file. Compare the two registry exports with a text comparison tool. The new administrator account has definitely less keys and values. Interesting are those blocks with just similar, but not identical lines.
Most interesting are the keys:
HKEY_CURRENT_USER\Console
HKEY_CURRENT_USER\Environment
HKEY_CURRENT_USER\Software\Classes\.bat
HKEY_CURRENT_USER\Software\Classes\batfile
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
Some keys from this list can be missing completely. That would be okay as in this case the same key under HKEY_LOCAL_MACHINE is taken into account by Windows. But when a key is missing in account on which batch file execution fails, it should be also missing in the other account, or something is wrong here.
What could be a big and hard to find problem is a registry key which is displayed on viewing the registry tree with Regedit, but when clicking on the key it cannot be read.
I have seen such partly corrupt registry keys already several times and it is very hard to fix such a registry error. This happens for example if an application removes a registry key without removing it also from index table of the registry.
Totally unexpected things happen if a registry key is present in index, but does not exist anymore in registry. For example I fixed once an issue where HKLM\Software\Classes\mailto was in index list, but did not exist anymore in registry. When the user clicked on a mailto link, Windows started creating new emails in Outlook in a loop until Windows run out of resources and user restarted Windows.
Please add # before echo, like this:
#echo aa
#pause
Its working in my Windows7 machine.
OS: Windows Server 2008 R2 Enterprise
I am unable to get the Task Scheduler to run .bat files while I am logged off. I have a a production .bat file I want to use, but for my troubleshooting purposes I am using C:\Users\Administrator\Desktop\test.bat
test.bat is very simple. All it contains is:
taskkill /im notepad.exe
It runs successfully on its own. If I have Notepad open and I double-click test.bat then the cmd window flashes and Notepad dies. The .bat file is not the problem.
The task I created in Task Scheduler is "Test taskkill" (without the quotes). I can see its location in Windows Explorer: C:\Windows\System32\Tasks\Test taskkill
If the task is set to run only when the user is logged on then the task works. With this setting, I can right-click on it > run and I get the same behavior as if I double-click the .bat file. That means the task is configured correctly.
If I set it to run whether or not the user is logged on then when I right-click > run nothing obvious happens. The job is reported as having been run successfully in the history, but Notepad survives. I know that I am using the proper credentials for the account that I am configuring to run the task. That account is the local administrator.
This issue appears similar to issues other have had in the past:
Running a .bat file in Scheduled Task
http://social.technet.microsoft.com/Forums/windowsserver/en-US/d47d116e-10b9-44f0-9a30-7406c86c2fbe/scheduled-task-wont-run-bat-file?forum=winservermanager
The thread in the second link seemed to resolve an identical issue for many people with the suggestion that the account used to run the task requires explicit permission to the .bat file and all files that the .bat file modifies. This was very promising (if an annoying requirement). However, I have confirmed or assigned explicit permissions for the local administrator to the following areas, and I am still not successful:
C:\Users\Administrator\Desktop\
C:\Users\Administrator\Desktop\test.bat
C:\Windows\System32\Tasks\
C:\Windows\System32\Tasks\Test taskkill
Additionally, I have confirmed that the local administrator account is in the local administrators group.
Am I missing some other permissions that needs to be set? Is there something else I should be looking at? Thanks!
I have resolved this problem with help from a contractor who was doing some other work for my institution.
In the properties window for the task, on the Action tab, when creating or editing the action that opens the batch file (in my case test.bat), there is the "Program/script:" field and there is also a "Start in (optional):" field. I previously had the "Program/script:" field containing the full path to the file, ending with the file name, and I had the "Start in (optional):" field null. This configuration appears to work without issue when the task is set to "Run only when user is logged on" in the General tab of the task's properties window. However, this configuration does not work when the task is set to "Run whether user is logged on or not".
In order to resolve the problem, I changed the action so that the "Program/script:" field contained only the file name, not the file path. I put the file path in the "Start in (optional):" field. This configuration works when the task is set to "Run only when user is logged on"! I have tested with the task running on a trigger and also with running it manually.
Not sure if this helps. My issue was using a interactive program (in particular Excel) as well.
Creating the 2 empty folders (Desktop) solved it. Create both even if you are running on x64.
C:\Windows\System32\config\systemprofile\Desktop
C:\Windows\SysWOW64\config\systemprofile\Desktop
I debugged my code and realised the code exited at this line
ExcelFile excelFile = new ExcelFile(directory, filename);
I read this solution somewhere and I cannot find the URL now.
Try this as well..
Resolved the "Run whether user is logged on or not" by setting the “Log on as Batch Job” security policy.
Here is details on the setting… https://danblee.com/log-on-as-batch-job-rights-for-task-scheduler/
You may take another approach, but here is the steps…
1) I updated the default domain group policy by adding my admin account (that I fire the task with) to the “Log on as Batch Job” security policy. (Even though ADMIN group is listed, and I am using an admin account)
2) I forced GPUPDATE on the client
3) Rebooted the client
4) Looked at the policy on the client to make sure it made it over from the DC
5) Set the task "Run whether user is logged on or not"
6) Ran the task on demand and it worked creating a PDF file.
7) Logged out and ran the task at scheduled time and it worked creating a PDF file.
I had similar problem, but it was related to the fact that there was a space in the path of my executable batch file. I have removed the space and now it works fine.
What work for me was make sure that the path is available.
i used net use Z: \computername\folder /persistent:yes
then the xxcopy /s /c /d /e /i /y D:*.* z:\BackupBat
I notice all my other .bat work fine only the one with the reference to a map folder was the one no running, so that need to be the problem. with these change the bat file runs good.
Verifying the following has worked for me:
1) 'Run if user is not logged in' option in Task Scheduler does not work for programs that is interactive mode. Example notepad, clock. Tasks will not run interactively. Make sure your program does not requires any interactive actions.
2)Check the “Start in (optional) “ If your program path is not one of the system paths. You have better to add you program path in it. On the "Program/Script" enter only the name of the program, and enter the path of your script in "Start in".
3)The user account you setup for your program must have permission not only to run your program itself and but also to run all other program that related to your program.
Here is a sample program:
On C:\myprogram\folderOne\test.cmd
date /t >> c:\Temp\testAuto.log
date /t >> c:\temp\testAuto.log
echo ----->> c:\temp\testAuto.log
cls
exit
-Create a Tasks, selecting on General>SecurityOptions "Run whether user is logged on or not"
-Enter on Action>Program/script: test.cmd
on "Start in" C:\myprogram\folderOne
-Run, you might see the cmd windows open and closes very quickly. The testAuto.log file should be in c:\tmp folder.
It might be because you don't have permission. I was facing the same problem and i found the solution like this -
Open Task Scheduler right click on your task and than select properties option.
In properties click on General tab and then click on 'User Group or User' and select the appropriate user.
I have a package that gets distributed to multiple machines including a batch file that moves some files into directories. One of the files that gets moved is an executable (.exe). This exe will be ran on a schedule, once the batch file runs for the first time, and moves files accordingly, it is never used again.
If I right click the .exe file > Properties > Compatibility, there is an option under Settings to run this program as an administrator. The application only seems to work when it is ran as an administrator, so I would like to enable this setting whenever the batch file runs.
Is there a way to modify this setting within a batch or via CMD?
What I continue to find in my search, is how to run cmd as admin, or how to add a runas command to the batch so the batch itself is executed as a admin. Since the batch file is only run at setup, and never again, I need a way to set the settings for the exe itself to run as admin.
Based on the info provided in this answer you can add the full path to the registry key Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers with the REG command like so:
Everything should be on one line but for clarity I put each argument on a separate line
REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
/v "c:\full\path\to\your\exe\file.exe"
/t REG_SZ
/d "RUNASADMIN"
Above command sets the compatibility flag for all users/system wide. If you want to set it only for the current user use HKCU instead of HKLM.
Do note that setting the value in HKLM subtree requires elevation.
This is tested on Win7 but should work on Vista and Win8.
Trying to delete the following registry key with a batch file but I can't even delete in the registry editor. Any ideas on how to write a batch file to do it, maybe it need to change permission also.
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\SWD\PRINTENUM\
There are about 8 sub keys under PRINTENUM such as {1974F44D-A278-......} that I want to be deleted.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\SWD\PRINTENUM
Same as above.
I did try psexec -s -i on the server and having the .reg file on the server too, but just couldn't run it on the client with admin command prompt using \server..... tried all sorts of reg import, to running the reg file with -aaceptuela given me error 0 which should be no errors and error 1 which I tried to login using -u to run it or runas etc.,.,
At the end I had to copy the .reg file and psexec to the local hard drive with the script and ran D:\PsExec.exe -i -s -accepteula reg import "D:\hidden_printers.reg" for it to work.
Thanks again.
First, read the Microsoft support article What are Control Sets? What is CurrentControlSet?.
After doing that it should be clear that whatever is modified under HKLM\SYSTEM\CurrentControlSet is in real done (usually) also on HKLM\SYSTEM\ControlSet001 and applied on next boot also to HKLM\SYSTEM\ControlSet002.
Key HKLM\SYSTEM\Select contains the information which control set is the current control set (usually control set 1), which one is the default control set on Windows boot (also usually control set 1) and which one is the last known good (usally control set 2).
Next read the Microsoft developer network article about HKLM\SYSTEM\CurrentControlSet\Enum Registry Tree.
By default only the SYSTEM account has the necessary permissions to change something in enum registry tree. That can be changed with Regedit.exe, but it is not advisable to do that. Enumerator keys are removed automatically on uninstalling a device with it's driver for example with the Windows device manager. It is not advisable to delete them manually from Windows registry.
It is possible to navigate in Regedit.exe to key HKLM\SYSTEM\CurrentControlSet\Enum\SWD\PRINTENUM, right click on this key, left click on context menu item Permissions, enable full access for group everyone, close the permissions dialog, delete the subkeys, re-open the permissions dialog for the key PRINTENUM again and uncheck full access for everyone to restore the standard permissions. But again, don't do that except you really know what you do.
See super user question Change registry permissions via command line (batch file) containing the answer on your question.