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

80,139

Solution 1

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.

enter image description here

Solution 2

These commands based on Java JAR files worked for me:
cmd
net use x: \\
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!
Neither I didn't need to change permission on jenkins service nor use runas command.

But I must mention that everyone had read and write access to the network drive.

Solution 3

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 i.e. shared drive. I used the UNC path and mapped drive as well but the error was same. For me it was error number 4 - MS DOS error code. The solution was to use net use command! Hope that it helps.

Solution 4

Easy fix for most things.

  1. Make a batch command with what your trying to run, filename.bat with the command prompt text inside.
  2. Make a normal windows shortcut for the batch command, edit the shortcuts advanced properties and check the "Run as admin" (tricky tricky).
  3. Now run the filename.lnk shortcut from jenkins command line call, this will get you around all the jazz.

:)

Share:
80,139

Related videos on Youtube

Chris
Author by

Chris

Updated on July 09, 2022

Comments

  • Chris
    Chris almost 2 years

    I am trying to run this command in jenkins after a MSbuild

    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. This runs fine in cmd.exe but when trying to run it in Jenkins, I am getting the error Invalid drive specification.

    Here is the output from 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.

    • Steve B
      Steve B almost 12 years
      isn't jenkins running as a service ? If that's true, you should resolve your issuer by specifing the network path in place of the mapped drive
    • JMC
      JMC almost 12 years
      Y: is probably mapped to a specific user. Jenkins probably runs under its own 'service account' with no access to that drive letter Y:? You can try using UNC pathing instead of Y:, i.e. "\\ServerName\Folder\Extraction_zone\Jenkins\"
    • Chris
      Chris almost 12 years
      Thanks for the replies. I tried this, but am getting access is denied: Time Elapsed 00:00:03.70 [trunk] $ cmd /c call C:\Windows\TEMP\hudson7535872922876977589.bat C:\Program Files (x86)\Jenkins\workspace\trunk>xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\D‌​ebug\Package\Package‌​Tmp" "\\COMP-NAME\Webs\Extraction_Zone" /E Access denied Unable to create directory - \\COMP-NAME\Webs\Extraction_Zone 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
    • Chris
      Chris almost 12 years
      I realized I forgot the \Jenkins part of the path, but still getting the Invalid drive specification error: C:\Program Files (x86)\Jenkins\workspace\trunk>xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\D‌​ebug\Package\Package‌​Tmp" "\\COMP-NAME\Webs\Extraction_Zone\Jenkins\" /E Invalid drive specification
    • Kohanz
      Kohanz about 11 years
      Did you resolve this? I am encountering something similar (Jenkins behaviour differs from command-line)
  • Chris
    Chris almost 12 years
    This didnt seem to work. I tried logging in as Administrator, but I'm getting the same error.
  • TWhite
    TWhite about 10 years
    I'm running from my work machine and I had this same issue. I did as @adarshr suggested and used This Account and Browse... then looked up my corprate AD account info and password. Worked like a charm!
  • DLeh
    DLeh about 9 years
    i had an issue with this: after changing the user the service would start and then stop. check the event viewer for more info. in my case my user didn't have access to C:\program files\jenkins
  • Schaki
    Schaki almost 9 years
    This made it work for me too, (I already had the jenkins server according to @adarshr 's reply). For improved readability the second line means net use x: \\yournetworkshare\foldername
  • Peter PitLock
    Peter PitLock almost 7 years
    If you came here via GIT options not working in your batch, but working in cmd.exe : issues.jenkins-ci.org/browse/JENKINS-34082
  • radix07
    radix07 over 5 years
    This fixed my Python scripts as well that were attempting to write to a network location. Changing/updating the Log On user didn't do the trick...
  • Davut Gürbüz
    Davut Gürbüz almost 5 years
    '"C:\your.lnk"' is not recognized as an internal or external command. This is what you get ;)