Stop Feature upgrade if precheck fails

lalajee 1,821 Reputation points
2020-10-14T09:56:52.947+00:00

Hi,
I'm using an action custom script to run a Powershell script to check for a few things before running the feature upgrade on a machine but even the machine is not compliant it still running the upgrade.

for example, I have this code in preinstall.cmd

if(-not (Test-path $path))
{
write-error "Stop"
exit 1

}

this should stop the upgrade but its not happening.

Does anyone know how I can stop the upgrade if it doesnt meet my preinstall checks

Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Intune | Configuration Manager | Updates
Microsoft Security | Intune | Configuration Manager | Deployment
0 comments No comments
{count} votes

Answer accepted by question author
  1. lalajee 1,821 Reputation points
    2020-10-15T16:02:20.527+00:00

    This is how I fix the issue which I have.

    In powershell i type this code

    if(-not (Test-path $path))
    {
    cmd /c "exit 10"
    Throw "error"
    }

    in the cmd file after the powershell script i put this code

    exit /b %ERRORLEVEL%

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 48,026 Reputation points
    2020-10-14T14:36:26.887+00:00

    I suppose that depends on how your script is run. If the executing (i.e. calling) script examines the $LastErrorCode variable it'll see that it's a "1". How it would react to that I don't know. On the other hand PowerShell usually manages errors by using exceptions.

    So . . instead of "Exit 1", try something like "Throw 'a meaningful exception message goes here'".


  2. Gary Blok 1,756 Reputation points MVP
    2020-10-14T17:45:06.603+00:00

    Just confirming, that you're having the preinstall.cmd call a powershell script, since I see you are using powershell code.
    If you're able to successfully exit the preinstall.cmd script with a non-zero exit code, it will pass the error to the setup engine, which will convert the error code to a pre-defined code

    0XC19001E2: MOSETUP_E_PREINSTALL_SCRIPT_FAILED A preinstall script failed to execute or returned an error.

    https://garytown.com/windows-10-upgrade-custom-action-scripts

    0 comments No comments

  3. Rich Matheisen 48,026 Reputation points
    2020-10-15T18:21:58.397+00:00

    What's missing from your original description is how you're running your PowerShell script.

    If you run it using PowerShell -command {"Boom again";exit 2} then the %ERRORLEVEL% isn't set to '2'.

    However, if you placed "Boom again";exit 2 in a .ps1 file and then ran PowerShell -File BOOM.ps1 the %ERRORLEVEL% is set the way you expect

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.