Why do all Pre-build or Post-build events in Visual Studio fail with exit code 1?
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.
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.
One possible issue is that you have to use $(ProjectDir)
instead of $(ProjectPath)
When you use $(ProjectPath)
it is actually: "C:\Users\....\Project\MyProject.csproj"
Versus $(ProjectDir)
which is: "C:\Users\....\Project\"
Notice, that the first version is actually pointing to your project solution file... which would cause everything to fail.
Another is that that $(ProjectDir)
automatically adds the trailing slash to the path. i.e. "$(ProjectDir)\test.bat"
would actually translate to: "C:\Users\....\Project\\test.bat"
Also you have to make sure that you enclose all your file paths in double quotes
So the correct call would look like this:
call "$(ProjectDir)test.bat"
Other things to check out would be to make sure that the directory that the script is executing from is correct. See SO: visual studio 2012, postbuild event, bat file not creating new file (not executing)
Have you checked out the suggestions in this StackOverflow? Post Build exited with code 1
Edit
Try this:
echo Hello World
@exit 0
You need to end any commands with @exit 0
otherwise it doesn't think that it finished properly
Edit 2
Why do we use @exit 0
instead of exit 0
? @Sunny has a good explanation of the At symbol - @ here:
The @ symbol tells the command processor to be less verbose; to only show the output of the command without showing it being executed or any prompts associated with the execution. When used it is prepended to the beginning of the command, it is not necessary to leave a space between the "@" and the command.
Essentially if you call xyz.bat
by default every command in the .bat
file is echoed back along with the actual output of the command. e.g.:
test.bat
echo Hello World
exit 0
Will output:
C:\>call test.bat
C:\>echo Hello World
Hello World
C:\>exit 0
C:\>
When we add a @
in front of the commands, we only get the output of the commands themselves.
test2.bat
@echo Hello World
@exit 0
Will output:
C:\>call c.bat
Hello World
C:\>
@echo off
is also a way to turn this off for the rest of the script. We typically do this simply to make the logs easier to read, as the text of the commands muddies the log output.