Why is 'cmd' not recognized in Jenkins declarative pipeline? - windows

I am trying to execute batch script on Jenkins on Windows agent, but I see the below error in a declarative pipeline in Jenkins, can someone please advise.
[Pipeline] bat
'cmd' is not recognized as an internal or external command,
operable program or batch file.
I added also C:\Windows\System32 in Jenkins Windows agent environment variable.
environment{
CREATE = "call C:\\IBM\\InformationServer11_3\\Clients\\Classic\\dsadmin.exe"
}
stage('create project'){
steps {
script{
echo 'Creating Project..'
{
bat "${CREATE} -domain ${BUILD_SERVER}:9443
This is the output of set path executed in a command prompt window:
Path=C:\Program Files (x86)\MKS Toolkit\mksnt;C:\PROGRA~2\MKSTOO~1\bin64;C:\PROGRA~2\MKSTOO~1\bin;C:\PROGRA~2\MKSTOO~1\bin\X11;C:\PROGRA~2\MKSTOO~1\mksnt;C:\IBM\InformationSever_117/jdk32\jre\bin;C:\IBM\InformationServer_117/jdk32\jre\bin\j9vm;C:\IBM\InformationServer_117\ASBNode\lib\cpp;C:\IBM\InformationServer_117\ASBNode\apps\proxy\cpp\vc60\MTdll\bin;D:\cygwin64\usr\local\bin;D:\cygwin64\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files\Java\jdk1.8.0_261\bin;D:\cygwin64\bin;C:\IBM\InformationServer11_3\jdk32\jre\bin;C:\IBM\InformationServer11_3\jdk32\jre\bin\j9vm;C:\IBM\InformationServer11_3\ASBNode\lib\cpp;C:\IBM\InformationServer11_3\ASBNode\apps\proxy\cppvc60\MT_dll\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;D:\net-snmp\bin;D:\Program Files\Git\cmd;/occ/cmg/build/encre/trunk/client-server/architecture_linux
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.sh;.ksh;.csh;.sed;.awk;.pl
So the list of folder paths in local Path is:
C:\Program Files (x86)\MKS Toolkit\mksnt
C:\PROGRA~2\MKSTOO~1\bin64
C:\PROGRA~2\MKSTOO~1\bin
C:\PROGRA~2\MKSTOO~1\bin\X11
C:\PROGRA~2\MKSTOO~1\mksnt
C:\IBM\InformationSever_117/jdk32\jre\bin
C:\IBM\InformationServer_117/jdk32\jre\bin\j9vm
C:\IBM\InformationServer_117\ASBNode\lib\cpp
C:\IBM\InformationServer_117\ASBNode\apps\proxy\cpp\vc60\MTdll\bin
D:\cygwin64\usr\local\bin
D:\cygwin64\bin
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
C:\Program Files\Java\jdk1.8.0_261\bin
D:\cygwin64\bin
C:\IBM\InformationServer11_3\jdk32\jre\bin
C:\IBM\InformationServer11_3\jdk32\jre\bin\j9vm
C:\IBM\InformationServer11_3\ASBNode\lib\cpp
C:\IBM\InformationServer11_3\ASBNode\apps\proxy\cppvc60\MT_dll\bin
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0
D:\net-snmp\bin
D:\Program Files\Git\cmd
/occ/cmg/build/encre/trunk/client-server/architecture_linux

Related

Jenkins cannot recognize sh.exe although added to path

I am trying to run a pipeline with sh instead of bat on Windows.
I have added the following paths in the global properties:
C:\Program Files\Git\usr\bin
C:\Program Files\Git\bin
but it still does not work.
When I run the pipeline, I get the following error message:
[pipeline] sh
/usr/bin/sh: C:\Program Files\Git\bin\sh.exe: Command not found
[pipeline] }
Note that the file sh.exe is located in the folder C:\Program Files\Git\bin\
However, if I use a Freestyle project with Execute Shell, it works correctly.
What else can I do to fix the problem?
It turns out that the space in Program Files was the one that was causing problems when trying to run the pipeline. Therefore sh.exe was not found in C:\Program Files\Git\bin\.
So I had to move Git\bin location somewhere else where there are no spaces in the path.
One thing you can do is to set the path variable when the pipeline is running. So basically you run this batch command within your pipeline:
SET PATH=PATH;"C:\Program Files\Git\bin"
and now, if you were to run sh as a batch command, it should refer to the sh.exe in "C:\Program Files\Git\bin

Which are the preinstalled PowerShell modules and why a cmdlet could be missing in a machine?

During the installation process of our software we use a batch file that replaces some text in some configuration files depending on user's input. The batch file basically calls a PowerShell command to do so:
REM %1 - file name e.g. file.txt
REM %2 - variable placeholder text e.g. {%placeholder%}
REM %3 - text to replace variable placeholder e.g. BLAH
powershell -Command "(gc %1) -replace '%2', '%3' | Out-File -encoding UTF8 %1" || echo ErrorLevel %errorlevel% > injection-bat.err
Some customers report that the batch file is failing. After some research on the failing machines we're able to see that PowerShell does not recognize the "Get-Content" (gc) cmdlet:
The term 'Get-Content' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
By taking a look on the installed PowerShell modules in the failing machine I was able to see that the Microsoft.PowerShell.Management module is not present. However, on all machines we've tested, the module is there (even in a clean fresh Windows installation).
Since I could not find a clear answer on that, I ask it here ...
Why so a basic module (Microsoft.PowerShell.Management) is not present in a Windows machine? Is there a list of "preinstalled" modules in Windows we can assure are always there? Would it be better to somehow install/import those modules using the batch file before using them just to be on the safe side? If this module is preinstalled with Windows, is there a reason why the module could be removed from the machine?
Many thanks in advance.
UPDATE 1:
I was able to find the PS version installed -> 5.1
The module Microsoft.PowerShell.Management is not in the list of available modules (Get-Module -ListAvailable).
UPDATE 2:
Here I answer the questions from #leeharvey1:
Does $Env:PSModulePath list C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules?
Yes
C:\Users\MAN Langenhagen\Documents\WindowsPowerShell\Modules;
C:\Program Files\WindowsPowerShell\Modules;
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
Does C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Management\Microsoft.PowerShell.Management.psd1 exist?
Yes
Is Microsoft.PowerShell.Commands.Management.dll present in your %PATH% (eg, $env:Path -split ';' | % { dir "$_\Microsoft.PowerShell.Commands.Management.dll" -ea silentlycontinue })?
I was not able to execute the command. But a Windows search showed 4 matches of the dll in:
C:\Windows\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\1.0.0.0__31bf3856ad364e35
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\v4.0_3.0.0.0__31bf3856ad364e35
C:\Windows\WinSxS\msil_microsoft.powershell.commands.management_31bf3856ad364e35_1.0.0.0_none_9fba00a194c2da2a
C:\Windows\WinSxS\msil_microsoft.powershell.commands.management_31bf3856ad364e35_10.0.19041.1_none_fcfa075fee21fc1f
However, these locations seem not to be part of the PATH (unless recursive).
C:\Program Files (x86)\Intel\iCLS Client\
C:\Program Files\Intel\iCLS Client\
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0\
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL
C:\Program Files\Intel\Intel(R) Management Engine Components\DAL
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT
C:\Program Files\Intel\Intel(R) Management Engine Components\IPT
C:\Program Files\Intel\WiFi\bin\
C:\Program Files\Common Files\Intel\WirelessCommon\
C:\WINDOWS\System32\OpenSSH\
C:\Users\User\AppData\Local\Microsoft\WindowsApps
Is a .NET Framework 4 version installed (eg, [System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription)?
That command returned nothing, however the call [System.Runtime.InteropServices.RuntimeInformation] returned:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False RuntimeInformation System.Object

Jenkins pipeline sh fail with "cannot run program nohup" on windows

I have windows 10 and I want to execute the sh command in the Jenkinsfile from Jenkins pipeline using bash for Ubuntu for windows, but it doesn't work
I have the following stage in my Jenkins pipeline :
stage('sh how to') {
steps {
sh 'ls -l'
}
}
The error message is :
[C:\Program Files (x86)\Jenkins\workspace\pipelineascode] Running shell script
Cannot run program "nohup" (in directory "C:\Program Files (x86)\Jenkins\workspace\pipelineascode"): CreateProcess error=2, Le fichier spécifié est introuvable
I tried changing Jenkins parameter->shell executable with
C:\Windows\System32\bash.exe
but same error...
how to run sh script using windows 10's bash?
From a very quick search, it looks like your error is related to the following issue : JENKINS-33708
The main cause looks like the sh step is not supported on the Windows. You may use bat or install Cygwin for instance.
Nevertheless two solutions were proposed in the previous link, suggesting you to do the following steps :
Install git-bash
Ensure the Git\bin folder (i.e.: C:\Program Files\Git\bin) is in the global search path, in order for Jenkins to find sh.exe
Make nohup available for Jenkins, doing the following in git-bash (adapt your paths accordingly) :
mklink "C:\Program Files\Git\bin\nohup.exe" "C:\Program Files\git\usr\bin\nohup.exe"
mklink "C:\Program Files\Git\bin\msys-2.0.dll" "C:\Program Files\git\usr\bin\msys-2.0.dll"
mklink "C:\Program Files\Git\bin\msys-iconv-2.dll" "C:\Program Files\git\usr\bin\msys-iconv-2.dll"
mklink "C:\Program Files\Git\bin\msys-intl-8.dll" "C:\Program Files\git\usr\bin\msys-intl-8.dll"
Depending on your installation you may have to use these paths :
mklink "C:\Program Files\Git\cmd\nohup.exe" "C:\Program Files\git\usr\bin\nohup.exe"
mklink "C:\Program Files\Git\cmd\msys-2.0.dll" "C:\Program Files\git\usr\bin\msys-2.0.dll"
mklink "C:\Program Files\Git\cmd\msys-iconv-2.dll" "C:\Program Files\git\usr\bin\msys-iconv-2.dll"
mklink "C:\Program Files\Git\cmd\msys-intl-8.dll" "C:\Program Files\git\usr\bin\msys-intl-8.dll"
With Git for Windows 2.16.2, I was able to add C:\Program Files\Git\usr\bin to the PATH (rather than C:\Program Files\Git\bin) and consequently my sh commands work in both FreeStyle and Pipeline builds. No mklink was necessary. (Source)
If you are executing on Windows, just change sh to bat. it will work as expected.
Example:
pipeline {
agent any
stages {
stage ('Compile Stage') {
steps {
withMaven(maven : 'apache-maven-3.6.1') {
bat'mvn clean compile'
}
}
}
}
}
With Git for Windows, I had to add C:\Program Files\Git\bin to the PATH environment variable of the slave node in Jenkins (to get access to sh), then add C:\Program Files\Git\usr\bin to the PATH locally on the Windows slave too (to get access to nohup).
Windows doesn't understand the "sh" command. To enable this, add
C:\Program Files\Git\bin &
C:\Program Files\Git\usr\bin
to the System Environment variable PATH, than restart your system.
Than execute your command in jenkins, it will work.
Switching sh to bat worked for me - I am running Jenkins on Windows. But only after I had resolved an issue caused by the fact I had not configured my tools (maven and the JDK) correctly in Jenkins either.
In my case I replaced 'sh' by 'bat' in Pipeline script and worked.
sh is not windows command. The simple way to enable the use of 'sh' command in windows is to install GIT BASH
Once you install GIT BASH, then you need to set below environment variables path.
C:\Program Files\Git\bin : This path contains sh.exe, bash.exe and git.exe
C:\Program Files\Git\usr\bin : This path contains several Linux based exe and dll (cat.exe, find.exe etc.)
By setting above configuration you will be able to execute 'sh' command in Jenkinsfiles on Jenkins installed on windows machine.
I was getting the same error below solutions worked for me..
Install git-bash
for windows use "bat" instead of "sh"
set "C:\Program Files\Git\usr\bin" to PATH(user variable)
My observation is that the agent seems to be trying to run nohup in the context where the agent.jar is run, not in the container. It didn't matter what I put in the container, the error message was the same. By putting nohup and sh in the PATH where the jenkins agent is running, I see a change in behavior.
git config core.sparsecheckout # timeout=10
git checkout -f c64c7bf905b6a4f5a8f85eb23bbd108f4c805386
sh: /home/jenkins/workspace/projname/simple_docker#tmp/durable-9fedc317/jenkins-log.txt: No such file or directory
sh: /home/jenkins/workspace/projname/simple_docker#tmp/durable-9fedc317/jenkins-result.txt.tmp: No such file or directory
mv: cannot stat '/home/jenkins/workspace/projname/simple_docker#tmp/durable-9fedc317/jenkins-result.txt.tmp': No such file or directory
I am seeing a folder /home/jenkins/workspace/projname/simple_docker#tmp/durable-9fedc317 which contains a file "script.sh" with the contents "docker inspect -f . repositoryname:tagname"
When this docker command is run manually on the command line, it always produces a single line of output consisting of a single period character. I have no doubt this is not what the jenkins system is looking for.
So you want the job running under WSL. If you want all jobs running under WSL have you considered installing Jenkins under WSL? Then everything is already in GNU land and you don't have to bridge the envrionment/culture of windows to GNU from within your Jenkins configuration.
I got the above issue in windows 10 and just added the path "C:\Program Files\Git\usr\bin" to the system variables then it started working.

Batch file for PsExec

I am trying to run PSExec through a batch file with the following command which runs fine when I execute through my command line:
C:\Program Files (x86)\PSTools\PsExec.exe -s -i 1 -u administrator -p force \135.20.230.160 C:\Program Files (x86)\Tensilica\Xtensa OCD Daemon 9.0.3\xt-ocd.exe
But when I include this command in a batch file (psexec.bat) and try to run it throws an error:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
How I can fix this?
Both local and remote desktops are Windows 7 64 bit OS.
Thanks for your help.
Quote file/folder references that include spaces
"C:\Program Files (x86)\PSTools\PsExec.exe" -s -i 1 -u administrator -p force \\135.20.230.160 "C:\Program Files (x86)\Tensilica\Xtensa OCD Daemon 9.0.3\xt-ocd.exe"

'C:\Program' is not recognized error

I've recently tried changing my environment variables to set paths to javac.exe (among other things). It was working fine until, all of a sudden, I started getting this error. For example, I declared a JAVA_HOME variable to be
C:\Program Files\Java\jdk1.7.0_25
After which, I add
%JAVA_HOME%\bin
to the PATH variable, but this gives me an error:
'C:\Program' is not recognized as an internal or external command, operable command or batch file.
This error makes it seem like it's running into problems with the space in "Program Files". This is weird, though, since it wasn't doing this for a good while, then started. Further, there are other variables with spaces in them that work just fine. I've tried deleting the variable and recreating it, putting quotes around JAVA_HOME (which goes to the correct path, but does not find javac.exe correctly)..
Any tips on what I might do?
This is on Windows 7.
EDIT:
The environment variables were set by going Control Panel > Advanced System Settings > Environment Variables. The value of the variables were set by copying the address of the folder I want through an Explorer window. I added it to the PATH environment variable by appending the address with a space between the preceding variable and a semicolon at the end, as such:
C:\Users\Demo_User_1\AppData\Roaming\npm; %JAVA_HOME%
where the JAVA_HOME variable is defined as such:
C:\Program Files\Java\jdk1.7.0_25
I test the value of the variable through a command prompt by typing %JAVA_HOME%, and that's where I get the resulting error of "'C:\Program' is not recognized..."
The results of 'set' are as follows:
C:\Users\Demo_User_1>set
ALLUSERSPROFILE=C:\ProgramData
ANDROID_HOME=C:\Users\Demo_User_1\Desktop\Android\adt-bundle-windows-x86_64-2013
0717\sdk
APPDATA=C:\Users\Demo_User_1\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=DEMO_USER_1-HP
ComSpec=C:\Windows\system32\cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\Demo_User_1
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_25
LOCALAPPDATA=C:\Users\Demo_User_1\AppData\Local
LOGONSERVER=\\DEMO_USER_1-HP
NUMBER_OF_PROCESSORS=4
OnlineServices=Online Services
OS=Windows_NT
Path=C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Clien
t\;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files
(x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;
C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program
Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\
x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Intel\I
ntel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Managem
ent Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engin
e Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Compone
nts\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\Wir
elessCommon\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files
(x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files (x86)\Windows
Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\
Tools\Binn\;C:\Program Files\nodejs\; C:\Users\Demo_User_1\Desktop\Android\adt-b
undle-windows-x86_64-20130717\sdk/platform-tools; C:\Users\Demo_User_1\Desktop\A
ndroid\adt-bundle-windows-x86_64-20130717\sdk\tools; %JAVA_HOME%; %ANT_HOME%/bin
; C:\Program Files\Java\jdk1.7.0_25\bin; C:\Users\Demo_User_1\AppData\Roaming\np
m; "%JAVA_HOME%"; ;C:\Users\Demo_User_1\Desktop\Android\adt-bundle-windows-x86_6
4-20130717\sdk/tools; C:\Users\Demo_User_1\Desktop\Android\adt-bundle-windows-x8
6_64-20130717\sdk/platform-tools
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PCBRAND=Pavilion
Platform=MCD
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=3a09
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\DEMO_U~1\AppData\Local\Temp
TMP=C:\Users\DEMO_U~1\AppData\Local\Temp
USERDOMAIN=Demo_User_1-HP
USERNAME=Demo_User_1
USERPROFILE=C:\Users\Demo_User_1
VS110COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools
\
windir=C:\Windows
windows_tracing_flags=3
windows_tracing_logfile=C:\BVTBin\Tests\installpackage\csilogfile.log
Yet another solution is to do this: C:\Program Files has a short name
C:\Progra~1
in windows.
so simply write Progra~1 instead of the Program Files. {added missing 'r'}
Okay, that makes it clearer.
There are two main problems here.
First of all, the reason you're getting 'C:\Program' is not recognized... is, of course, because it contains spaces. The fact that you have it quoted in the PATH environment variable has no relation to how %JAVA_HOME% is interpreted at the prompt. You have two options.
Quote it when you define the variable, i.e. set JAVA_HOME to "C:\Program Files\Java\jdk1.7.0_25"
Quote it when you invoke it. Type "%JAVA_HOME%\bin" at the prompt. Of course, you'll get the "not recognized as an internal or external command, operable program or batch file" error unless you end the path with an executable (e.g. "%JAVA_HOME%\bin\javac.exe"), but you'll see that this way it complains about '"C:\Program Files\Java\jdk1.7.0_25"' rather than 'C:\Program'.
Second, you can't use environment variables in the path. You can use environment variables when you set the path at the command prompt. For example,
set PATH=%PATH%;%JAVA_HOME%
will work, but that's because %JAVA_HOME% is expanded at the command line and PATH is set to the result. If you check the value of PATH, you'll see that it ends with C:\Program Files\Java\jdk1.7.0_25, not %JAVA_HOME%.
Also, if javac.exe is in the bin subdirectory, you'll need to include that in the path, i.e. add ;C:\Program Files\Java\jdk1.7.0_25\bin to the path.
(BTW, you have %JAVA_HOME% in the path twice, and there's an extra semicolon after the second one.)
Is the path you are setting the JAVA_HOME environment variable in a user variable or a system variable? You cannot use user variables within system variables. So if JAVA_HOME is defined as a user variable and you are adding it to your system path that won't work.
From the output of your set command it looks like %JAVA_HOME% is not being resolved. It should show the expanded version not the one with % signs in.
Add a Path user variable and add %JAVA_HOME%\bin to that. Windows will add your user path to the end of the system path.
You should not need quotes in the JAVA_HOME variable even if it contains spaces.
Even though Adi Inbar is pretty clear on the problem, I think his workaround isn't the best solution, because it tries to patch around the original problem: spaces in the path of your JDK installation.
The best way to solve your problem is actually reinstalling JDK to a space-less path. All other workarounds will cause you headaches in the long run.
just add cd before adding the location
eg:
instead of
C:\Program Files\Java\jdk1.7.0_25
use
cd C:\Program Files\Java\jdk1.7.0_25
this helped me.
It is very clear that , this is caused due to the blank space. Just add quotes to the phrase it foldername which contains a blank space
C:\"Program Files"\Java\jdk1.7.0_25
if you are Windows 10, Use Browse Folder rather than Edit text. I had similar issue and this got resolved using above method.
Hope this helps!
Reinstall Java and change its installation directory from C:\Program Files\Java\jdk to somewhere in C:\Java\jdk. Avoid to use Program Files folder in installation as space between 'Program' and 'Files' creates problem.
Thanks!!

Resources