Execute Batch Script not working in Jenkins Pipeline Job - windows

I have created a powershell script which will transfer(using winscp.dll) the files from Jenkins windows server to Linux server. In Jenkins batch command, I have executed that powershell script and it works fine.
But when i tried the same in Jenkins pipeline job, it calls the powershell script and comes to the next step. Its not waiting for powershell script response.
bat 'powershell.exe -ExecutionPolicy Bypass "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1 $env:EndMarket $env:Environment"'
I have tried with another powershell script which will connect to Linux server and execute some commands. It works fine in pipeline job
Kindly guide me to fix this issue.

Interesting. Your problem doesn't seem to be in your script because you already explained that it works in a batch job.
I don't know how your pipeline is written, but I would suggest taking a look to Stage, Lock and Milestone which is probably what you need.
The stage step is a primary building block in Pipeline, dividing the
steps of a Pipeline into explicit units and helping to visualize the
progress using the "Stage View" plugin
I guess you could add a stage block like this one in your pipeline:
stage("Previous Step") {
// Some previous step
}
stage("Wait for Script Execution") {
// Call your script
bat 'powershell.exe -ExecutionPolicy Bypass "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1 $env:EndMarket $env:Environment"'
}
stage("Next Step") {
// Script already finished its execution
}
But without your pipeline info is just a guessing. Also to improve your script compatibility avoiding "bat and ExcutionPolicy" and using the PowerShell plugin, with that plugin you could simplify your code like this:
powershell -File your_script.ps1
EDIT: I forgot to mention that you can try a different alternative to powershell and the winscp lib using "scp" direct compatibility between Windows and Linux, I'm talking about Cygwin.
With Cygwin installed (with scp) you can use scp as it was a Linux box and a bash script instead powershell:
D:/cygwin-64/bin/run.exe /usr/bin/bash -lic \"/home/user/file.sh\"
In this case I'm running a script silently through Cygwin within a Jenkins Project with a "Run Windows Batch" option. In the script you can add shell commands and the scp instructions you want.
It may seems a little bit more complex but adds more flexibility to perform Windows - Linux tasks.
I've detailed more examples in my blog, you may find it useful.
As avvi mentioned you should check if that variables are being loaded.

You are not calling the script correctly. You are passing in $Env in the powershell invocation which is a powershell variable.
You are in batch mode so should be passing in %.
bat 'powershell.exe -ExecutionPolicy Bypass "D:\\Test\\Deploymentscripts\\PowerShellScript\\FileTransfer.ps1" "%EndMarket%" "%Environment%"'

Related

Intune Win32 App Install Parameters Array

I have some powershell who has the following:
Param([Parameter(Mandatory = $true)] $TargetPaths=#())
I bundle this .ps1 into a .intunewin with content manager and upload to intune.
I now wanted to call my powershell with an install command similar to powershell -executionpolicy bypass -command .\powershell.ps1 -TargetPaths #('1','2','3'...)
This works fine on my local machine... but does not pass through the intune installer, my log files are never created and after careful examination it is clear the script is never run.
Can anyone advise me on moving forward to get this to run? If I execute the script locally with the same parameters it works fine. I even re-jiggered it to work through a cmd execution instead of powershell (including the use of """ for a single double quote) and couldn't get this working.
Unfortunately, I cannot provide you with the final solution.
But you may want to try the -file parameter of powershell.exe. It partially works for us. This means that the script is executed correctly, but Intune always shows a wrong installation status in the first moment. So we have not yet solved this case conclusively either.
powershell -executionpolicy bypass -file ".\install.ps1" -i -name "myApp" -variant "2022"
While not really an answer to the question, I was able to work around the problem of passing an array by instead flattening the entire thing into a string on parameter pass and later breaking it into an array inside my code. In order to do this I removed all internal quotation marks and then assumed their location later. Not really the most elegant but the script now executes.

Execute WIndows bat file in Jenkins

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.

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