简体   繁体   English

来自Jenkins的执行Windows批处理命令失败,但在cmd.exe中运行正常

[英]Execute windows batch command from Jenkins fails but runs fine in cmd.exe

I am trying to run this command in jenkins after a MSbuild 我试图在MSbuild之后在jenkins中运行此命令

xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\Debug\Package\PackageTmp" "Y:\Extraction_Zone\Jenkins\" /E 

Y: is a mapped network drive. Y:是映射的网络驱动器。 This runs fine in cmd.exe but when trying to run it in Jenkins, I am getting the error Invalid drive specification . 这在cmd.exe中运行正常但是当试图在Jenkins中运行它时,我收到错误Invalid drive specification

Here is the output from jenkins: 以下是jenkins的输出:

Time Elapsed 00:00:04.03
[trunk] $ cmd /c call C:\Windows\TEMP\hudson3389873107474371072.bat

C:\Program Files (x86)\Jenkins\workspace\trunk>xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\Debug\Package\PackageTmp" "Y:\Extraction_Zone\Jenkins\" /E 
Invalid drive specification
0 File(s) copied

C:\Program Files (x86)\Jenkins\workspace\trunk>exit 4 
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE

Any help would be appreciated. 任何帮助,将不胜感激。

I too had a similar issue once. 我曾经有过类似的问题。 Try granting the Jenkins service "Logon as This account" right under services.msc and make sure the account you type there is the same as the one you use for running cmd.exe . 尝试在services.msc下授予Jenkins服务“以此帐户登录”,并确保您在其中键入的帐户与用于运行cmd.exe的帐户相同。

在此输入图像描述

These commands based on Java JAR files worked for me: 这些基于Java JAR文件的命令对我有用:
cmd CMD
net use x: \\\\ 净使用x:\\\\
xcopy "dist\\" x:\\ /Y xcopy“dist \\”x:\\ / Y.

And that's it! 就是这样! I spent lot of time figure out this issue and nothing worked until I wrote CMD and NET USE! 我花了很多时间弄清楚这个问题,直到我写了CMD和NET USE才行不通!
Neither I didn't need to change permission on jenkins service nor use runas command. 我既不需要更改jenkins服务的权限也不需要使用runas命令。

But I must mention that everyone had read and write access to the network drive. 但我必须提到每个人都有对网络驱动器的读写权限。

I had the same issue with my Windows Task running a batch file (I know it is not exactly same) where I tried to copy file to network location ie shared drive. 我的Windows任务运行批处理文件时遇到了同样的问题(我知道它不完全相同)我尝试将文件复制到网络位置即共享驱动器。 I used the UNC path and mapped drive as well but the error was same. 我也使用了UNC路径和映射驱动器,但错误是一样的。 For me it was error number 4 - MS DOS error code. 对我来说这是错误号4 - MS DOS错误代码。 The solution was to use net use command! 解决方案是使用net use命令! Hope that it helps. 希望它有所帮助。

Easy fix for most things. 容易修复大多数事情。

  1. Make a batch command with what your trying to run, filename.bat with the command prompt text inside. 使用您尝试运行的内容创建一个批处理命令,filename.bat,其中包含命令提示符文本。
  2. Make a normal windows shortcut for the batch command, edit the shortcuts advanced properties and check the "Run as admin" (tricky tricky). 为批处理命令创建一个普通的Windows快捷方式,编辑快捷方式高级属性并选中“以管理员身份运行”(棘手的棘手)。
  3. Now run the filename.lnk shortcut from jenkins command line call, this will get you around all the jazz. 现在从jenkins命令行调用运行filename.lnk快捷方式,这将使您了解所有爵士乐。

:) :)

The solution of adarshr (ie, modifying the log on credentials of the service) has worked for me for a part of the problem: in my case, this allowed me to successfully check out a mercurial repository (using ssh protocol), which I could not do when using 'Local System account'. adarshr的解决方案(即,修改服务的登录凭据)对我来说是一个问题的一部分:在我的情况下,这使我成功检查了一个mercurial存储库(使用ssh协议),我可以使用“本地系统帐户”时不要这样做。

However, I still have different behavior between running a command-line script or running the same script from a jenkins 'execute shell' script in the build section. 但是,在构建部分中运行命令行脚本或从jenkins的“执行shell”脚本运行相同的脚本之间,我仍然有不同的行为。 In my case, I compile a Python extension. 在我的例子中,我编译了一个Python扩展。 In Jenkins, I cannot import the extension (I don't see any error, but the execution simply stops, so I suspect it crashes). 在Jenkins,我无法导入扩展(我没有看到任何错误,但执行只是停止,所以我怀疑它崩溃)。

If I uninstall the service and run the slave agent as a Java Web Start, I do get the same behavoir. 如果我卸载服务并将从属代理作为Java Web Start运行,我会得到相同的行为。 It is a temporary fix for me, but it means that when I reboot the windows build machine, I have to manually re-start the Java Web Start application. 这对我来说是一个临时修复,但这意味着当我重新启动Windows构建机器时,我必须手动重新启动Java Web Start应用程序。

So -at least in my case- it is clear that this is a credential problem. 所以 - 至少在我的情况下 - 很明显这是一个凭证问题。

Credentials usage documentation: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI 凭证使用文档: https//wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI

我用CIFS插件解决了我的问题。

Faced similar issue and found two ways to solve. 面对类似的问题,找到了两种解决方法。

Type 1: Tell Jenkins about mapped drive. 类型1:告诉詹金斯关于映射驱动器。 1.Goto -> Manage Jenkins -> Script Console (Groovy Script). 1.转到 - >管理Jenkins - >脚本控制台(Groovy脚本)。 2.Run below command 2.跑到命令之下

 def mapdrive = "net use Y: \\\\copy_nework_address"
 mapdrive.execute();
 println "net use".execute().getText()

Type:2 1.Goto -> cmd -> run "net use" to know network address 键入:2 1.Goto - > cmd - >运行“net use”以了解网络地址

xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\Debug\Package\PackageTmp" "Copy_Network_Address\Extraction_Zone\Jenkins\" /E

Conclusion:- I prefer 2nd types as after every restart i should run Groovy Script. 结论: - 我更喜欢第二种类型,因为每次重启后我应该运行Groovy Script。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM