Windows Powershell扩展类型系统

2019-09-30 13:53:08王振洲

             Printer Drivers

出现上面的情况,是因为WMI的所有对象都会以System.Management.ManagementObject类型返回。因此ETS没有出错,罪魁祸首是WMI这个特殊的类型。所以扩充ETS时一定要细化一个具体的类型。事实上WMI对象有一个PSTypeNames属性,通过它就可以找到更具体的类型。


PS C:Powershell> $object = Get-WmiObject Win32_Processor | Select-Object -first1
PS C:Powershell> $object.PSTypeNames
System.Management.ManagementObject#rootcimv2Win32_Processor
System.Management.ManagementObject
System.Management.ManagementBaseObject
System.ComponentModel.Component
System.MarshalByRefObject
System.Object

上面显示了WMI对象类型的继承层次。所以我们需求中要扩展的对象类型应该为:System.Management.ManagementObject#rootcimv2Win32_Processor

所以应当修改配置文件,重新加载更新。更新时会有一条异常
Update-FormatData : 加载格式数据文件时出错:
Microsoft.PowerShell,C:PowershellWin32_Processor.format.ps1xml: 文件被跳过,
因为该文件已在“Microsoft.PowerShell”中出现过。

异常可以忽略,然后重新测试。


PS C:Powershell> Get-WmiObject win32_Processor

Name         Description                    ID
----         -----------                    --
CPU0         x64 Family 6 Model 15 Stepp... BFEBFBFF000006FD

PS C:Powershell> Get-WmiObject win32_share

Name                       Path                       Description
----                       ----                       -----------
ADMIN$                C:Windows          Remote Admin
C$                         C:                        Default share