I'm working on a PowerShell script to provide clear error messages when a specific application isn't installed. The script is going to be run through Intune Scripts and Remediation, which only captures the last output of the PowerShell script. Here's what I've written so far:
```powershell
$Application = get-package "Application" -ErrorAction Stop | Where-Object { $_.metadata['installlocation'] }
if (!(Test-Path $Folder)) {
try {
Write-Output $Application
}
catch {
Write-Output "Application not installed"
}
}
```
The issue I'm running into is that it displays an error message that the package can't be found, along with some default error codes from PowerShell, but the catch error for "Application not installed" isn't visible. I've also tried using Write-Host and Write-Error in the catch block, but nothing displays. What am I doing wrong?
1 Answer
It seems your command `$Application...` is causing the script to stop before reaching the try/catch. The `-ErrorAction Stop` option means if that line fails, it exits the entire script. If you want to use try/catch, either handle potential nulls by checking if `$Application` is `$null`, or put the `Get-Package` command directly inside the try block as I suggested earlier.
I overlooked that! Thanks for pointing it out. I’ve sorted it now!