Want to check if multiple workstations exists in AD from a TXT fil

Discussion in 'Scripting' started by Zee, Apr 11, 2008.

  1. Zee

    Zee Guest

    Hi,
    We are igrating our company's 2500 user to One DC from 3 DC. I have a list
    of all the workstations with Hostnames. I want to check if these machines are
    migrated. I have an excel file with workstations names. What i want to do is
    Copy those workstation names into a TXT file and check if all of them exists
    in the New DC or not? Presently i have to check every individual one by
    searching in AD. Is there any script which i can use to search for all those
    machine names from a txt file? I have read only access to AD.
    Cheers
     
    Zee, Apr 11, 2008
    #1
    1. Advertisements

  2. A VBScript program can read computer names from either an Excel spreadsheet
    or a text file. There are several ways to check if the corresponding
    computer object exists in AD, but I think the fastest would be to use the
    NameTranslate object to convert the hostname (the NetBIOS name) to the
    Distinguished Name. You could also use ADO to search for the objects, but I
    find NameTranslate faster.

    Of course, another option is to dump out a list of all computers in the
    domain and compare.

    A program to read computer names from the first column of a spreadsheet
    (starting at row 1 and continuing until the first blank entry is found) and
    checking each could be:
    =========
    ' Constants for NameTranslate.
    Const ADS_NAME_INITTYPE_GC = 3
    Const ADS_NAME_TYPE_NT4 = 3
    Const ADS_NAME_TYPE_1779 = 1

    ' Specify spreadsheet file.
    strExcelPath = "c:\scripts\computers.xls"

    ' Bind to Excel object.
    Set objExcel = CreateObject("Excel.Application")

    ' Open spreadsheet.
    objExcel.Workbooks.Open strExcelPath

    ' Bind to worksheet.
    Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

    ' Determine DNS domain name from RootDSE object.
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootDSE.Get("defaultNamingContext")

    ' Use the NameTranslate object to find the NetBIOS domain name
    ' from the DNS domain name.
    Set objTrans = CreateObject("NameTranslate")
    objTrans.Init ADS_NAME_INITTYPE_GC, ""
    objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
    strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
    ' Remove trailing backslash.
    strNetBIOSDomain = Left(strNetBIOSDomain, _
    Len(strNetBIOSDomain) - 1)

    ' Read the spreadsheet, starting at row 1, until the
    ' first blank cell is found in column 1.
    intRow = 1
    Do While objSheet.Cells(intRow, 1).Value <> ""
    strNTName = Trim(objSheet.Cells(intRow, 1).Value)
    ' Use NameTranslate to convert NT name to Distinguished Name.
    On Error Resume Next
    objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName
    If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Computer " & strNTName _
    & " not found in Active Directory"
    Else
    On Error GoTo 0
    strDN = objTrans.Get(ADS_NAME_TYPE_1779)
    Wscript.Echo "Computer " strNTName & " is " & strDN
    End If
    intRow = intRow + 1
    Loop

    ' Close the workbook.
    objExcel.ActiveWorkbook.Close

    ' Quit Excel.
    objExcel.Application.Quit
    ==========
    Here is a link to an example program that dumps out the Distinguished Names
    (DN's) of all users in the domain to a spreadsheet:

    http://www.rlmueller.net/Create User List 3.htm

    This program can be modified to dump the DN's of computers instead by
    changing the ADO filter from this:

    strFilter = "(&(objectCategory=person)(objectClass=user))"

    to this:

    strFilter = "(objectCategory=computer)"
     
    Richard Mueller [MVP], Apr 11, 2008
    #2
    1. Advertisements

  3. Zee

    Zee Guest

    Hi Richard,
    Thank you for your reply. I am getting errors as i try to run this script. I
    am totally new to this with only Read Only AD Rights. I have error coming on
    line 47. Do i have to change anything in this Script or just copy and paste
    it and save it as .vbs file? Please let me know in layman terms if possible.
    Cheers
     
    Zee, Apr 11, 2008
    #3
  4. My mistake. I see an error on line 47. The line:

    Wscript.Echo "Computer " strNTName & " is " & strDN

    should be:

    Wscript.Echo "Computer " & strNTName & " is " & strDN

    I am using the concatenation symbol, "&" to concatenate 4 strings. I missed
    the "&" between the string "Computer " and the string variable strNTName.
    --
    Richard Mueller
    Microsoft MVP Scripting and ADSI
    Hilltop Lab - http://www.rlmueller.net
    --

     
    Richard Mueller [MVP], Apr 11, 2008
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.