Find every Group & contact if there is a space between the alias names.

Hi,

Find every Group & contact if there is a space between the alias names.
I have exchange 2007 and i get errors when i open the properties of a Group 7 Contact  that has a space in the alias. Is there a way i could get just the user names to a txt file that has a space in the alias name.
All users needs to be checked within a OU.

Regards
sharath
'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 objOutputAll = objFSO.CreateTextFile("C:\Scripts\OutputAll.txt", True)
Set objOutputSpace = objFSO.CreateTextFile("C:\Scripts\OutputSpace.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

objCommand.CommandText = "SELECT samaccountname,mailnickname 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
		strUsername = objRecordSet.Fields("samaccountname").Value
		strAlias = objRecordSet.Fields("mailnickname").Value
		objOutputAll.WriteLine strUsername & "," & strAlias
		If InStr(strAlias, " ") > 0 Then
			objOutputSpace.WriteLine strUsername
		End If
		objRecordSet.MoveNext
	Loop
End If

objOutputAll.Close
objOutputSpace.Close

Open in new window

LVL 11
bsharathAsked:
Who is Participating?
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.

rlandquistCommented:
Here is a modifed version for groups.

Contacts version coming soon...
'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 objOutputAll = objFSO.CreateTextFile("C:\Scripts\OutputAll.csv", True)
Set objOutputSpace = objFSO.CreateTextFile("C:\Scripts\OutputSpace.txt", 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

objCommand.CommandText = "SELECT cn,mailnickname 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
		strAlias = objRecordSet.Fields("mailnickname").Value
		WScript.Echo strName
		objOutputAll.WriteLine strName & "," & strAlias
		If InStr(strAlias, " ") > 0 Then
			objOutputSpace.WriteLine strName
		End If
		objRecordSet.MoveNext
	Loop
End If

objOutputAll.Close
objOutputSpace.Close

Open in new window

0
rlandquistCommented:
Here is a better version.  It prompts for search type, Users, Groups, Contacts

Let me know what you think.
0
rlandquistCommented:
Forgot to attach!
'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 objOutputAll = objFSO.CreateTextFile("C:\Scripts\OutputAll.csv", True)
Set objOutputSpace = objFSO.CreateTextFile("C:\Scripts\OutputSpace.txt", 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

objOutputAll.Close
objOutputSpace.Close


Sub UserSub
	WScript.Echo "Users"
	objCommand.CommandText = "SELECT samaccountname,mailnickname 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
			strAlias = objRecordSet.Fields("mailnickname").Value
			WScript.Echo strName
			objOutputAll.WriteLine strName & "," & strAlias
			If InStr(strAlias, " ") > 0 Then
				objOutputSpace.WriteLine strName
			End If
			objRecordSet.MoveNext
		Loop
	End If
End Sub

Sub GroupSub
	WScript.Echo "Groups"
	objCommand.CommandText = "SELECT cn,mailnickname 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
			strAlias = objRecordSet.Fields("mailnickname").Value
			WScript.Echo strName
			objOutputAll.WriteLine strName & "," & strAlias
			If InStr(strAlias, " ") > 0 Then
				objOutputSpace.WriteLine strName
			End If
			objRecordSet.MoveNext
		Loop
	End If
			
End Sub

Sub ContactSub
	WScript.Echo "Contacts"
	objCommand.CommandText = "SELECT name,mailnickname 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
			strAlias = objRecordSet.Fields("mailnickname").Value
			WScript.Echo strName
			objOutputAll.WriteLine strName & "," & strAlias
			If InStr(strAlias, " ") > 0 Then
				objOutputSpace.WriteLine strName
			End If
			objRecordSet.MoveNext
		Loop
	End If
			
End Sub

Open in new window

0

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
bsharathAuthor Commented:
Thanks a lot was an Awesome thought
:-)
Please have a look at my posts and see if you can help on any... :-)
0
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
Programming Languages-Other

From novice to tech pro — start learning today.