• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

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
0
bsharath
Asked:
bsharath
  • 3
  • 2
1 Solution
 
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?
0
 
bsharathAuthor Commented:
Colum A the names and there after the X500 addresses
0
 
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

0
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
0
 
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now