I have one OU with about 40 child OUs. In each of these live mail-enabled contacts. I need a script that will recursively go through the OUs and generate the following in CSV format:
Name of Contact
Email Address
Title
Office
Group Membership (some contact may be members of up to 5 groups)
Can anyone help me here? The Group Membership part is the tricky one for me.
Thanks in advance!
PowershellVB ScriptActive Directory
Last Comment
RobSampson
8/22/2022 - Mon
RobSampson
Hi, try this. Change strOU to suit.
Regards,
Rob.
strOU = "OU=TestUsers,OU=TestOU,"strOutput = "Contacts.csv"Set objFSO = CreateObject("Scripting.FileSystemObject")Set objOutput = objFSO.CreateTextFile(strOutput, True)objOutput.WriteLine """Name"",""Email"",""Title"",""Office"",""Groups"""Const ADS_SCOPE_SUBTREE = 2Set objConnection = CreateObject("ADODB.Connection")Set objCommand = CreateObject("ADODB.Command")objConnection.Provider = "ADsDSOObject"objConnection.Open "Active Directory Provider"Set objCommand.ActiveConnection = objConnectionobjCommand.Properties("Page Size") = 1000objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREESet objRootDSE = GetObject("LDAP://RootDSE")strDomain = objRootDSE.Get("defaultNamingContext")If Trim(strOU) <> "" Then If Right(strOU, 1) <> "," Then strOU = strOU & ","End IfobjCommand.CommandText = "SELECT adsPath FROM 'LDAP://" & strOU & strDomain & "' WHERE objectCategory='user' AND objectClass='contact'"Set objRecordSet = objCommand.ExecuteobjRecordSet.MoveFirstWhile Not objRecordSet.EOF Set objUser = GetObject(objRecordSet.Fields("adsPath").Value) objOutput.Write """" & objUser.Name & """,""" & objUser.Mail & """,""" & objUser.Title & """,""" & objUser.physicalDeliveryOfficeName & """" If TypeName(objUser.MemberOf) = "Empty" Then objOutput.WriteLine ",""" ElseIf TypeName(objUser.MemberOf) = "String" Then objOutput.WriteLine ",""" & objUser.MemberOf & """" Else For Each strGroup In objUser.MemberOf objOutput.WriteLine ",""" & objUser.MemberOf & """" Next End If objRecordSet.MoveNextWendobjOutput.CloseMsgBox "Done. Please see " & strOutput
I recived the following error:
Missing '(' after 'if' in if statement.
chapter_info.ps1:17 char:4
+ If <<<< Trim(strOU) <> "" Then
+ CategoryInfo : ParserError: (OpenParenToken:TokenId) [], ParseException
+ FullyQualifiedErrorId : MissingEndParenthesisInIfStatement
RobSampson
Hi, sorry, this is not PowerShell. It's VBS. Save it as a VBS and run it.
Regarsd,
Rob.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
wdurrett
ASKER
Sorry, my bad. However, I got this new error:
c:>chapter_info.vbs(21, 1) Provider: Table does not exist.
RobSampson
Double check your OU path. It needs to be listed in reverse order, not including the DC parts. So, if you need to enumerate:
domain.com/Sites/MainOffice/Users
Then use
strOU = "OU=Users,OU=MainOffice,OU=Sites,"
Regards,
Rob.
wdurrett
ASKER
My bad agian. I was missing an "s" at the end of my OU.
But now I come back with yet another error:
c:\chapter_info.vbs(32, 7) Microsoft VBScript runtime error: Type mismatch
Unlimited question asking, solutions, articles and more.
RobSampson
Oh! This time it's my bad.....change this:
For Each strGroup In objUser.MemberOf
objOutput.WriteLine ",""" & objUser.MemberOf & """"
Next
to this
For Each strGroup In objUser.MemberOf
objOutput.WriteLine ",""" & strGroup & """"
Next
Regards,
Rob.
wdurrett
ASKER
OK, that time I did get some output. However, there are some issues.
1) Iam seeing CN= in front of all the names. I can deal with this if it has to be this way, but my hope is to pass this along to a non-technical end user for her to run. That will freak her out.
2) The group names show as "CN=Seattle Core Committee,OU=Seattle,OU=United States,OU=Chapters,OU=Internet Contacts,DC=mydoman,DC=org" Once again, I woudl like it just say "Seattle Core Committee."
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
If a contact is not a member of any group, can it return "None"? I am seeing some errors in the output followng a contact that has no group membership.
Thanks dude. you are awesome.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
RobSampson
Sure, just change
If TypeName(objUser.MemberOf) = "Empty" Then
objOutput.WriteLine ","""
to
If TypeName(objUser.MemberOf) = "Empty" Then
objOutput.WriteLine ",None"""
Regards,
Rob.
wdurrett
ASKER
Yes, I just did that before you posted it.
The issue is that for any contact not in a group, the next contact is on the same line. It is like I need to add a line return in the script for those few contacts.
Regards,
Rob.
Open in new window