9

I'm using an SCCM 2012 application deployment to install software. The specific install type is scripted installer as SCCM kept failing for exe validation errors. The script is powershell and successfully installs the software even when executed by SCCM.

The problem is that SCCM only sees the return of 0 from the completion from the script. I've tried write-host, return, write-output, and just a line item with the code for a soft reboot. None of them seem to be read by SCCM as the app execution log captures a return of 0.

How do you output return codes from powershell so that SCCM can interpret them?

  • Have you tried something like exit 1 at the end of the PowerShell script? – jscott Aug 25 '15 at 21:44
  • In this case exit is a cmd builtin. In other deployment systems I seen .ps1 files run with start or cmd /c powershell.exe somefile.ps1. Sorry for spitballing without access to an SCCM test. – jscott Aug 25 '15 at 21:54
9

There is a known problem with powershell exit codes (see the end of this answer) that can manifest when using powershell installation scripts with SCCM. To workaround the problem, I take two measures:

  1. I always have SCCM invoke a batch file which runs the powershell script by invoking powershell.exe explicitly.
  2. I ensure that every code path in the installation script ends in an explicit call to [System.Environment]::Exit().

With these two measures, I haven't had a problem related to exit codes. That is a huge win because troubleshooting exit codes of installation scripts is a slow process because you have to wait for the SCCM client to invoke your script for each iteration of troubleshooting.

Here is what the batch file and powershell script look like:

Install-Application.bat

powershell.exe .\Install-Application.ps1
exit /b %errorlevel%

Install-Application.ps1

try 
{
    # do a bunch of installation stuff
    if ( $rebootNeeded )
    {
        [System.Environment]::Exit(3010)
    }

    [System.Environment]::Exit(0)
}
catch
{
    [System.Environment]::Exit(1)
}

Why are exit codes unreliable when SCCM invokes powershell scripts directly?

Here is how we know exit codes are unreliable:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.