How to search users with multiple mailboxes in different domains?
Hi, everyone! I have a problem and i need your help. I have a forestof 1 top domain (xxx.com) )and 3 sub domains in my organization. Each sub domain represents a country (uk.xxx.com, it.xxx.com, dk.xxx.com) and has its own Exchange installation. We have a lot of users traveling from country to country so they have a mailbox in each of these 3 domains (john.doe@uk.xxx.com, john.doe@it.xxx.com, john.doe@dk.xxx.com). Message routing is enabled between all3 domains.I need to find all users such as john doe in my example cause i need to delete all his mailboxes except the mailbox in the country where he actually works and lives. I'd like to write a script but i'm new to scripting and programming so i'd really appreciate any help. I've wrote something but i can't figure out how to search for duplicate names in my array. Or maybe it can be done another way? Any help will be much appreciated! Thanks a lot in advance! Set colNamedArguments = WScript.Arguments.NamedstrOutputFile = colNamedArguments.Item("f")strNoConOutput = colNamedArguments.Item("s")strHelp = colNamedArguments.Item("help")dim allUsers()redim AllUsers(50000,3)Set objRootDSE = GetObject("LDAP://rootDSE")strDomain = objRootDSE.Get("defaultNamingContext")strADPath = "LDAP://" & strDomainSet objDomain = GetObject(strADPath)strOutput = strOutput & VbCrLf & "Domain: " & objDomain.distinguishedNameSet objConnection = CreateObject("ADODB.Connection")objConnection.Open "Provider=ADsDSOObject;"Set objCommand = CreateObject("ADODB.Command")objCommand.ActiveConnection = objConnectionobjCommand.Properties("Page Size") = 20000 objCommand.CommandText = _ "<" & strADPath & ">" & ";(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))) ))))" & ";distinguishedName,sn,givenName" & _ ";subtree" Set objRecordSet = objCommand.Execute strOutput = strOutput & VbCrLf & VbCrLf & "====================================" If objRecordSet.RecordCount = 0 Then strOutput = strOutput & VbCrLf & "" WScript.Echo strOutput WriteFile WScript.Quit Else While Not objRecordSet.EOFi = i + 1 varSN = objRecordSet.Fields(1).ValuestrOutput = strOutput & VbCrLf & varSN & " "Allusers(i,0) = varSNvargivenName = objRecordSet.Fields(2).ValuestrOutput = strOutput & vargivenNameAllusers(i,1) = vargivenName objRecordSet.MoveNext Wend WriteCon WriteFile End If Sub WriteFile If IsEmpty(strOutputFile) Then WScript.Echo "No file output" Else Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile(strOutputFile) objFile.WriteLine strOutput objFile.Close End If End Sub Sub WriteCon If Not colNamedArguments.Exists("s") Then WScript.echo strOutput Else WScript.Echo "No console output" End If End Sub
October 24th, 2007 3:15pm

You should change your Array to a Dictionary object Then you can add values and check later if the value is allready in the list Dictionary.Add "A" If Dictionary.Exists ("A") Then ... Dictionary Object http://msdn2.microsoft.com/en-us/library/x4k5wbx4.aspx Deli
Free Windows Admin Tool Kit Click here and download it now
October 24th, 2007 8:47pm

I've tried this one already but as i've said i'm not a programmer and i can't figure out the logic of this method. LDAP queries and simple scripts are easy to understand but something more complex can't fit my brain )))) But thanks a lot for the answer! Your advise is much appreciated!
October 25th, 2007 10:03am

Using the dictionary object is really simple I find it much more easier than using an array Just a sample on how you can use the dictionary object Dim d ' Create a variable.Set d = CreateObject("Scripting.Dictionary")d.Add "a", "Athens" ' Add some keys and items.d.Add "b", "Belgrade"d.Add "c", "Cairo" If d.exists("a") Then wscript.echo "Exists" Else wscript.echo "Does not Exist" End If d.Remove("a") If d.exists("a") Then wscript.echo "Exists" Else wscript.echo "Does not Exist" End If Deli
Free Windows Admin Tool Kit Click here and download it now
October 25th, 2007 12:26pm

Hi, Deli! Look what i've done after reading some additional stuff ))) But i have some questions... Option Explicit Const INPUT_FILE_1 = "C:\input1.csv"Const INPUT_FILE_2 = "C:\input2.csv"Const OUTPUT_FILE = "C:\results.txt" Const ForReading = 1Const ForWriting = 2 Dim dicData1, dicData2, strKey, fso, tsDim filInput1, filInput2, filResults, strLine Set fso = CreateObject("Scripting.FileSystemObject") Set filInput1 = _ fso.OpenTextFile(INPUT_FILE_1, ForReading)Set filInput2 = _ fso.OpenTextFile(INPUT_FILE_2, ForReading)Set filResults = _ fso.OpenTextFile(OUTPUT_FILE, ForWriting, True) Set dicData1 = CreateObject("Scripting.Dictionary")While Not filInput1.AtEndOfStreamstrLine = filInput1.ReadLinedicData1.Add Split(strLine,",")(0), Split(strLine,",")(1)WendfilInput1.Close Set dicData2 = CreateObject("Scripting.Dictionary")While Not filInput2.AtEndOfStreamstrLine = filInput2.ReadLinedicData2.Add Split(strLine,",")(0), Split(strLine,",")(1)WendfilInput2.Close For Each strKey In dicData1If Not dicData2.Exists(strKey) ThenfilResults.WriteLine strKey & ": 1"ElseIf dicData2.Item(strKey) <> dicData1.Item(strKey) ThenfilResults.WriteLine strkey & _ ": 666"End IfEnd IfNextFor Each strKey In dicData2If Not dicData1.Exists(strKey) ThenfilResults.WriteLine strKey & ": 2"End ifNextfilResults.WriteLine "Lines counted in file1 " & _"(" & dicData1.Count & ") and file2 (" & dicData2.Count & ")."filResults.Close .CSV structure username1,987654 username2,987655 ... username2500,..... So, how can i set Dictionary size? I have 2 .csv lists - 2500 (domain1) and 500 (domain2) users. By direct compare i get an error message but when i reduce the number of line to 100 in each .csv everything works just fine. I can't understand if these .csv's should have the same number of lines (i've tried to add additional lines in the second .csv so they both have 2500 but it was useless) or do i need to set my Dictionary size somehow?
October 29th, 2007 11:01am

I don't think Dictionary has a size limit Can you post the excact error that you receive and also indicate which line? Maybe there is a string that is not correctly listed in csv file Deli
Free Windows Admin Tool Kit Click here and download it now
October 29th, 2007 1:20pm

The error message is: "Microsoft VBScript runtime error: This key is already associated with an element of this collection"on line "dicData1.Add Split(strLine,",")(0), Split(strLine,",")(1)" In this script i compare 2 .csv's to find duplicate usernames. I have 4 domains (4 .csv for each domain taken fromDCs)in a forest so it means i should make a lot of compares - 1-2, 1-3, 1-4, 2-3... I can also take the same info about user accounts from the Global Catalogin my forest and then i have 1 list with all user accounts. Is there any chance that i can use Dictionary object to search for duplicates in this case?
October 29th, 2007 1:36pm

Apparently you have duplicates in the CSV file that you want to import into your Dictionary Convert the following part While Not filInput1.AtEndOfStreamstrLine = filInput1.ReadLinedicData1.Add Split(strLine,",")(0), Split(strLine,",")(1)Wend Into this While Not filInput1.AtEndOfStreamstrLine = filInput1.ReadLine if dicData1.Exists (Split(strLine,",")(0)) Then wscript.echo Split(strLine,",")(0) & " Allready added to dictionary" Else dicData1.Add Split(strLine,",")(0), Split(strLine,",")(1) EndWend You might need to do that for all of your Dictionaries Deli
Free Windows Admin Tool Kit Click here and download it now
October 29th, 2007 2:05pm

It works! Awesome! Don't know how to thank you, Deli! Your help is much appreciated! Huge thanks!
October 29th, 2007 3:00pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics