Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Thread Tools Search this Thread
Old 22-07-2009
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
Reply With Quote
Old 22-07-2009
Join Date: Oct 2005
Posts: 2,389
Re: how to pull local group members in vbscript?

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

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. 
' Create GroupList text file. 
' Find users for each group. 
' Save Excel File. 


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_LOCAL_GROUP = &h00000004 
' Concatenate the two type for a Global Security group. 
' 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 
' 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" 
' 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 
' Close connection variables 
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) 
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) 
' 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 
' 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 = "" 
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 
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.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 
End If 
End Sub 
Function GetScriptPath() ' Find path from where this script is executed. 
GetScriptPath = MID(Wscript.ScriptFullName, 1, InstrRev(Wscript.ScriptFullName,"\")) 
End Function
Reply With Quote
Old 23-07-2009
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.Filter = Array("group")
For Each objGroup In colGroups
Wscript.Echo objGroup.Name
For Each objUser in objGroup.Members
Wscript.Echo vbTab & objUser.Name
Reply With Quote
Old 25-07-2009
Join Date: Mar 2009
Posts: 2
ThumbsUp Re: how to pull local group members in vbscript?


Net localgroup "Local Group Name"

This will extract the all the users in local group

Rajesh J S
Reply With Quote

  TechArena Community > Software > Software Development
Tags: ,

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads for: "how to pull local group members in vbscript?"
Thread Thread Starter Forum Replies Last Post
Listing members of Group with >1500 members Umesh Thakur Windows Server Help 11 03-02-2012 05:29 AM
Display members of a group with more than 1500 members Simon G Windows Server Help 5 25-10-2011 12:35 PM
Maybe a VBScript to pull email addresses? Modom Active Directory 1 03-08-2011 04:21 PM
how to pull NTFS security permission in vbscript vivekmohan Software Development 3 25-07-2009 01:28 PM
Looking for way to enumerate members of local administrators group Mark Windows Server Help 4 04-06-2008 05:44 PM

All times are GMT +5.5. The time now is 12:39 PM.