Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do all Pre-build or Post-build events in Visual Studio fail with exit code 1?

My goal is copying an .exe file from a bin folder of solution A to another solutions B resource folder on the post build event of solution A.
I created the necessary xcopy command and tried it out in powershell: It works perfectly.
Whenever I add any command to the actions in VS build fails with: "#command# exited with code 1", where #command# is, for example, the xcopy command.
I tried running VS as admin and currently I tried just running a .bat file that contains "@echo off @exit 0". That too leads to "#command# exited with code 1".

Have some example of what I tried out as VS post/pre-build command:
call "projdir\test.bat"
call projdir\test.bat
"projdir\test.bat"
projdir\test.bat
... I tried projdir as "$(ProjectPath)" and manual path.

I put output to verbose and found the following:
The command "C:\Users\Traubenfuchs\AppData\Local\Temp" is written incorrectly or couldn't be found. (That folder actually exists but I don't know what it wants to do it.)
The same thing happens when I put an xcopy command in pre/post build.

Anyone knows what I am doing wrong?

enter image description here

like image 624
ASA Avatar asked Jul 30 '14 08:07

ASA


People also ask

How do I run a batch file in post-build event?

If you go to the Properties page for your project, you should select the Build Events tab. You can type in the call to your batch file in the Post-build event command line text box. If you want to refer to the batch file using the paths included in the project or solution, you can click on the Edit Post-Build...

How to use Post-build event in Visual Studio?

In the Post-build event command line box, specify the syntax of the build event. Add a call statement before all post-build commands that run . bat files. For example, call C:\MyFile.

How do I debug a post-build event in Visual Studio?

Another way is to check the bin\debug dir for 'PreBuildEvent. bat' or 'PostBuildEvent. bat' which are the file that Visual Studio creates and run during the build events, if there is an error the files remain in the output dir and you can run them manually and spot the error.

What is PostBuildEvent?

PostBuildEvent. This event executes after the build finishes. The following table lists each use-in-build element: XML Element. Description.


2 Answers

Recently I met similar problem.

About your "hello world" post-build event : try to call powershell directly

powershell -command "write-output 'hello world'; exit 0"

=================

My pre-build event looks like :

powershell -file scriptPath.ps1

My scriptPath.ps1 looks like :

<my epic powershell code>
...
exit 0

Note that without "exit 0" at the end I recieved return code 1 from my script.

like image 164
Алексей Козлов Avatar answered Nov 10 '22 09:11

Алексей Козлов


We had this issue because of a "Software Restriction Policy" set up in a domain GPO. It appears that pre and post builds create a .cmd batch file in the temp folder. Here's what the logging showed me:

cmd.exe (PID = 8456) identified C:\Users\brian\AppData\Local\Temp\tmp733425d2c0604973a90a0a175c13353e.exec.cmd as Disallowed using default rule, Guid = {11015445-d282-4f86-96a2-9e485f593302}

To fix this we modified the GPO that controlled the SRP so that it did not include .cmd files. After all, the goal of SRP is to block executable malware, not batch files. I may get some security blowback because of this. Hopefully someone knows a better way to fix this issue.

like image 32
Brain2000 Avatar answered Nov 10 '22 09:11

Brain2000