简体   繁体   中英

Suppress Error Messages in PowerShell at get-package

I use PowerShell to check, if specific apps are installed on the users PC:

$Application_MicrosoftEdge   = get-package "Microsoft Edge" | % { $_.metadata['installlocation'] }
$Application_Microsoft365    = get-package *"Microsoft 365"* | % { $_.metadata['installlocation'] }
Write-Host "Microsoft Edge Path    : $Application_MicrosoftEdge"
Write-Host "Microsoft 365 Path     : $Application_Microsoft365"

This works quiet well , but if an application is not installed on the users PC, then an error message is shown (here "Microsoft 365" is not installed on the PC):

get-package : Für "*Microsoft 365*" wurde kein Paket gefunden.
In D:\Scripts\GetInstalledApp.ps1:12 Zeichen:32
+ $Application_Microsoft365    = get-package *"Microsoft 365"* | % { $ ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...lets.GetPackage:GetPackage) [Get-Package], Exception
    + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage

I tried the following way, but here an error message is shown:

Try {
  $Application_MicrosoftEdge   = get-package "Microsoft Edge" | % { $_.metadata['installlocation'] }
   $Application_Microsoft365    = get-package *"Microsoft 365"* | % { $_.metadata['installlocation'] }
}
Catch {
  # Place action on error here
}

I also tried to wrap it in $(... ) | out-null $(... ) | out-null to suppress the error, but this is also not working. ` Any idea?

The simplest way is to pass argument Ignore for common parameter -ErrorAction ( -EA ):

$Application_MicrosoftEdge   = get-package "Microsoft Edge" -EA Ignore | % { $_.metadata['installlocation'] }
$Application_Microsoft365    = get-package *"Microsoft 365"* -EA Ignore | % { $_.metadata['installlocation'] }

if( $Application_MicrosoftEdge ) {
    Write-Host "Microsoft Edge Path    : $Application_MicrosoftEdge"
}
if( $Application_Microsoft365 ) {
    Write-Host "Microsoft 365 Path     : $Application_Microsoft365"
}

On error, the pipeline output will be empty, which converts to $false in a boolean context, which we test using the if statements.


Alternatively, as Abraham Zinala commented, to make try / catch working, pass argument Stop for common parameter -ErrorAction ( -EA ) or set the preference variable $ErrorActionPreference = 'Stop' at the beginning of your script:

try {
    $Application_MicrosoftEdge   = get-package "Microsoft Edge" -EA Stop | % { $_.metadata['installlocation'] }
    $Application_Microsoft365    = get-package *"Microsoft 365"* -EA Stop | % { $_.metadata['installlocation'] }
}
catch {
    # Place action on error here
}

Using -EA Stop , errors are turned into script-terminating errors (exceptions), which must be caught using try / catch , otherwise the script would end prematurely.

Note that this code skips the 2nd get-package call, if the 1st one failed! This is because execution flow jumps straight from the first error location into the catch block. To handle errors separately for both get-package calls, you'd need two try / catch blocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM