0

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. Is there a way I can get the object they represent using WMI query in powershell?

Community
  • 1
  • 1
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152

2 Answers2

6

Assuming that oA and oB actually are strings you should be able to resolve these WMI paths to WMI objects like this:

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
3

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.

$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'

ravikanth
  • 24,922
  • 4
  • 60
  • 60
  • Then, don't down vote my answer. It shows you an example of how to get references. – ravikanth Aug 07 '13 at 04:41
  • Thanks @Saher. Whoever did that should at least leave a comment. – ravikanth Aug 07 '13 at 07:27
  • Thank you. I will try your suggestion tomorrow and let you know – Saher Ahwal Aug 07 '13 at 07:28
  • I couldn't get it to work. I didn't know what to substitute for my the class I am using – Saher Ahwal Aug 09 '13 at 21:28
  • We can't help beyond this unless we know what is the class and key attributes of the class. – ravikanth Aug 10 '13 at 02:02
  • I cannot see how it depends on class? if I tell you class name is `className` and key attribute is `Key`? The classes do not exist yet that is why I cannot say anything about them... – Saher Ahwal Aug 10 '13 at 02:31
  • LOL! then, this is not a valid question at all. Read more about WMI references and associations. You will know how it depends. – ravikanth Aug 10 '13 at 02:42
  • I am wondering, if the classes did not exist, how did you verify my answer? – ravikanth Aug 10 '13 at 02:45
  • no I mean don't exist in the build, they are new classes. even if I give them to you, you will not have them – Saher Ahwal Aug 10 '13 at 02:47
  • Sorry, we cannot help beyond this and the question is very broad. Consider closing it unless you understand the dependencies. Even when your classes exist in the build, a generic answer I provided there is the way to get references and associations. There is nothing much to discuss beyond this. – ravikanth Aug 10 '13 at 02:49
  • how can I close it when I have open bounty – Saher Ahwal Aug 10 '13 at 21:33