I have my silent installation PowerShell script but I have issue that even if some of the arguments are not correct my execution of the powershell script will not fail.
$expression="C:\app\tool.exe /COMPUTER=server1 /INSTALL /SILENT"
try {
Invoke-Expression $expression -ErrorAction Stop
}
catch {
Write-Host "Entered Exception"
}
Unfortunately it's always "green", like execution was successful and no Errors have been thrown.
In the log however I can see that it was "Unintended cancel" of the setup.
******************************************************** Error:
Connect to computer registry failed
Computer 'SERVER1' does not exist
The network path was not found.
Unattended setup >>> 'Cancel'
********************************************************
Exit code = 0x4C7
My script does not enter the catch clause.
Is it possible that I can somehow force this to fail, so I can catch that error.
I am having big issue that my powershell does not fail, although it should because it got wrong installation argument.
Thank you!!!
"C:\app\tool.exe /COMPUTER=server1 /INSTALL /SILENT"Can you please tell me why it doesn't then throw error if I run .exe file directly?$LASTEXITCODEat the end of execution of exe. if status is not 0, throw error. I have posted it in the answer for reference.Invoke-Expression(iex) should generally be avoided; definitely don't use it to invoke an external program or PowerShell script.Invoke-Expression), which means that PowerShell's error-handling features (e.g.try { ... } catch { ... }) do not apply (except accidentally, due to a bug with redirections up to v7.1). Instead, check if$LASTEXITCODEis nonzero to determine whether an external program failed. Future PowerShell versions may offer an opt-in mechanism for integrating external-program calls into PowerShell's error handling. See the linked duplicate for details.