locked
How do I get the target name of the PC that the PowerShell CIM or WMI command was run against from the error details? RRS feed

  • Question

  • Hi, I am hoping someone can help me.

    I have automated script that is running against 1000s of PCs. It does a test-connection and then a WMI call.

    In some cases, the test-connection passes, and then the WMI call fails. This isnt totally unexpected due to WinRM\permissions requirements etc. However, I want to know which connections have failed.

    However, the get-WMIobject cmd or replacement Get-cimclass cmd errors do not seem to contain the computername of the target object as Test-connection does. Instead I just get a large list of $errors stating at RPC has failed but neglecting to tell me connecting to which object out of the 1000s in the loop?

    I have simplified this issue in the code below (only 2 test servers). You can copy and run this locally in ISE as no servers are required, as we are testing what happens when connections fail!

    #How do I get the target name of the PC that the CIM or WMI command was run against from the error details?

    $com = "Server1","Server2"
    $TcError.Clear()
    $WmiError.Clear()
    $CimError.Clear()

    foreach ($c in $com) {
    Test-Connection $c -ev +TcError -count 1
    get-wmiobject -class Win32_OperatingSystem -ComputerName $c -ev +WmiError
    Get-CimClass -ClassName Win32_OperatingSystem -ComputerName $c -ev +CimError
    }

    echo "TC target objects"
    $TcError.targetobject
    echo "WMI target objects"
    $WmiError.targetobject
    echo "CIM target objects"
    $CimError.targetobject

     

    Does anyone know how I can get this information out of the $WmiError or $CimError error details or even add it to the error details as they are generated?

    My results from running the script above are:

    TC target objects (expected)
    Server1
    Server2
    WMI target objects
    CIM target objects
    root\cimv2:Win32_OperatingSystem
    root\cimv2:Win32_OperatingSystem




    • Edited by PeteMitch99 Tuesday, March 13, 2018 4:27 PM
    Tuesday, March 13, 2018 4:23 PM

Answers

  • For $CimError i found $CimError.origininfo.pscomputername - this returns the computername.

    Now I look closer at the get-CIMclass response it also has the name of the computer in the message ($CimError.Exception.Message). Its just the WMI error object that doesn't.

    I started this off as just a get-WMIobject query and added CIM as writing it - turns out I should have just investigated get-CIMclass / get-CIMInstance more! Looks like I need to switch over to the new CIM cmds!

    get-CIMInstance has the same behavior as get-CIMclass






    • Marked as answer by PeteMitch99 Tuesday, March 13, 2018 4:50 PM
    • Edited by PeteMitch99 Friday, March 16, 2018 10:32 AM update get-CIMInstance
    Tuesday, March 13, 2018 4:34 PM
  • This is because the exceptions with Cim come from WinRM when it is enabled.  Wmi only uses DCOM which does not support that form of exception.

    PS D:\scripts> get-wmiobject -class Win32_OperatingSystem -ComputerName $c -ev +WmiError
    
    PS D:\scripts> $WmiError[0].GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     ErrorRecord                              System.Object
    

    PS D:\scripts> $cimerror[0].GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     RemotingErrorRecord                      System.Management.Automation.ErrorRecord

    Notice the difference.  "ErrorRecord"  and "RemotingErrorRecord".  Remoting adds the extra information.


    \_(ツ)_/


    • Edited by jrv Tuesday, March 13, 2018 4:41 PM
    • Marked as answer by PeteMitch99 Tuesday, March 13, 2018 4:52 PM
    Tuesday, March 13, 2018 4:41 PM

All replies

  • The computer info is not available in the exception.  Use Try/Catch to detect the error.

    \_(ツ)_/

    Tuesday, March 13, 2018 4:31 PM
  • For $CimError i found $CimError.origininfo.pscomputername - this returns the computername.

    Now I look closer at the get-CIMclass response it also has the name of the computer in the message ($CimError.Exception.Message). Its just the WMI error object that doesn't.

    I started this off as just a get-WMIobject query and added CIM as writing it - turns out I should have just investigated get-CIMclass / get-CIMInstance more! Looks like I need to switch over to the new CIM cmds!

    get-CIMInstance has the same behavior as get-CIMclass






    • Marked as answer by PeteMitch99 Tuesday, March 13, 2018 4:50 PM
    • Edited by PeteMitch99 Friday, March 16, 2018 10:32 AM update get-CIMInstance
    Tuesday, March 13, 2018 4:34 PM
  • This is because the exceptions with Cim come from WinRM when it is enabled.  Wmi only uses DCOM which does not support that form of exception.

    PS D:\scripts> get-wmiobject -class Win32_OperatingSystem -ComputerName $c -ev +WmiError
    
    PS D:\scripts> $WmiError[0].GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     ErrorRecord                              System.Object
    

    PS D:\scripts> $cimerror[0].GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     RemotingErrorRecord                      System.Management.Automation.ErrorRecord

    Notice the difference.  "ErrorRecord"  and "RemotingErrorRecord".  Remoting adds the extra information.


    \_(ツ)_/


    • Edited by jrv Tuesday, March 13, 2018 4:41 PM
    • Marked as answer by PeteMitch99 Tuesday, March 13, 2018 4:52 PM
    Tuesday, March 13, 2018 4:41 PM