lit
lit

Reputation: 16236

Cannot seem to identify type

Modeled on the example in Get-Help about_Type_Operators:

PS C:\> (get-culture) -is [System.Globalization.CultureInfo]
True

I am trying to do just about the same thing with a different type. Why does this fail? I copied the type name from the output of Get-TypeData.

(My apologies for the original question using is instead of -is.)

This suggestion did not work.

PS C:\> (Get-WMIObject -Class Win32_BIOS) -is [System.Management.ManagementObject#root\cimv2\Win32_BIOS]
Unable to find type [System.Management.ManagementObject#root\cimv2\Win32_BIOS].
At line:1 char:1
+ (Get-WMIObject -Class Win32_BIOS) -is [System.Management.ManagementOb ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Manageme...imv2\Win32_BIOS:TypeName)
    [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

On a related note, what is the purpose of each of these?

PS C:\> Get-TypeData | Where-Object {$_.TypeName -like '*Win32_BIOS' }

TypeName                                                              Members
--------                                                              -------
System.Management.ManagementObject#root\cimv2\Win32_BIOS              {}
Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_BIOS {}

Upvotes: 1

Views: 374

Answers (2)

Lance U. Matthews
Lance U. Matthews

Reputation: 16606

Assuming...

PS> $bios = Get-WmiObject -Class Win32_BIOS

...you can use the __CLASS system property to test the specific WMI class of an object like this...

PS> $bios.__CLASS -eq 'Win32_BIOS'
True

...or this...

PS> $bios.SystemProperties['__CLASS'].Value -eq 'Win32_BIOS'
True

You might also test the namespace just to be really sure you've got the right class:

PS> $bios.__NAMESPACE -eq 'root\cimv2' -and $bios.__CLASS -eq 'Win32_BIOS'
True

Note that the comparisons above don't work exactly like -is because you are testing for the exact class, whereas -is takes the class hierarchy into account. That is, the following fails even though Win32_BIOS inherits from CIM_BIOSElement:

PS> $bios.__CLASS -eq 'CIM_BIOSElement'
False

The reason why $bios | Get-Member shows System.Management.ManagementObject#root\cimv2\Win32_BIOS as the type name is because Win32_BIOS and its inheritance chain have been added to the TypeNames property...

PS> $bios.PSObject.TypeNames
System.Management.ManagementObject#root\cimv2\Win32_BIOS
System.Management.ManagementObject#root\cimv2\CIM_BIOSElement
System.Management.ManagementObject#root\cimv2\CIM_SoftwareElement
System.Management.ManagementObject#root\cimv2\CIM_LogicalElement
System.Management.ManagementObject#root\cimv2\CIM_ManagedSystemElement
System.Management.ManagementObject#Win32_BIOS
System.Management.ManagementObject#CIM_BIOSElement
System.Management.ManagementObject#CIM_SoftwareElement
System.Management.ManagementObject#CIM_LogicalElement
System.Management.ManagementObject#CIM_ManagedSystemElement
System.Management.ManagementObject
System.Management.ManagementBaseObject
System.ComponentModel.Component
System.MarshalByRefObject
System.Object

The actual type is still ManagementObject...

PS> $bios.GetType().FullName
System.Management.ManagementObject

Upvotes: 3

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10799

You are using the string is as the comparison operator; however, all comparison operators begin with the hyphen, so you should be using -is: (Get-WMIObject -Class Win32_BIOS) -is [System.Management.ManagementObject...]

Upvotes: 3

Related Questions