Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 286351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:37:43+00:00 2026-05-12T05:37:43+00:00

I am new to powershell, and I am trying to add error handling via

  • 0

I am new to powershell, and I am trying to add error handling via try/catch statements, but they don’t seem to actually be catching the error. This is powershell v2 CP3.

$objComputer = $objResult.Properties;
$strComputerName = $objComputer.name
write-host "Checking machine: " $strComputerName

try
{
    $colItems = get-wmiobject -class "Win32_PhysicalMemory" -namespace "root\CIMV2" -computername $strComputerName -Credential $credentials
    foreach ($objItem in $colItems) 
    {
        write-host "Bank Label: " $objItem.BankLabel
        write-host "Capacity: " ($objItem.Capacity / 1024 / 1024)
        write-host "Caption: " $objItem.Caption
        write-host "Creation Class Name: " $objItem.CreationClassName      
        write-host
    }
}
Catch 
{
    write-host "Failed to get data from machine (Error:"  $_.Exception.Message ")"
    write-host
}
finally 
{ }  

When it fails to contact a specific machine, I get this in console, and not my clean catch message:

Get-WmiObject : The RPC server is
unavailable. (Exception from HRESULT:
0x800706BA) At Z:\7.0 Intern
Programvare\Powershell\Get memory of
all computers in AD.ps1:25 char:34
+ $colItems = get-wmiobject <<<< -class "Win32_PhysicalMemory"
-namespace "root\CIMV2" -computername $strComputerName -Credential
$credentials
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject],
COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:37:44+00:00Added an answer on May 12, 2026 at 5:37 am

    I was able to duplicate your result when trying to run a remote WMI query. The exception thrown is not caught by the Try/Catch, nor will a Trap catch it, since it is not a “terminating error”. In PowerShell, there are terminating errors and non-terminating errors . It appears that Try/Catch/Finally and Trap only works with terminating errors.

    It is logged to the $error automatic variable and you can test for these type of non-terminating errors by looking at the $? automatic variable, which will let you know if the last operation succeeded ($true) or failed ($false).

    From the appearance of the error generated, it appears that the error is returned and not wrapped in a catchable exception. Below is a trace of the error generated.

    PS C:\scripts\PowerShell> Trace-Command -Name errorrecord  -Expression {Get-WmiObject win32_bios -ComputerName HostThatIsNotThere}  -PSHost
    DEBUG: InternalCommand Information: 0 :  Constructor Enter Ctor
    Microsoft.PowerShell.Commands.GetWmiObjectCommand: 25857563
    DEBUG: InternalCommand Information: 0 :  Constructor Leave Ctor
    Microsoft.PowerShell.Commands.GetWmiObjectCommand: 25857563
    DEBUG: ErrorRecord Information: 0 :  Constructor Enter Ctor
    System.Management.Automation.ErrorRecord: 19621801 exception =
    System.Runtime.InteropServices.COMException (0x800706BA): The RPC
    server is unavailable. (Exception from HRESULT: 0x800706BA)
       at
    System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at System.Management.ManagementScope.InitializeGuts(Object o)
       at System.Management.ManagementScope.Initialize()
       at System.Management.ManagementObjectSearcher.Initialize()
       at System.Management.ManagementObjectSearcher.Get()
       at Microsoft.PowerShell.Commands.GetWmiObjectCommand.BeginProcessing()
    errorId = GetWMICOMException errorCategory = InvalidOperation
    targetObject =
    DEBUG: ErrorRecord Information: 0 :  Constructor Leave Ctor
    System.Management.Automation.ErrorRecord: 19621801
    

    A work around for your code could be:

    try
    {
        $colItems = get-wmiobject -class "Win32_PhysicalMemory" -namespace "root\CIMV2" -computername $strComputerName -Credential $credentials
        if ($?)
        {
          foreach ($objItem in $colItems) 
          {
              write-host "Bank Label: " $objItem.BankLabel
              write-host "Capacity: " ($objItem.Capacity / 1024 / 1024)
              write-host "Caption: " $objItem.Caption
              write-host "Creation Class Name: " $objItem.CreationClassName      
              write-host
          }
        }
        else
        {
           throw $error[0].Exception
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to powershell, but I'm trying to output some simple logging in a
I'm new to powershell, but I'm trying to output some simple logging in a
I'm pretty familiar with PHP, Javascript, etc but I'm new to Powershell. I'm trying
I am new to PowerShell, I am trying to change a property of an
I'm fairly new to PowerShell, but have a strong C# background. The task I'm
In Powershell I am defining a new PSDrive called test . But when I
I am new to powershell and was trying to use the example script posted
Little new to powershell. I am trying to locate a get-childitem like command that
I'm trying to grab out some information from Active Directory using Powershell, but I
I'm trying to pass some arguments into a new Powershell remoting session using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.