简体   繁体   English

如何从WMI对象引用中获取WMI对象?

[英]How to get WMI object from a WMI object reference?

Similar to this question except that no answer was given with regards to the main question of getting an object from reference. 类似于此问题,不同之处在于未回答从引用中获取对象的主要问题。

For example: 例如:

PS C:\Users\admin> Get-WmiObject -Namespace $namespace -Class $class


    ...

IsActive     :  1
oA: \\.\ROOT\abc\abc\ABC:abc.xyz="tst2"
oB : \\.\ROOT\abc\abc\ABC:abc.xyz="tst3"
PSComputerName         : admin-test2

oA and oB are references and therefore come up as strings in powershell. oAoB是引用,因此在powershell中作为字符串出现。 Is there a way I can get the object they represent using WMI query in powershell? 有没有一种方法可以在Powershell中使用WMI查询获取它们表示的对象?

Assuming that oA and oB actually are strings you should be able to resolve these WMI paths to WMI objects like this: 假设oAoB实际上是字符串,您应该能够将这些WMI路径解析为WMI对象,如下所示:

Get-WmiObject -Namespace $namespace -Class $class | ForEach-Object {
    $oA = [wmi]$_.oA
    $oB = [wmi]$_.oB
}

Example: 例:

PS C:\> $namespace = 'root/cimv2'
PS C:\> $class = 'Win32_OperatingSystem'
PS C:\> $obj1 = Get-WmiObject -Namespace $namespace -Class $class
PS C:\> $obj1

SystemDirectory : C:\Windows\system32
Organization    :
BuildNumber     : 7601
RegisteredUser  : foo
SerialNumber    : 00371-OEM-8310595-XXXXX
Version         : 6.1.7601


PS C:\> $obj1.GetType().FullName
System.Management.ManagementObject
PS C:\> $obj1.Path.Path
\\FOO\root\cimv2:Win32_OperatingSystem=@
PS C:\> ($obj1.Path.Path).GetType().FullName
System.String
PS C:\> $obj2 = [wmi]$obj1.Path.Path
PS C:\> $obj2

SystemDirectory : C:\Windows\system32
Organization    :
BuildNumber     : 7601
RegisteredUser  : foo
SerialNumber    : 00371-OEM-8310595-XXXXX
Version         : 6.1.7601


PS C:\> $obj2.GetType().FullName
System.Management.ManagementObject

Your question is rather vague, though, so I'm not sure if this answer actually covers what you've been asking. 不过,您的问题很模糊,因此我不确定这个答案是否真的涵盖了您一直在问的问题。

As OP mentioned that all he wants is a generic answer (which is again tough given the nature of Object Paths and dependency on key), I am giving another example of using Associators Of WMI query. 正如OP提到的,他想要的只是一个通用的答案(考虑到对象路径的性质和对键的依赖,这再困难不过了),我在举另一个使用WMI查询关联器的示例。

$query = "ASSOCIATORS OF {Win32_Account.Name='DemoGroup2',Domain='DomainName'} WHERE Role=GroupComponent ResultClass=Win32_Account"
Get-WMIObject -Query $query | Select Name

If you need to use the example above, you need to first find out what is the key property and use that in the object path. 如果需要使用上面的示例,则需要首先找出什么是键属性,然后在对象路径中使用它。

-----Original answer ----- -----原始答案-----

What namespace? 什么名称空间? What class? 什么级别? You need to use associations and/or references to retrieve that. 您需要使用关联和/或引用来检索它。 It is hard to give a generic answer unless we know the exact object path. 除非我们知道确切的对象路径,否则很难给出通用答案。 For example, 例如,

$query = "REFERENCES OF {Win32_Service.Name='Netlogon'} WHERE ClassDefsOnly"
Get-WMIObject -Query $query

The above query will give all references of Win32_Service with an object path ServiceName='NetLogon' 上面的查询将使用对象路径ServiceName ='NetLogon'给出Win32_Service的所有引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM