2017-05-18 46 views
0

仿照例子:似乎无法识别<code>Get-Help about_Type_Operators</code>型

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

我试图做的只是用不同的类型一样的东西。为什么这会失败?我从Get-TypeData的输出中复制了类型名称。

(我的道歉使用is代替-is原来的问题。)

这个建议没有工作。

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 

在相关说明中,这些每个的目的是什么?

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 {} 
+2

投票结束为一个简单的错字。你的情况下,gwmi对象的基类型是'(Get-WmiObject -Class win32_bios)-is [System.Management.ManagementBaseObject]' – Matt

+0

@Matt - 是的,这是有效的。因此,我们无法检查它是否是Win32_BIOS类型,如'Get-WmiObject -Class win32_bios | gm'? – lit

回答

2

假设...

PS> $bios = Get-WmiObject -Class Win32_BIOS 

...你可以使用__CLASSsystem property测试的特定WMI类的反对这样的...

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

...或...这

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

也可能测试的命名空间只是为了真的确保你有合适的班级:

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

注意的是,上面的比较不完全一样-is工作,因为你正在测试的确切类,而-is考虑了类层次结构。也就是说,下面的失败从CIM_BIOSElement即使Win32_BIOS继承:

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

之所以$bios | Get-Member显示System.Management.ManagementObject#root\cimv2\Win32_BIOS作为类型名字是因为Win32_BIOS和继承链已经被添加到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 

实际类型仍然是ManagementObject ...

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

您正在使用字符串is作为比较运算符;然而,所有的比较运营商开始用连字符,所以你应该使用-is(Get-WMIObject -Class Win32_BIOS) -is [System.Management.ManagementObject...]

+0

这似乎不适用于我的系统。我已经编辑了这个问题来包括这个尝试。 – lit

+0

你的问题似乎没有被编辑。 **'-is',而不是'**' –

+0

现在编辑。 – lit