I am trying to run a script(called testScript.bat) on a remote computer.
The script is:
mkdir D:\someFolderPath\batchActivatedTestDir
ROBOCOPY "\\Computer1\D\Drivers" "\\Computer2\D\User\Desktop\DestinationTest" /E
I tested the script and it works fine when I double click it on the remote machine.
Now I am trying to run it via WMI command:
wmic /user:User /password:"Password" /node:Computer2 process call create
"d:\scriptLocationPath\testScript.bat"
Now I get the usual success message in my cmd on computer1:
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ProcessId = 2028;
ReturnValue = 0;
};
But as I learned it refers to the excution of WMI command.
The result of the WMI command above is that the directory: "batchActivatedTestDir" is created but robocopy is not starting.
I don't even see the process ROBOCOPY.exe in task manager. Why is it not working properly, if the batch is activated ?
Even if I try to activate robocopy directly like this:
wmic /user:User /password:"12345678" /node:Shimon process call create
"ROBOCOPY.exe \\DavidPC\D\drivers d:\User\Desktop\destination1\test /E"
I get to see the process "ROBOCOPY.exe" in the task manager for a second and then it's gone :(
I don't understand why robocopy refuses to be activated.
Please help.
Related
I have a problem in my jenkins job and I isolated into one command. So I created another separate job to try to fix it.
So in this job, called "teste" I only have one single command:
start cmd /k call "C:\Program Files\myDir\myBat.bat"
This opens a separate cmd window running my bat file, which should keep running "forever".
But the problem is when I do it, my jenkins job keeps stuck into a "exit 0" operation that I have no idea from where it came from.
Thats the console:
[EnvInject] - Loading node environment variables.
Building remotely on Machine01 in workspace C:\workspace\teste
[teste] $ cmd /c call C:\...dir\jenkins.bat
C:\workspace\teste>start cmd /k call "C:\Program Files\myDir\myBat.bat"
C:\workspace\teste>exit 0
Then it keep stuck at that point.
Example of myBat.bat content:
echo hi
pause
There's any way to make this call in another window without waiting for its finish?
I solve my problem changing the way I was calling my other .bat, calling it through powershell. But since I was from a bat file, I used the command to send a powershell command, calling my other bat file.
Also, I've added another line changing the jenkins BUILD_ID to a fake one, so it doesn't kill it.
So I changed from this line:
start cmd /k call "C:\Program Files\myDir\myBat.bat"
To this :
set BUILD_ID=dontKillMe
powershell -Command "Start-Process 'C:\Program Files\myDir\myBat.bat'"
I hope it helps someone someday.
I've been trying to get a Jenkins deploy job to work by running a batch script to do the install of an msi from the Jenkins build machine itself. I've given the appropriate access rights, but still am not able to run the following command remotely, using WMIC
wmic /node:myServerIp /user:"clientpc\my-user" /password:"my-password" process call create "D:\someDir\someOtherDir\test.bat"
The follow response from the above command:
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 9;
};
After some research, it looks like return value of '9' is 'Path not found' according to https://msdn.microsoft.com/en-us/library/aa389388(v=vs.85).aspx, but I've verified that the path exists on the remote server.
The test.bat file that I'm trying to run is very simple, and should just write to a text file.
#echo This is a test.> test.txt
I've verified that both files exist on the server, and have granted 'EVERYONE' to the shared folder 'someDir'.
I have tried prefixing 'cmd.exe /c' to the path called:
wmic /node:myServerIp /user:"clientpc\my-user" /password:"my-password" process call create "cmd.exe /c D:\someDir\someOtherDir\test.bat"
...for which I receive:
Invalid Verb Switch.
I've verified that the user access is correct by providing a bad password, in which case permission is denied.
EDIT:
Changed the path from D:\someDir\someOtherDir\test.bat to D:\\someDir\\someOtherDir\\test.bat but now receive the following error:
ERROR:
Description = The RPC server is unavailable.
EDIT 2:
Looks like the RPC user I was using was the cause for the error. Still troubleshooting, but when I use my AD user, as opposed to the administrator I created to run this, I get the following AGAIN...
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 9;
};
I was able to get the following to work on an Active Directory domain.
Wmic /node:"ComputerName" process call create "cmd.exe /c (net use o: /delete /y & net use o: \\Server\share /user:Domain\Administrator Password & o:\Dir\Subdir\test.cmd) >> c:\users\MyUser\testout2.txt"
The very simple contents of test.cmd:
echo Just a test >> c:\users\MyUser\testout.txt
date /t >> c:\users\MyUser\testout.txt
time /t >> c:\users\MyUser\testout.txt
The "job" is being sent to "ComputerName" on the domain. The batch/script file the job runs is on a network share. The job running on "ComputerName" will not see any mapped drives, so I delete and map a drive. I don't believe it is ever necessary to delete the drive, but I added that for completeness sake.
After execution, testout2.txt shows the batch file executing the commands and
testout.txt contains the results of the batch file commands as expected.
Things to watch out for:
As mentioned, mapped drives are not visible from the remote job
You are executing in the target machine's environment - drive letters need to make sense to that machine
Internal commands such as 'echo' require the job starts with 'CMD.EXE /c'
Group multiple commands inside parentheses and separate with ampersands (&)
Don't collide file access. I use testout.txt and testout2.txt files. If I had given them the same name, one set of outputs would have been lost.
Nothing you do this way will ever be visible to the user; the job is run in such a way that it can not display on the user's screen.
Sending a password in clear text as I show in the example is a security hazard and should be avoided. I'm not sure of a better way to map drives in this context however.
I have a problem with executing an other command within a FOR-command on Windows 7 Ultimate.
The for-command is part of a batch-file and should parse the outputs of another command.
Usually this:
for /f %%a IN ('tasklist') DO echo %%a
should execute the command "Tasklist" and output its results via echo.
But I always get "command not found".
I tried to execute the command outside of the forloop and it works.
I also tried do execute lots of outer commands within the for, but every command said "command not found".
I also tried the examples of this post:
Batch: Execute command with quotes in for loop with piping to find
I`m sure
that the commands exists
that I have read and execute rights to it
that my User is in Administrator Group
that I run the commands with "Run as Administrator"
But nothing is working in this pc.
The same commands work on another PC wich is also running a windows 7 ultimate.
So has anybody an idea would could be wrong on the pc where all commands are not found ?
Here is an example of my console outputs when I try it with the command "ls".
ls.exe is a executable file from the gun4win project, and its located in the same folder where my batch-file is running.
The windows is in german, so the error output is also in german.
C:\test>test_for.bat
C:\test>rem --- test a command stand-alone ---
C:\test>ls
ls.exe test_for.bat
C:\test>rem --- test same command in a FOR-Loop ---
C:\test>for /F "delims=" %a in ('ls') do echo FOR-OUTPUT: %a
Der Befehl "ls" ist entweder falsch geschrieben oder konnte nicht gefunden
werden.
FINAL EDIT:
The problem was as wrong value for the system envoirement variable ComSpec.
I changed ComSpec in Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen" to "C:\Windows\system32\cmd.exe and the problem was solved.
Thanks to #foxidrive and #jeb
If you use FOR /F and get the error 'something' is not recognized as an internal or external command, operable program or batch file for every program, even internal commands, the most common cause is a wrong ComSpec variable.
You can check the variable with set ComSpec, it should be C:\Windows\System32\cmd.exe.
It will not help to change the variable on the command line, the cause is described at DosTips: ComSpec strange behaviour
If the variable contains a different value, you should correct this under
For a German system:
Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen
For an English system:
Win Key+Pause Key->Advanced System Settings->Environment Variables->System Variables
There exists a second possible cause for strange FOR /F behaviour
If the AutoRun feature can be enabled in the registry ...\Command Processor\AutoRun, for more details see cmd /?.
The AutoRun feature can start a batch file each time a new cmd.exe instance is started.
This can be useful for ex. showing some data on opening a new cmd window or always change to a choosen directory.
But this batch will be also executed inside the FOR /F and normally causes unexpected results.
Pipes also start new cmd instances, but suppress the AutoRun script
Is there a way to start multiple programs in a batch file on system start-up? In addition to that, in that batch file, I would like to be able to say: Once I execute a program, wait until that program completely loads, and execute the next listed program.
Any help would be appreciated.
I had the same issue in Win7 regarding running a script (.bat) at startup (When the computer boots vs when someone logs in) that would modify the network parameters using netsh. What ended up working for me was the following:
Log in with an Administrator account
Click on start and type “Task Scheduler” and hit return
Click on “Task Scheduler Library”
Click on “Create New Task” on the right hand side of the screen and set the parameters as follows:
a. Set the user account to SYSTEM
b. Choose "Run with highest privileges"
c. Choose the OS for Windows7
Click on “Triggers” tab and then click on “New…”
Choose “At Startup” from the drop down menu, click Enabled and hit OK
Click on the “Actions tab” and then click on “New…”
If you are running a .bat file use cmd as the program the put
/c .bat
In the Add arguments field
Click on “OK” then on “OK” on the create task panel and it will now
be scheduled.
Add the .bat script to the place specified in your task event.
Enjoy.
To run a batch file at start up: start >> all programs >> right-click startup >> open >> right click batch file >> create shortcut >> drag shortcut to startup folder.
The path to the folder is : [D|C]:\Profiles\{User}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Go to Run (WINDOWS + R) and
Type
shell:startup, paste your .bat file there !
To start the batch file at the start of your system, you can also use a registry key.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Here you can create a string. As name you can choose anything and the data is the full path to your file.
There is also the registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
to run something at only the next start of your system.
There are a few ways to run a batch file on start up. The one I usually use is through task scheduler. If you press the windows key then type task scheduler it will come up as an option (or find through administerative tools).
When you create a new task you can chose from trigger options such as 'At log on' for a specific user, on workstation unlock etc. Then in actions you select start a program and put the full path to your batch script (there is also an option to put any command line args required).
Here is a an example script to launch Stack Overflow in Firefox:
#echo off
title Auto launch Stack Overflow
start firefox http://stackoverflow.com/questions/tagged/python+or+sql+or+sqlite+or+plsql+or+oracle+or+windows-7+or+cmd+or+excel+or+access+or+vba+or+excel-vba+or+access-vba?sort=newest
REM Optional - I tend to log these sorts of events so that you can see what has happened afterwards
echo %date% %time%, %computername% >> %logs%\StackOverflowAuto.csv
exit
RunOnce
RunOnce is an option and have a few keys that can be used for pointing a command to start on startup (depending if it concerns a user or the whole system):
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
setting the value:
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce" /v MyBat /D "!C:\mybat.bat"
With setting and exclamation mark at the beginning and if the script exist with a value different than 0 the registry key wont be deleted and the script will be executed every time on startup
SCHTASKS
You can use SCHTASKS and a triggering event:
SCHTASKS /Create /SC ONEVENT /MO ONLOGON /TN ON_LOGON /tr "c:\some.bat"
or
SCHTASKS /Create /SC ONEVENT /MO ONSTART/TN ON_START /tr "c:\some.bat"
Startup Folder
You also have two startup folders - one for the current user and one global.
There you can copy your scripts (or shortcuts) in order to start a file on startup
::the global one
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
::for the current user
%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
1. Copy the following lines to Notepad.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Users\toto\your_file.bat" & Chr(34), 0
Set WshShell = Nothing
Note: Replace the batch file name/path accordingly in the script according to your requirement.
2. Save the file with .VBS extension, example launch_bat.vbs
3. Create new .bat file, in our case your_file.bat
4. Write the content of your .bat file.
Example:
#echo off
php c:\laragon\www\my_app\artisan serve --host=127.0.0.1 --port=8000
5. Run your_file.bat and ejoy :)
If your Windows language is different from English, you can launch the Task Scheduler by
Press Windows+X
Select your language translation of "Computer Management"
Follow the instruction in the answer provided by prankin
Another option would be to run the batch file as a service, and set the startup of the service to "Automatic" or "Automatic (Delayed Start)".
Check this question for more information on how to do it, personally I like NSSM the most.
This is sort of a follow-up to my question earlier (link).
To test things out I made this simple batch file to ensure the Task Scheduler was properly executing the batch file:
cd "C:\Users\user\Desktop"
echo. 2>test.txt
So after the test.txt document is created on the desktop, the batch file should end but it continues to run:
Is there a way, either at the end of the batch file or a setting in the Task's Properties, to ensure that the cmd process quits?
Thanks!
I ran into the exact same problem. However, I felt duped when I read what Trevor778 wrote in this post:
I had the same problem - the task worked but the status kept showing Running. One simple thing to try is click on the Task Scheduler Library in the left column. Click Action/Refresh. Presto. Status changed to Ready. That's all it was for me, the task ran fine, just the status didn't update. Hope this helps.
ref: https://social.technet.microsoft.com/Forums/en-US/2f6dc29c-3b8b-45f5-a2a7-53e076acc062/task-scheduler-scheduler-status-is-being-running-always?forum=winservergen
you can add "exit" to last line of your script
cd "C:\Users\user\Desktop"
echo. 2>test.txt
exit
Running TASKKILL /F /IM cmd.exe will kill all cmd.exe processes whether it was the one that spawned this batch file or not. That's probably not desirable behavior. :-)
Judging by your last question, I'm guessing you're still running your task with cmd.exe /k, which will keep that window open indefinitely. For an unattended task, cmd.exe /c is a better choice. When the batch file finishes, the process should end.
Same here on Windows 7.
Putting all batch files in a directory in the user User specific path who runs the task
run programm = " cmd.exe " (without a path)
Your extras, mine where = " /c "C:\Users[username]\whatever\your_batchfile.bat" >> log.txt" "
" >> log.txt " so that i can see the output of the batch...
start in = " C:\Users[username]\whatever "
I also checked the "run with highest privilges" box
after that everything worked fine :)
Use following
exit /B
you may find more information in windows console area then type:exit/?
I know it's an old question, but I personally found that if I let a pause at the end of the bat file, it would keep the status as "Running".
I usually leave a pause at the end to help with debugging, but I found when I removed it, the task scheduler finally recognised it as having exited. It didn't help if I just refreshed it.
The solution I found was to add this line at the very end of the batch file:
TASKKILL /F /IM cmd.exe
Now after the batch file task runs and completes, it is no longer in the All Running Tasks list and the status goes back to 'Ready' instead of staying at 'Running'.
Warning:
That command will kill all running command processor instances so it may be potentially harmful!