Sccm 2007 and 2012 report failed

Hi all hope someone can help. We are using the psappdt to handle our reboots so that users don’t lose any work. Problem we have is the deployment or advert reports as failed on the first run. The logic in the script is as follows show the defer prompt window if the user continues the script runs the msi and reboots. But the sccm client doesn’t report that it was successful until a subsequent tub. At what point is the success code passed to the sccm client ?