Try catch for Unauthorized error exception

Does anyone know how to catch this specific error using try catch?

Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At C:\Install\powershell\Retrieve.ps1:66 char:15

  • $shares = gwmi <<<< Win32_Share -computername $fileServer -filter ‘type=0’
    • CategoryInfo : NotSpecified: (:slight_smile: [Get-WmiObject], UnauthorizedAccessException
    • FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

To catch in in Try catch, you just need to use it :-).

Awesome examples here: about Try Catch Finally - PowerShell | Microsoft Docs