Get the X500 details of all users in a txt file. In Colum B & C & D and So on...

hi,

Get the X500 details of all users/Contacts/Groups in a txt file. In Colum B & C & D and So on
I need to check if all have 2 or more X500 addreses.
I will have the users fullnames or if you want email addresses.
Colum A with the Group/User/Contact name
REgards
sharath
LVL 11
bsharathAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evetsleepCommented:
So you want to export from Active Directory all users\contacts\groups that have 2 or more X500 addresses?  What columns did you want exactly?
bsharathAuthor Commented:
Colum A the names and there after the X500 addresses
rlandquistCommented:
Give this a try.  It works like the other script I did for you.
'Define Constants
Const ADS_SCOPE_BASE = 0 'Search base object only
Const ADS_SCOPE_ONELEVEL = 1 'Search one level of immediate children
Const ADS_SCOPE_SUBTREE = 2 ' Search target object and all sub levels

'Create Objects
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objOutputFile = objFSO.CreateTextFile("C:\Scripts\Output.csv", True)

'Construct an ADsPath to the Current Domain with rootDSE
Set objRootDSE = GetObject("LDAP://rootDSE")
strADsPath = "LDAP://" & objRootDSE.Get("defaultNamingContext")

'Connect to Active Directory
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_ONELEVEL

'Prompt for path to starting OU
Do
	strOUPath = InputBox("Please enter the OU Path to search " & _
	" - Seperate OUs with a \", "OU Path Input", "TopOU\SubOU")
	If strOUPath = False Then
		WScript.Quit
	End If
Loop Until strOUPath <> ""

'Split OU path by OU
strOUPath = UCase(strOUPath)
strOUPath = Split(strOUPath, "\")

'Prepare variables for search
intOULevel = 0
strSearchADsPath = strADsPath

'Search through each OU level in path provided
For intOULevel = 0 To UBound(strOUPath)
	objCommand.CommandText = "SELECT ADsPath FROM '" & strSearchADsPath & _
	"'" & " WHERE objectCategory='organizationalUnit' AND Name = '" & _
	strOUPath(intOULevel) & "'"
	Set objRecordSet = objCommand.Execute
					
	'Verify OU was found
	If objRecordSet.EOF Then
		WScript.echo "OU named " & strOUPath(intOULevel) & _
		" not found, Exiting script."
		WScript.quit
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			strSearchADsPath = objRecordSet.Fields("ADsPath").Value
			objRecordSet.MoveNext
		Loop
	End If
Next

'Search AD
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Do
	strSearchType = InputBox("Please enter the search type " & _
	" - Users or Groups or Contacts", "Input", "")
	If strSearchType = False Then
		WScript.Quit
	End If
Loop Until strSearchType <> ""
strSearchType = UCase(Trim(strSearchType))

Select Case strSearchType
	Case "USERS"
		UserSub						
	Case "GROUPS"
		GroupSub		
	Case "CONTACTS"
		ContactSub						
End Select

objOutputFile.Close


Sub UserSub
	WScript.Echo "Users"
	objCommand.CommandText = "SELECT samaccountname,proxyAddresses FROM '" & strSearchADsPath & _
	"'" & " WHERE objectCategory='person' AND objectClass='user'"
	Set objRecordSet = objCommand.Execute
	If objRecordSet.EOF Then
		WScript.echo "No users found, Exiting script."
		WScript.quit
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			strName = objRecordSet.Fields("samaccountname").Value
			strproxyAddresses = objRecordSet.Fields("proxyAddresses").Value
			WScript.Echo strName
			If IsArray(strproxyAddresses) Then
				strOutput = strName
				For Each strproxyaddress In strproxyAddresses
					If Left(strproxyaddress, 4) = "X500" Then
						strOutput = strOutput & "," & strproxyaddress
					End If
				Next
				objOutputFile.WriteLine strOutput
			End If
			strOutput = ""
			objRecordSet.MoveNext
		Loop
	End If
End Sub

Sub GroupSub
	WScript.Echo "Groups"
	objCommand.CommandText = "SELECT cn,proxyAddresses FROM '" & strSearchADsPath & _
	"'" & " WHERE objectCategory='group'"
	Set objRecordSet = objCommand.Execute
	If objRecordSet.EOF Then
		WScript.echo "No users found, Exiting script."
		WScript.quit
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			strName = objRecordSet.Fields("cn").Value
			strproxyAddresses = objRecordSet.Fields("proxyAddresses").Value
			WScript.Echo strName
			If IsArray(strproxyAddresses) Then
				strOutput = strName
				For Each strproxyaddress In strproxyAddresses
					If Left(strproxyaddress, 4) = "X500" Then
						strOutput = strOutput & "," & strproxyaddress
					End If
				Next
				objOutputFile.WriteLine strOutput
			End If
			strOutput = ""
			objRecordSet.MoveNext
		Loop
	End If
				
End Sub

Sub ContactSub
	WScript.Echo "Contacts"
	objCommand.CommandText = "SELECT name,proxyAddresses FROM '" & strSearchADsPath & _
	"'" & " WHERE objectCategory='person' AND objectClass='contact'"
	Set objRecordSet = objCommand.Execute
								
	If objRecordSet.EOF Then
		WScript.echo "No users found, Exiting script."
		WScript.quit
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			strName = objRecordSet.Fields("name").Value
			strproxyAddresses = objRecordSet.Fields("proxyAddresses").Value
			WScript.Echo strName
			If IsArray(strproxyAddresses) Then
				strOutput = strName
				For Each strproxyaddress In strproxyAddresses
					If Left(strproxyaddress, 4) = "X500" Then
						strOutput = strOutput & "," & strproxyaddress
					End If
				Next
				objOutputFile.WriteLine strOutput
			End If
			strOutput = ""
			objRecordSet.MoveNext
		Loop
	End If
				
End Sub

Open in new window

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

bsharathAuthor Commented:
Thanks Nice one
But i get 1 X500 retrieved and not all.
I will have more than 1 X500 address and want all
rlandquistCommented:
Try this version
'Define Constants
Const ADS_SCOPE_BASE = 0 'Search base object only
Const ADS_SCOPE_ONELEVEL = 1 'Search one level of immediate children
Const ADS_SCOPE_SUBTREE = 2 ' Search target object and all sub levels

'Create Objects
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objOutputFile = objFSO.CreateTextFile("C:\Scripts\Output.csv", True)

'Construct an ADsPath to the Current Domain with rootDSE
Set objRootDSE = GetObject("LDAP://rootDSE")
strADsPath = "LDAP://" & objRootDSE.Get("defaultNamingContext")

'Connect to Active Directory
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_ONELEVEL

'Prompt for path to starting OU
Do
	strOUPath = InputBox("Please enter the OU Path to search " & _
	" - Seperate OUs with a \", "OU Path Input", "TopOU\SubOU")
	If strOUPath = False Then
		WScript.Quit
	End If
Loop Until strOUPath <> ""

'Split OU path by OU
strOUPath = UCase(strOUPath)
strOUPath = Split(strOUPath, "\")

'Prepare variables for search
intOULevel = 0
strSearchADsPath = strADsPath

'Search through each OU level in path provided
For intOULevel = 0 To UBound(strOUPath)
	objCommand.CommandText = "SELECT ADsPath FROM '" & strSearchADsPath & _
	"'" & " WHERE objectCategory='organizationalUnit' AND Name = '" & _
	strOUPath(intOULevel) & "'"
	Set objRecordSet = objCommand.Execute
						
	'Verify OU was found
	If objRecordSet.EOF Then
		WScript.echo "OU named " & strOUPath(intOULevel) & _
		" not found, Exiting script."
		WScript.quit
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			strSearchADsPath = objRecordSet.Fields("ADsPath").Value
			objRecordSet.MoveNext
		Loop
	End If
Next

'Search AD
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Do
	strSearchType = InputBox("Please enter the search type " & _
	" - Users or Groups or Contacts", "Input", "")
	If strSearchType = False Then
		WScript.Quit
	End If
Loop Until strSearchType <> ""
strSearchType = UCase(Trim(strSearchType))

Select Case strSearchType
	Case "USERS"
		UserSub						
	Case "GROUPS"
		GroupSub		
	Case "CONTACTS"
		ContactSub						
End Select

objOutputFile.Close


Sub UserSub
	WScript.Echo "Users"
	objCommand.CommandText = "SELECT samaccountname,proxyAddresses FROM '" & strSearchADsPath & _
	"'" & " WHERE objectCategory='person' AND objectClass='user'"
	Set objRecordSet = objCommand.Execute
	If objRecordSet.EOF Then
		WScript.echo "No users found, Exiting script."
		WScript.quit
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			strName = objRecordSet.Fields("samaccountname").Value
			strproxyAddresses = objRecordSet.Fields("proxyAddresses").Value
			WScript.Echo strName
			If IsArray(strproxyAddresses) Then
				strOutput = strName
				For Each strproxyaddress In strproxyAddresses
					If UCase(Left(strproxyaddress, 4)) = "X500" Then
						strOutput = strOutput & "," & strproxyaddress
					End If
				Next
				objOutputFile.WriteLine strOutput
			End If
			strOutput = ""
			objRecordSet.MoveNext
		Loop
	End If
End Sub

Sub GroupSub
	WScript.Echo "Groups"
	objCommand.CommandText = "SELECT cn,proxyAddresses FROM '" & strSearchADsPath & _
	"'" & " WHERE objectCategory='group'"
	Set objRecordSet = objCommand.Execute
	If objRecordSet.EOF Then
		WScript.echo "No users found, Exiting script."
		WScript.quit
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			strName = objRecordSet.Fields("cn").Value
			strproxyAddresses = objRecordSet.Fields("proxyAddresses").Value
			WScript.Echo strName
			If IsArray(strproxyAddresses) Then
				strOutput = strName
				For Each strproxyaddress In strproxyAddresses
					If UCase(Left(strproxyaddress, 4)) = "X500" Then
						strOutput = strOutput & "," & strproxyaddress
					End If
				Next
				objOutputFile.WriteLine strOutput
			End If
			strOutput = ""
			objRecordSet.MoveNext
		Loop
	End If
					
End Sub

Sub ContactSub
	WScript.Echo "Contacts"
	objCommand.CommandText = "SELECT name,proxyAddresses FROM '" & strSearchADsPath & _
	"'" & " WHERE objectCategory='person' AND objectClass='contact'"
	Set objRecordSet = objCommand.Execute
									
	If objRecordSet.EOF Then
		WScript.echo "No users found, Exiting script."
		WScript.quit
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			strName = objRecordSet.Fields("name").Value
			strproxyAddresses = objRecordSet.Fields("proxyAddresses").Value
			WScript.Echo strName
			If IsArray(strproxyAddresses) Then
				strOutput = strName
				For Each strproxyaddress In strproxyAddresses
					If UCase(Left(strproxyaddress, 4)) = "X500" Then
						strOutput = strOutput & "," & strproxyaddress
					End If
				Next
				objOutputFile.WriteLine strOutput
			End If
			strOutput = ""
			objRecordSet.MoveNext
		Loop
	End If
					
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.