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.
Related
this is my first post. I am using a batch command to check if an instance of my program is already running based on its command line argument. I am able to see if it is running by using the following code:
wmic Path win32_process Where "CommandLine Like '%Account A%' AND Caption Like '%Leads Manager.exe%'"
If the process exists, I will get a full break down of the process information, but if the process isn't running i get No Instance(s) Available.
I want to be able to run an If statement, so if the result shows No Instance(s) Available. I want to run a command without displaying the process information and if it does exists, I want to echo Account A is already running
Hopefully someone with more experience can point me in the right direction
Here's a batch-file example, which may help you out:
#Echo Off
For %%A In (A B C D) Do WMIC Process Where^
"Name='Leads Manager.exe' And Not CommandLine Like '%%Account %A%%%'"^
Get Name 2>Nul|Find "Name">Nul && (
Start "" "C:\Leads Manager.exe" -- "C:\Account %%A.ini")
This example uses the outer For loop to choose the Account letters, it is therfore checking to see if Leads Manager.exe is running with a commandline containing the string Account A, Account B, Account C or Account D, and if not it should run Leads Manager specifying the path to the appropriate account. (Please note that your .exe and .ini file paths both contain spaces, so I have doublequoted them, but left the rest of the command as you wrote it in your comment. You may want to consider checking that what you provided was correct and adjust it as necessary before testing, or replace that line entirely with a simple Echo Is Not Running) || Echo Is Running) statement, or perhaps run another batch file instead).
Environment:
Master Linux Jenkins server
Two Windows slave nodes
The windows slaves are running as a service
First Test
I create a Pipeline and use a use a "Execute Windows batch command" in the build section
In the Command box I put "C:\Jenkins\mytest.bat"
I checked the box "Restrict where this project can be run" and write down the name of the Windows slave
I built the pipeline and was successful
Second Test
I create a Declarative Pipeline as follows:
pipeline {
agent { label 'SiebelWindows' }
stages {
stage('Test Bat') {
steps {
bat 'C:\\Jenkins\\mytest.bat'
//bat 'start cmd.exe /c C:\\Jenkins\\mytest.bat'
//call C:\\Jenkins\\mytest.bat
}
}
}
}
In this case the build FAIL with error "cmd is not recognized as a internal o external command"
So, why can I run the .bat with a non-declarative pipeline, but fails with a declarative pipeline?
When I display the "Path" and the "PATH" variables, this was the result
Original_Path
The solution was redefine the PATH enviorement variable, like this
environment {
PATH = "C:\\WINDOWS\\SYSTEM32"
}
#JustAProgrammer aske me if C:\WINDOWS\SYSTEM32 was in the PATH of my Windows machine, and that is correct, but seems like Jenkins master do not know the slave's Windows Path.
I resolve my issue, but I still looking for a complete solution, I need to set the PATH enviorement variable with ALL the path, no only C:\WINDOWS\SYSTEM32
If someone has the problem too although the path variable looks correct, check the length of the PATH variable (echo %PATH%). Windows has some restriction of the length of an environment variable (see Microsoft DevBlog).
Remove some entries or set the PATH explicit like already mentioned.
Check also the configuration of the Environment Injector Plugin, which can override the default user and system PATH variable.
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.
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.
I have written some batch files in windows to centralise some calls to other batch files so that I can start up some processes easily, but when run from my central file the processes don't run properly; they need network access and this seems to be denied and I get a load of connection refused exceptions. This is presumably a permissions problem, but I'm not sure how to get round it. I've tried running my script by ricght clicking and choosing 'run as administrator' but this doesn't work properly (my first 'cd...' does not change the directory for some reason, then the calls fail)
Is there a way I can imply my permissions to the other processes?
My scripts look basically like this:
cd "F:\Applications\Process1"
START "Process 1" runProcess1.cmd
cd "C:\Applications\Process2"
START "Process 2" runProcess2.cmd
command runas with option /savecred should ask for password the first time it runs.
maybe this can help http://www.bellamyjc.org/en/superexec.html,
It turns out if you try and use 'cd "F:\Applications\Process1"' drive F will switch to the chosen folder, but you won't be switched to drive F. i.e. you will still be at C:\user\username (or whatever the default is) and if you then type 'F:', you will be at 'F:\Applications\Process1'. This meant that I was running two instances of the same process from the same place, when in fact I wanted to run two instances of the same process in different locations, so it looked like ti was launching properly then faailing to get network permissions, but was infact conflicting with itself. To fix this, I chagned my script as so:
F:
cd "F:\Applications\Process1"
START "Process 1" runProcess1.cmd
C:
cd "C:\Applications\Process2"
START "Process 2" runProcess2.cmd
Meant as a comment on answered Aug 24 '12 at 14:18 user1111284
But I cannot comment until I get 50 Reputation.
Use "cd/?" to get the short summary of cd command usage.
Use "cd/d" to change the current drive as well as the path.
Change your script like this:
cd/d "F:\Applications\Process1"
START "Process 1" runProcess1.cmd
cd/d "C:\Applications\Process2"
START "Process 2" runProcess2.cmd
I post this to other seekers. The /d option should be better known.