Announcement

Collapse
No announcement yet.

how to pull members of localgroups?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to pull members of localgroups?

    hi everyone ,

    here is the sample script which capture me the local group in server where i run the script , i need to pull all associcated members of all local groups in that server,

    i tried using objitem.members and memberof but no use

    i am inserting the code for your reference
    ----------- vbscript code-----------------------------
    On Error Resume Next
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_Group Where LocalAccount = True")
    For Each objItem in colItems
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "Domain: " & objItem.Domain
    Wscript.Echo "Local Account: " & objItem.LocalAccount
    Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "SID: " & objItem.SID
    Wscript.Echo "SID Type: " & objItem.SIDType
    Wscript.Echo "Status: " & objItem.Status
    Wscript.Echo
    Next

  • #2
    Re: how to pull members of localgroups?

    A previous thread was posted one minute before this one. I deleted that one because it was doubled with this thread. ::

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      how to pull local group members along with their domain?

      hi everyone,

      i am having the script with fetch me local groups and their members, what i need is the members which i am getiing from the server or computer is giving me only their name. i want their domain also in which the paticular user or group belongs to .

      say for example if i have computer name "XYZ" and their local groups are A,B,C.. etc each local group have some members let us say for A there are 10 members (let us take "QQQ""PPP"..etc as members)who belongs to different domain such as apac, usa,Aus etc . when i run the below code i am getting the result as only QQQ(name of the user alone not their domain in which the user or group belongs) what i need is i want the result as "apac\QQQ","aus\PPP"


      strComputer = "XYZ"
      Set colGroups = GetObject("WinNT://" & strComputer & "")
      colGroups.
      colGroups.Filter = Array("group")
      For Each objGroup In colGroups
      Wscript.Echo objGroup.Name
      For Each objUser in objGroup.Members
      Wscript.Echo vbTab & objUser.Name
      Next
      Next

      Comment


      • #4
        Re: how to pull members of localgroups?

        @vivekmohan
        You started a third thread for the same subject. I've merged the to last two because of the new info you added.

        please take some time to read the forum rules again!

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: how to pull members of localgroups?

          To administer or to document local objects the WinNT provider is used.
          But you'll need extra code to retrieve more information about the object.

          The example below uses WMI (like in your first script) to retrieve the information you require. I used WMI because it'll run faster than a WinNT solution for this.

          Code:
          Option Explicit
          
          Dim oWshNet: Set oWshNet = CreateObject("WScript.Network" ) 
          Dim strComputer: strComputer = oWshNet.ComputerName
          Dim objWMIService: Set objWMIService = GetObject(_
             "winmgmts:\\" & strComputer & "\root\CIMV2")
          
          call MembersOfAllLocalGroups
          
          Sub MembersOfAllLocalGroups
          '# globaly declared: strComputer, objWMIService
          
             With objWMIService: Dim strQuery
             strQuery = "SELECT * FROM Win32_Group where domain='" _
               & strComputer &"'"
             Dim objItem
             For Each objItem In .ExecQuery(strQuery)
               msgbox GetUsersFromGroup(objitem.Name)
             Next
             End With
          End Sub
          
          Function GetUsersFromGroup (strGroup)
          '# globaly declared: strComputer, objWMIService
          
             With objWMIService: Dim strQuery
               strQuery = "select * from Win32_GroupUser where " _
                 & "GroupComponent = " & chr(34) & "Win32_Group.Domain='" _
                 & strComputer & "',Name='" & strGroup & "'" & Chr(34)
               Dim Item, strObjectPath, Membername, MemberDomain
               Dim GrpMembers: GrpMembers = Empty
               Dim sN, lN, sD, lD
               For Each Item In .ExecQuery(strQuery)
                 strObjectPath = Item.PartComponent
                 sN = inStrRev(strObjectPath, "Name=""",-1,1)
                 lN = Len(strObjectPath)-(sN+6)
                 sD = inStrRev(strObjectPath, "Domain=""",-1,1)
                 lD = (sN-2)-(sD+8)
                 Membername = mid(strObjectPath, sN+6, lN)
                 MemberDomain = mid(strObjectPath, sD+8,lD)
                 GrpMembers =  GrpMembers & " - " _
                   & MemberDomain & "\" & Membername & vbNewLine
               Next
             End With
             If Len(GrpMembers) = 0 then GrpMembers = "<none>"
             GetUsersFromGroup = "Local Group:" & vbNewLine _
               & vbNewLine & """" & strComputer & "\" & strGroup & """" _
               & vbNewLine & vbNewLine & "Members:" & vbNewLine _
               & "--------------------------------------------" _
               & vbNewLine & GrpMembers
          End Function
          \Rems

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: how to pull members of localgroups?

            thanks Rems,

            The code what you presented is exactly i am looking for..

            thanks rems

            Comment

            Working...
            X