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%
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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%
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'".
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
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