Complex wmi queries using execquery

3

1

In powershell I can do a query like

> get-wmiobject -query 'select * from win32_groupuser' | % {[wmi]$_.partcomponent|select domain,name,SID}

Which will list for each wmi, the domain, name and sid for each user.

How would I accomplish this using vbscript?

I currently use execquery:

Dim strComputer, objWMIService
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"& strComputer & "\root\cimv2")
objWMIService.ExecQuery("select partcomponent,groupcomponent from win32_groupuser")

for the first part, but how do I incorporate the equivalent of | % {[wmi]$_.partcomponent|select domain,name,SID}

Alternatively, how could I do

gwmi win32_groupuser | % { [wmi]$_.partcomponent | select domain,name,sid}

using vbscript; as it is not a query, I cannot use execquery, but it has the same output as the original query.

A G

Posted 2016-04-28T13:17:32.647

Reputation: 463

Answers

0

ASSOCIATORS OF Statement

The ASSOCIATORS OF statement retrieves all instances that are associated with a particular source instance. The instances that are retrieved are referred to as the endpoints. Each endpoint is returned as many times as there are associations between it and the source object.

Sample script:

option explicit
Dim sResult, strComputer, objWMIService, group, groups, user, users
sResult = ""

strComputer = "."
Set objWMIService = GetObject(_
  "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set groups = objWMIService.ExecQuery( "SELECT * FROM Win32_Group" )
For Each group in groups
  Set users = objWMIService.ExecQuery( _
    "Associators of {Win32_Group.Domain='" & group.Domain _
        & "',Name='" & group.Name & "'} " _
        & "Where AssocClass = Win32_GroupUser ResultRole = PartComponent")
  For Each user in users
    sResult = sResult & vbNewLine & group.Domain & " " & group.Name _
        & vbTab & user.Name & vbTab & user.SID
  Next
Next

Wscript.Echo sResult

JosefZ

Posted 2016-04-28T13:17:32.647

Reputation: 9 121