Trouble with PowerShell Try-Catch not working

1

I'm having trouble with Try-Catch in PowerShell.

Here is the code I'm trying to use (note that $Wksn is passed to this routine):

$BitLockerNameSpace = "root\cimv2\security\MicrosoftVolumeEncryption"
$BitLockerClass = "Win32_EncryptableVolume"
$Filter = "DriveLetter = 'C:'"

Try {
   $BitLocker = Get-WmiObject -namespace $BitLockerNameSpace -Class $BitLockerClass -Filter $Filter -ComputerName $Wksn
} Catch {
   $SimplifiedStatus = "Cannot connect"
}

When I test this against a computer where I know RPC isn't available or against a computer is not valid, it doesn't "catch" the error:

Get-WmiObject : The RPC server is unavailable. (Exception from
HRESULT: 0x800706BA) At C:\Temp\Untitled67.ps1:39 char:20
+ ... BitLocker = Get-WmiObject -namespace $BitLockerNameSpace -Class $BitL ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

What am I missing?

powershell
asked on Stack Overflow Mar 26, 2021 by mconwell

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0