Execute WIndows bat file in Jenkins - windows

I am using Jenkins 2.46.1
I have a build pipeline plugin installed and I want to execute a Windows batch file in it. The batch file should execute in a new command window and not on jenkins console output. I give the below Jenkins pipeline groovy script:
node {
stage 'Init'
bat '''
call C:\\myprj\\mybat.bat stop
EXIT /B 0
'''
stage 'Deploy'
bat '''call C:\\myprj\\mybat.bat'''
}
In the init stage, I want to kill the process if it is already open and in stage deploy it should open a new command window and run my batch file.
The problem is that the above does not work. The build is succesful but no command window opens up. Pls suggest

Technically, to do what you're asking you should be able to run
bat 'start cmd.exe /c C:\\myprj\\mybat.bat'
This will launch a new command windows (cmd.exe) and run the batch file given. Depending how your Jenkins slave is running you may not see anything. (eg if it's running as a windows service or different user, you won't see anything)

Alternative solution if the agent is running in a service and you would like to get output:
bat(readFile("mybat.bat"))
Note: The bat file will need to be in your workspace.
Additional Note: You are no longer running the bat file from its original location. Instead it is being run from a temp location created by the underlying durable task system. This means things like %~dp0 in your script are not going to return the paths you might expect.

Related

TortoiseSVN Hook Scripts No Longer Run

A while a go we set up a post_commit hook script in TortoiseSVN, this is a simple batch file, and this worked fine. We are running Windows 10.
Recently (around the beginning of 2023) this stopped working. TortoiseSVN now reports:
The hook script returned an error:
The process cannot access the file because it is being used by another process.
The script executes fine when run manually.
Changing the script to execute to another batch file or a program, results in the same message.
Does anyone have any suggestions?
Thanks,
Chris
Edit:
The hook script is client side and is configured as:
Working Copy Path:
C:\Temp\Repo
Command Line To Execute:
C:\Users\chris\SVNFeeder\testrepo_sync.bat
Wait for script to finish: True
Hide the script while running: False
Always execute the script: True
The batch file contains the following:
java -jar %USERPROFILE%/SVNFeeder/SvnFeeder.jar -c %USERPROFILE%/SVNFeeder/testrepo_configFile
The java file updates our Jira server with commit messages.
Changing the command line to execute the java file directly results in the same error, even a simple batch file that only creates a folder doesn't execute.
There is nothing wrong with the batch file or the java file as both execute fine outside of TSVN.
I have just found out that Sophos was blocking the execution of the script.
There was no mention of this in the events page, which didn't help track it down.

run curl from windows task scheduler automatically

I have a simple one line bat file that runs a curl localhost:port. My curl.exe is in the same folder as the bat I don't have it installed globally. It runs fine if I double click it, it also runs fine if I right click in task scheduler and select run task. It also says it completes the "scheduled tasks" successfully but nothing happens (I'm sure of this as I'm checking data that should update if the script is run, and it doesn't happen under the scheduled/automatic scenarios). After reading lots of users issues I tried configuring in two ways (all on 5 minute updates):
Common Way
Action: Start a Program
Program/script: C:\p\updater.bat
Start in: C:\p\
Other Way
Action: Start a Program
Program/script: cmd
Add arguments: /c start "" "C:\p\updater.bat"
I have set permissions to the bat and the containing folder to allow all for all users/etc. Neither work when automatically triggered, neither error. I've tried in Server 2008 and Windows 10 (my OS) mode. Both work if I right click run task, neither automatically, any ideas?
Default working directory for scheduled script is %Windows%\system32. Try to add in first line to you updater.bat:
cd c:\p\
For diagnostic, you can add output redirect to you commands in bat file:
echo Script Started >> c:\p\log.txt
curl SomeCommand SomeCommand >>c:\p:\curloutput.txt
echo Script Ended >> c:\p\log.txt
and check files log.txt and curloutput.txt after execution of your script

SVN update using windows task scheduler not working

I want to setup a windows cron job to update svn. I created a batch file with this step:
START TortoiseProc.exe /command:update /path:"C:\svn" /closeonend:0
and setup the task scheduler to run this daily. This svn requires a password which I want to enter each time rather than cache it. The batch file works as expected without any issues.
The task scheduler shows that the job was successfully executed. However, I don't get the GUI to enter the password, neither does the directory get updated. Also, when I tried to manually update the svn directory thereafter, it says that svn is locked.
Why is task scheduler reporting successful?
Why is svn getting locked?
Task scheduler reports success because START seems to return exit code 0. START only starts an application and exits. It doesn't wait until the started application has exited. The task scheduler never sees the exit code of TortoiseProc this way.
You can do this with START WAIT. Enter START /? in a CMD for more information.
You can also do this with CMD /K itself. Enter CMD /? in a CMD for more information.
But I don't think you need START or CMD at all. You can either run TortoiseProc directly or call it via a batch script. In the batch script you can add more debugging like echos and redirected output in files to see what happens.
Do not use TortoiseProc.exe for this task! Use Subversion command-line client (svn.exe). Read the docs:
Remember that TortoiseSVN is a GUI client, and this automation guide
shows you how to make the TortoiseSVN dialogs appear to collect user
input. If you want to write a script which requires no input, you
should use the official Subversion command line client instead.

Task Scheduler fails to run batch file despite reporting task completed

I have a simple batch file which calls a powershell script.
I've looked at the following 3 previous questions on the subject as well:
Run a batch file from Task Scheduler is not working with a java command
Windows Task Scheduler doesn't start batch file task
Task Scheduler not executing batch (bat) file with MSTest commands
It seems like I've tried every single combination of running the task and it still doesn't execute my powershell script.
batch file contents:
powershell.exe "E:\SIS\fileCopy.ps1"
If I run the command in the batch file manually, it runs just fine. Here are things I've tried to do to get it working:
I've change the Security options to run as System with "Run with highest priveleges" checked
I've tried every other combination of "Run only when user is logged on", "Run whether user is logged on or not" and "Run with highest priveleges"
I've tried adding the "Start in (optional" setting to the folder where the files are located.
I'm at my wits end and can't believe Microsoft hasn't figured out a way to make this easier.
You need to have task scheduler execute Powershell.exe and have the arguments be the path to your .ps1 file.
To validate your script is running properly, you should set the Security options to 'Run only when user is logged on'. It will then pop the powershell dialog when it runs. I often also use start-transcript to view the results of scheduled poweshell scripts.
After you validate the script is running correctly, you can set the security options however best fits your situation.
Use the execution policy flag to flag that instance to unrestrisicted because your powershell settings may be blocking script execution.
powershell -executionpolicy unrestricted -Command "E:\SIS\fileCopy.ps1"
I found that Task Scheduler can't run a batch file if it lives in a folder that is being synced by OneDrive. I had to move the batch file to another folder to get Task Scheduler to be able to run it.

Bamboo Remote Agent Does Not Recognized Windows command or exectuables

I started trial with Bamboo recently. I ran bamboo server on my personal PC and configure our build server to be a remote agent. Our build environment is in Windows. The build process is carried out via TCL script, which basically executes Windows batch command to build different stuffs. The remote agent execute that TCL script after source code checkout. However, the Windows commands that are suppoesed to execute do not actually execute. For example, if we try to execute msbuild batch command from windows, we will do
exec "cmd << msbuild **.sln"
in the TCL script. The result of this command is 'msbuild' is not recognized as an internal or external command, operable program or batch file. The problem was gone for two builds then re-appear right after.
I tried just simply called the script from CMD on the remote agent and aslo execute the job on my PC(which is the default agent) through Bamboo. The mentioned problem does not occur.
Can anyone tell me if there are some other configurations that I have missed?
Thank you.
Well, your command:
exec "cmd << msbuild **.sln"
That's not going to work (unless you have a program called exactly that, which I think is illegal on Windows; you can't have * in a file name). Instead, you need to invoke it either via cmd or using a direct Tcl style invoke.
Which one will be best? I don't know; try them and find out for yourself.
Invoking via cmd.exe
exec cmd /c msbuild *.sln
Invoking a more Tcl-ish style
exec msbuild {*}[glob *.sln]
(Note that I think ** is always unnecessary as * just means “any number of any character” when expanding a glob. If you're using it to mean “recursive search through a directory structure”, say so and I'll provide code to do it using the find command in the Tcllib fileutil package.)

Resources