Stop Feature upgrade if precheck fails

lalajee 1,811 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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,446 questions
Microsoft Configuration Manager Updates
Microsoft Configuration Manager Updates
Microsoft Configuration Manager: An integrated solution for for managing large groups of personal computers and servers.Updates: Broadly released fixes addressing specific issue(s) or related bug(s). Updates may also include new or modified features (i.e. changing default behavior).
1,005 questions
Microsoft Configuration Manager Deployment
Microsoft Configuration Manager Deployment
Microsoft Configuration Manager: An integrated solution for for managing large groups of personal computers and servers.Deployment: The process of delivering, assembling, and maintaining a particular version of a software system at a site.
929 questions
0 comments No comments
{count} votes

Accepted answer
  1. lalajee 1,811 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 45,596 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,736 Reputation points
    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 45,596 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