Is there a script which will report which OU a server resides in, given an Active Directory 2003 dom

Discussion in 'Scripting' started by Spin, Oct 7, 2008.

  1. Spin

    Spin Guest


    Is there a script which will report which OU a server resides in, given an
    Active Directory 2003 domain?
    Spin, Oct 7, 2008
    1. Advertisements

  2. Spin

    Jorge Silva Guest

    Doing a simple find using ADUC will show you where the server is.
    I hope that the information above helps you.
    Have a Nice day.

    Jorge Silva
    MCSE, MVP Directory Services

    Please no e-mails, any questions should be posted in the NewsGroup
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Jorge Silva, Oct 7, 2008
    1. Advertisements

  3. Spin

    Spin Guest

    I need a report dumping the OU membership of all servers, in a spreadsheet
    sorted by server name alphabetically.
    Spin, Oct 7, 2008
  4. Spin

    Adrian Guest

    You can use [ DSQUERY Computer -limit 0 ] put that into excel and modify that
    data to suit your needs.
    Adrian, Oct 7, 2008
  5. I find ADFind.exe to be more efficient and produce better output.

    If you use the following, you will get a list of the DN of each computer
    object outputted to CSV format:
    adfind -b dc=domain,dc=com -f "objectcategory=computer" -dn -csv
    -nocsvheader >Results.txt

    If you are looking for computers in a specific OU, called Servers for
    example, then the following will work too:
    adfind -b dc=domain,dc=com -f "objectcategory=computer" -incldn Servers -dn

    John Policelli


    This posting is provided "AS IS" with no warranties and confers no rights!
    Always test before proceeding.

    John Policelli [MVP - DS], Oct 7, 2008
  6. Spin

    Al Dunbar Guest

    type this command:

    dsquery user -samid username

    Al Dunbar, Oct 8, 2008
  7. I have an example VBScript program to document all servers in the domain
    linked here: Servers.htm

    The program outputs the Distinguished Name of each machine, which indicates
    the OU/container the computer resides in. If you run the script at a command
    prompt with the cscript host, the output can be redirected to a text file.
    Richard Mueller [MVP], Oct 8, 2008
  8. More code is required to sort the server names and write the output to an
    Excel spreadsheet. You can use a disconnected recordset to sort the names.
    The following VBScript program worked in my domain (Excel must be installed
    on the client where this runs):
    Option Explicit

    Dim objRootDSE, strDNSDomain, adoConnection, adoCommand, strQuery
    Dim adoRecordset, strComputerDN, strOS
    Dim adoResults, strOU, strName, intIndex
    Dim objExcel, objSheet, intRow, strExcelPath

    Const adVarChar = 200
    Const MaxCharacters = 255

    ' Specify spreadsheet.
    strExcelPath = "c:\Scripts\Servers.xls"

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

    ' Use ADO to search Active Directory for all computers.
    Set adoCommand = CreateObject("ADODB.Command")
    Set adoConnection = CreateObject("ADODB.Connection")
    adoConnection.Provider = "ADsDSOObject"
    adoConnection.Open "Active Directory Provider"
    adoCommand.ActiveConnection = adoConnection

    strQuery = "<LDAP://" & strDNSDomain _
    & ">;(objectCategory=computer);" _
    & "sAMAccountName,distinguishedName,operatingSystem;subtree"

    adoCommand.CommandText = strQuery
    adoCommand.Properties("Page Size") = 100
    adoCommand.Properties("Timeout") = 30
    adoCommand.Properties("Cache Results") = False

    Set adoRecordset = adoCommand.Execute

    Set adoResults = CreateObject("ADODB.Recordset")
    adoResults.Fields.Append "Name", adVarChar, MaxCharacters
    adoResults.Fields.Append "OU", adVarChar, MaxCharacters

    ' Enumerate computer objects with server operating systems.
    Do Until adoRecordset.EOF
    strOS = adoRecordset.Fields("operatingSystem").Value
    If (InStr(UCase(strOS), "SERVER") > 0) Then
    strComputerDN = adoRecordset.Fields("distinguishedName").Value
    strName = adoRecordset.Fields("sAMAccountName").Value
    ' Remove trailing "$".
    strName = Left(strName, Len(strName) - 1)
    intIndex = InStr(LCase(strComputerDN), ",ou=")
    If (intIndex = 0) Then
    intIndex = InStr(LCase(strComputerDN), ",cn=")
    End If
    If (intIndex = 0) Then
    intIndex = InStr(strComputerDN, ",")
    End If
    strOU = Mid(strComputerDN, intIndex + 1)
    strOU = Left(strOU, InStr(LCase(strOU), ",dc=") - 1)
    adoResults.Fields("Name").Value = strName
    adoResults.Fields("OU").Value = strOU
    End If

    adoResults.Sort = "Name"

    Set objExcel = CreateObject("Excel.Application")
    Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

    intRow = 2
    objSheet.Cells(1, 1).Value = "Server Name"
    objSheet.Cells(1, 2).Value = "OU/Container"
    Do Until adoResults.EOF
    objSheet.Cells(intRow, 1).Value = adoResults.Fields("Name").Value
    objSheet.Cells(intRow, 2).Value = adoResults.Fields("OU").Value
    intRow = intRow + 1

    objExcel.ActiveWorkbook.SaveAs strExcelPath

    ' Clean up.

    Wscript.Echo "Done"
    Richard Mueller [MVP], Oct 8, 2008
  9. Spin

    Al Dunbar Guest

    aargh, my apologies - I responded before I noticed you were looking for

    Al Dunbar, Oct 8, 2008
  10. On the dc from a command prompt:

    dsquery computer -name "computername"

    Replace "Computername" as appropriate. This should give you the DN of the
    James Yeomans BSc, MCSE, Oct 8, 2008
    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.