Results 1 to 4 of 4

Thread: how to pull local group members in vbscript?

  1. #1
    Join Date
    Jun 2009
    Posts
    18

    Post how to pull local group members in vbscript?

    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. #2
    Join Date
    Oct 2005
    Posts
    2,394

    Re: how to pull local group members in vbscript?

    Try to use the below code for pulling out local group members in Vbscript

    Code:
    Main Sub. 
    ' Global variables. 
    Dim objBook 
    Dim intRowGroup 
    Dim XL 
    Dim FSO 
    Dim strGroupOnly 
    Dim strDomain 
    ' Create Global instance of Excel object. 
    Set XL = WScript.CreateObject("Excel.Application") 
    ' Initialize Global File System Object 
    Set FSO = CreateObject("Scripting.FileSystemObject") 
    ' Start to write to row 6 in Excel File. 
    intRowGroup = 6 
    ' Create Excel File. 
    BuildSpreadSheet 
    ' Create GroupList text file. 
    CreateGroupList 
    ' Find users for each group. 
    FindUsersInGroup 
    ' Save Excel File. 
    SaveSpreadSheet
    
    --------------------------------------------------------
    
    Sub CreateGroupList() 
    On Error Resume Next 
    Dim rootDSE 
    Dim strDomain 
    Dim adodbConn 
    Dim objCatalog 
    Dim objCommand 
    Dim rsConnection 
    Dim strGroupScope 
    Dim strSecurityGroup 
    ' Constants for all types of group in domain. 
    Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h00000002 
    Const ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP = &h00000004 
    Const ADS_GROUP_TYPE_LOCAL_GROUP = &h00000004 
    Const ADS_GROUP_TYPE_UNIVERSAL_GROUP = &h00000008 
    Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000 
    ' Concatenate the two type for a Global Security group. 
    strGroupScope = ADS_GROUP_TYPE_GLOBAL_GROUP or ADS_GROUP_TYPE_SECURITY_ENABLED 
    ' Connect to domain. 
    Set RootDSE = GetObject("[link=http://www.visualbasicscript.com/ldap://RootDSE]LDAP://RootDSE[/link]") 
    Set strDomain = GetObject("LDAP://"&RootDSE.get("DefaultNamingContext")) 
    ' Select query to filter only Global Security Group. 
    strQuery="Select cn,distinguishedname,groupType from '" & _ 
    strDomain.ADSPath & "' Where objectclass='group' AND GroupType='" & strGroupScope & "'" 
    'WScript.Echo strQuery 
    ' Connect to Global Catalog in domain. 
    Set objCatalog = GetObject("GC:") 
    For Each objFound In objCatalog 
    Set objGC = objFound 
    Next 
    ' Get the path of Global Catalog. 
    AdsPath = objGC.ADSPath 
    ' Open a connection to Active Directory Directory. 
    Set adodbConn = Createobject("ADODB.Connection") 
    Set objCommand = CreateObject("ADODB.Command") 
    adodbConn.Provider = "ADSDSOObject" 
    adodbConn.Open 
    ' Open a recordset to populate all Global Security Groups in AD. 
    Set objCommand.ActiveConnection = adodbConn 
    Set rsConnection = adodbConn.Execute(strQuery) 
    Do While Not rsConnection.EOF 
    strSecurityGroup = strSecurityGroup & rsConnection.Fields("distinguishedname") & vbcrlf 
    rsConnection.Movenext 
    Loop 
    ' Close connection variables 
    rsConnection.Close 
    adodbConn.Close 
    Set adodbConn=Nothing 
    Set objCommand=Nothing 
    Set RootDSE=Nothing 
    Set objCatalog=Nothing 
    Set rsConnection=nothing 
    ' Create a text file containing all Global Security Groups for later use. 
    CreateTempFile strSecurityGroup, "GroupList.txt" 
    End Sub 
    -------------------------------------------
    
    Sub CreateTempFile (strSource, strFileName) 
    Dim strTempFile 
    ' Create text file based on strFileName containing values from strSource. 
    Set strTempFile = FSO.OpenTextFile(strFileName, 2, True) 
    strTempFile.WriteLine(strSource) 
    strTempFile.Close 
    End Sub 
    
    ---------------------------------------------------
    
    Function FindUsersInGroup() 'Find all members in each group from a text file. 
    Dim arrMemberOf 
    Dim objGroup 
    Dim strUsers 
    Dim strTemp 
    Dim intPos 
    Dim strGroup 
    Dim strFirstChar 
    Dim strTempFile 
    Dim strMember 
    On Error Resume Next 
    ' Open text file GroupList.txt containing all Global Security Groups. 
    Set strTempFile = FSO.OpenTextFile("GroupList.txt",1, True) 
    ' Store first line to save Excel file later. 
    strDomain = strTempFile.ReadLine 
    Do Until strTempFile.AtEndOfStream 
    ' Read one group at a time. 
    strGroup = strTempFile.ReadLine 
    ' Check if there is something in strGroup. 
    ' I don't know why last group is always empty. 
    strFirstChar = Left(strGroup,1) 
    If Len(strGroup) = 0 Then 
    Exit Function 
    End If 
    ' Connect to this group in AD. 
    Set objGroup = GetObject("LDAP://" & strGroup) 
    objGroup.GetInfo 
    ' Parse current line to extract only group name. 
    strGroupOnly = GetGroupOnly(strGroup) 
    ' Get all members in current group. 
    arrMemberOf = objGroup.GetEx("member") 
    
    ---------------------------------------------------------
    
    For Each strMember in arrMemberOf 
    strTemp = strMember 
    ' Parse current line to extract only user name. 
    If InStr(strTemp,",") Then 
    intPos = InStr(strTemp, ",") 
    strTemp = Mid(strTemp, 1, intPos - 1) 
    strTemp = Mid(strTemp,4) 
    End if 
    ' Concatenate all users for current group. 
    strUsers = strUsers & strTemp & vbcrlf 
    Next 
    ' Create temporary text file containing all users for current group. 
    CreateTempFile strUsers, "UserList.txt" 
    ' Call procedure to add current group and all users it contain in the Excel file. 
    AddGroupToSpreadSheet strGroupOnly, "UserList.txt" 
    ' Reset variable. 
    strUsers = "" 
    arrMemberOf = "" 
    Loop 
    End Function 
    
    --------------------------------------
    
    Sub AddGroupToSpreadSheet(strGroup, strUsers) ' Add one group at a time with all members in an Excel file. 
    Dim strTempFile 
    ' Will insert a blank line in Excel file after last group. 
    intRowGroup = intRowGroup + 1 
    ' Write current group in first column. 
    XL.Cells(intRowGroup, 1).Value = UCase(strGroup) 
    ' Open temporary text file containing users for current group. 
    Set strTempFile = FSO.OpenTextFile(strUsers,1, True) 
    Do Until strTempFile.AtEndOfStream 
    ' Write users for current group in third column. 
    XL.Cells(intRowGroup, 3).Value = strTempFile.ReadLine 
    intRowGroup = intRowGroup + 1 
    Loop 
    strTempFile.Close 
    End Sub 
    
    Function GetDomain(strFile) 
    Dim strTemp 
    Dim intPos 
    ' Parse line to extract only the Domain name. 
    ' strList contain CN=Domain Computers,CN=Users,DC=Mydomain,DC=local 
    If InStr(strFile,"DC=") Then 
    intPos = InStr(strFile, "DC=") 
    strTemp = Mid(strFile, intPos + 3) 
    If InStr(strTemp, ",") Then 
    intPos = InStr(strTemp, ",") 
    strTemp = Mid(strTemp, 1, intPos - 1) 
    End If 
    End If 
    ' strTemp contain only Mydomain 
    GetDomain = strTemp 
    End Function 
    '-------------------------------------------------------------------------------------------------- 
    
    Sub BuildSpreadSheet() ' Create Excel File. 
    ' Format cells from row 1 to 3 with fixed width column. 
    XL.Visible = True 
    Set objBook = XL.WorkBooks.Add 
    XL.Columns(1).ColumnWidth = 35 
    XL.Columns(2).ColumnWidth = 1 
    XL.Columns(3).ColumnWidth = 65 
    'XL.Cells(1, 4).Value = objDir 
    ' Create Header for column 1 and 3 
    XL.Cells(2, 1).Value = "Legend:" 
    XL.Cells(2, 3).Value = "Date of extraction " & Now() 
    XL.Cells(4, 1).Value = "Group Name" 
    XL.Cells(4, 3).Value = "User in Group" 
    ' Select range and format it Bold with font size 12. 
    XL.Range("A1:D4").Select 
    XL.Selection.Font.Bold = True 
    XL.Selection.Font.Size = 12 
    End Sub 
    '-------------------------------------------------------------------------------------------------- 
    
    Sub SaveSpreadSheet() ' Save Excel file. 
    Dim ObjWorkbook 
    Dim strDate 
    Dim strXlFile 
    Dim strDomainName 
    ' Parse strDomain variable to extract Domain name. 
    strDomainName = GetDomain(strDomain) 
    Set objWorkbook = XL.ActiveWorkbook 
    strDate = Date 
    strDate = Replace(strDate, "/", "-") 
    ' Find script path to save Excel file in it with prefix + current date and .xls extension. 
    strXlFile = GetScriptPath() & "GlobalSecGroup_InDomain_" & strDomainName & "_" & strDate & ".xls" 
    strXlFile = XL.GetSaveAsFilename(strXlFile) 
    If strXlFile <> False Then 
    ObjWorkbook.SaveAs(strXlFile) 
    End If 
    'XL.Quit 
    End Sub 
    '-------------------------------------------------------------------------------------------------- 
    Function GetScriptPath() ' Find path from where this script is executed. 
    GetScriptPath = MID(Wscript.ScriptFullName, 1, InstrRev(Wscript.ScriptFullName,"\")) 
    End Function

  3. #3
    Join Date
    Jun 2009
    Posts
    18

    Re: how to pull local group members in vbscript?

    hi reegan,

    thanks for your replay i found the code for my requriment, i will post my code below, what i requried is i need to produce ouput with their domain rightnow what i am getting is local group and their members say for example if the user(ABC) belongs to paticular domain(say XYZ) i need to get the result as XYZ/ABC .this how it is in member column but what i am getting is only ABC (to be clear its fetchs me only ABC)pls help me in this regards.

    strComputer = "USAADST0GMDEV25"
    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

  4. #4
    Join Date
    Mar 2009
    Posts
    2

    ThumbsUp Re: how to pull local group members in vbscript?

    Hi

    Net localgroup "Local Group Name"

    This will extract the all the users in local group

    Regards
    Rajesh J S

Similar Threads

  1. Listing members of Group with >1500 members
    By Umesh Thakur in forum Windows Server Help
    Replies: 11
    Last Post: 03-02-2012, 04:29 AM
  2. Display members of a group with more than 1500 members
    By Simon G in forum Windows Server Help
    Replies: 5
    Last Post: 25-10-2011, 12:35 PM
  3. Maybe a VBScript to pull email addresses?
    By Modom in forum Active Directory
    Replies: 1
    Last Post: 03-08-2011, 04:21 PM
  4. how to pull NTFS security permission in vbscript
    By vivekmohan in forum Software Development
    Replies: 3
    Last Post: 25-07-2009, 01:28 PM
  5. Replies: 4
    Last Post: 04-06-2008, 05:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •