O GetWMICOMException é um erro de não finalização, o que significa que com um $ErrorActionPreference de Continue padrão, o código no bloco try continuará sua execução depois de gravar a exceção como um erro
Coloque a chamada Get-WmiObject com try-catch bloqueia, mas certifique-se de que -ErrorAction esteja definido como Stop :
# Try-Catch block starts
try
{
# Call Get-WmiObject
Get-WmiObject -ComputerName $computer -Class "Win32_NetworkAdapter" -ErrorAction Stop
}
# If an Exception of the type COMException is thrown, execute this block
catch [System.Runtime.InteropServices.COMException]
{
# You can inspect the error code to see what specific error we're dealing with
if($_.Exception.ErrorCode -eq 0x800706BA)
{
# This is instead of the "RPC Server Unavailable" error
Write-Error -Message "Your own custom message"
}
else
{
Write-Error -Message "Some other COMException was thrown"
}
}
# If any other type of Exception is thrown, execute this block
catch [System.Exception]
{
Write-Error -Message "Some other exception that's nothing like the above examples"
}
# When all of the above has executed, this block will execute
finally
{
Write-Verbose "Get-WmiObject object was executed"
}
Como alternativa, você pode definir o $ErrorActionPreference para Stop antes de executar seu script:
# Before the rest of the script
$ErrorActionPreference = Stop
Para mais ajuda sobre a construção try-catch-finally:
Get-Help about_Try_Catch_Finally -Full
Para mais ajuda sobre o $*Preference vars:
Get-Help about_Preference_variables -Full