Announcement

Collapse
No announcement yet.

need vbscript to promote fully qualified name of server

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • need vbscript to promote fully qualified name of server

    hi everyone,

    i have a text file which will have list of servers, i need to Query AD to get their container name (fully qulified name) of server is it possible?

    For example in text file i will have server name as USABHSMNONAM04 after querying AD it should give me

    “CN=USABHSM0NAM04,OU=ABH,OU=Servers,OU=Technology, OU=GMOL,DC=nam,DC=corp,DC=gm,DC=com”.

  • #2
    Re: need vbscript to promote fully qualified name of server

    Originally posted by vivekmohan View Post
    i need to Query AD to get their container name (fully qulified name) of server
    Use dsquery and dsget.

    -vP

    Comment


    • #3
      Re: need vbscript to promote fully qualified name of server

      What you meant by "promote fully qualified name" itis not not clear to me where you want to output the results to?

      The script below outputs a popup for every computer from you list, it is assuming that in your list the computer names are on separate lines.
      Code:
      ' Get names from all computer objects in AD domain
      Dim ComputerAccounts: Set ComputerAccounts = _
         CreateObject("Scripting.Dictionary")
      ComputerAccounts.CompareMode = vbTextCompare : GetComputerAccounts
      
      
      ' Loop through your list with computer names.
      inputFile = "c:\test.txt"
      Const ForReading =  1 
      Set fso = CreateObject("Scripting.FileSystemObject") 
      
      On Error Resume Next
      Set ts = fso.OpenTextFile(inputFile, ForReading)
      Do until ts.AtEndOfStream
         s = trim(ts.ReadLine)
         If len(s) > 0 Then
          strComputer = trim(split(s, " ")(0))
          strComputer = lcase(strComputer)
          If (ComputerAccounts.Exists(strComputer) = True) Then
           wsh.echo s, "," & _
                 vbNewLine & ComputerAccounts.Item(strComputer)
          End If
         End If
      Loop
      ts.Close: Set ts = Nothing: Set fso = Nothing
      
      wscript.quit
      
      
      Sub GetComputerAccounts
         Const ADS_SCOPE_SUBTREE = 2
      
         Set objRootDSE = GetObject("LDAP://RootDSE")
         strRootDSE = objRootDSE.Get("DefaultNamingContext")
      
         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_SUBTREE 
      
         strCommand = "SELECT sAMAccountName, distinguishedName " _
            & "FROM 'LDAP://" & strRootDSE _
            & "' WHERE ObjectCategory='computer'"  
         objCommand.CommandText = strCommand
      
         Set objRecordSet = objCommand.Execute
      
         objRecordSet.MoveFirst
         Do Until objRecordSet.EOF
           accName = objRecordSet.Fields("sAMAccountname").Value
           accName = LEFT(accName,LEN(accName)-1)
           strDN = objRecordSet.Fields("distinguishedName").Value
           ComputerAccounts.Add LCase(accName), strDN
           objRecordSet.MoveNext
           Loop
      
         objConnection.Close
      End Sub
      \Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: need vbscript to promote fully qualified name of server

        hi rems,

        i am glad with your replay "promote fully name means", i will have list of server name for example USABHSMNONAM04(server name), in txt file what i need is i need to query AD with this server name to get its container name (fully qulified name of the server)
        “CN=USABHSM0NAM04,OU=ABH,OU=Servers,OU=Technology, OU=GMOL,DC=nam,DC=corp,DC=gm,DC=com”. (the think i need to get from AD)

        Note: i will have only the server name

        Comment


        • #5
          Re: need vbscript to promote fully qualified name of server

          Originally posted by vivekmohan View Post
          hi rems,

          i am glad with your replay "promote fully name means", i will have list of server name for example USABHSMNONAM04(server name), in txt file what i need is i need to query AD with this server name to get its container name (fully qulified name of the server)
          “CN=USABHSM0NAM04,OU=ABH,OU=Servers,OU=Technology, OU=GMOL,DC=nam,DC=corp,DC=gm,DC=com”. (the think i need to get from AD)

          Note: i will have only the server name
          Yes I understand what you are looking for, but in what way (or what format) do want to view the results in. The sample vbscript will pop-up the distinguished name of each server from the list, have you looked at the sample script, does this help you? It would also be possible to add the results (the distinguished name of each server) to a txt (?) or csv (?) file.


          \Rems

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: need vbscript to promote fully qualified name of server

            yes its very helpful but it throws me error as

            Microsoft VBScript runtime error (54, 6) : This key is already associated with an element of this collection

            i think the error maybe on this line,
            ComputerAccounts.Add LCase(accName), strDN

            i try to rectifiy the error but i couldn't
            could pls the resolve the error
            Last edited by vivekmohan; 30th June 2009, 11:47.

            Comment


            • #7
              Re: need vbscript to promote fully qualified name of server

              Originally posted by vivekmohan View Post
              Microsoft VBScript runtime error (54, 6) : This key is already associated with an element of this collection

              i think the error maybe on this line,
              ComputerAccounts.Add LCase(accName), strDN
              That is strange,
              the error could indicate that computers were found with double names (however within one domain the accounnames suppost to be unique) Or, the error can also mean that the search was done twice (?)

              I have created a new "Sub GetComputerAccounts" subroutine, to replace the original subroutine part from the sample in my previous reply.

              Now when a double record was found, there will be a popup with details.
              Can you run the modified script and tell whether the computernames that are double also show the same OU and domain or not.

              Code:
              Sub GetComputerAccounts
                 Const ADS_SCOPE_SUBTREE = 2
              
                 ComputerAccounts.RemoveAll
              
                 Set objRootDSE = GetObject("LDAP://RootDSE")
                 strRootDSE = objRootDSE.Get("DefaultNamingContext")
              
                 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_SUBTREE 
              
                 strCommand = "SELECT sAMAccountName, distinguishedName " _
                    & "FROM 'LDAP://" & strRootDSE _
                    & "' WHERE ObjectCategory='computer'"  
                 objCommand.CommandText = strCommand
              
                 Set objRecordSet = objCommand.Execute
              
                 objRecordSet.MoveFirst
                 Do Until objRecordSet.EOF
                   accName = objRecordSet.Fields("sAMAccountname").Value
                   accName = LEFT(accName,LEN(accName)-1)
                   strDN = objRecordSet.Fields("distinguishedName").Value
                   If (ComputerAccounts.Exists(accName) = False) Then
                     ComputerAccounts.Add LCase(accName), strDN
                   Else
                     msgbox "ERROR - Computer with a non-unique name found!!" _
                      & vbNewLine & accName & " - " & strDN & vbNewLine _
                      & "Double with " & ComputerAccounts.Item(accName)
                   End If
              
                   objRecordSet.MoveNext
                Loop
              
                 objConnection.Close
              End Sub

              \Rems

              This posting is provided "AS IS" with no warranties, and confers no rights.

              __________________

              ** Remember to give credit where credit's due **
              and leave Reputation Points for meaningful posts

              Comment


              • #8
                Re: need vbscript to promote fully qualified name of server

                yes there is some dulipcate computer name (ie) the computer are doubled, and there are in same OU and there in same domain..

                how do i rectifiy this reply me,

                is this script is fetching data from current work station?because i am running this script in windowsXP iam not running on window server installed machine.

                if i add this code to my project without any modification this will work ?

                can i apply this script anywhere?

                Comment


                • #9
                  Re: need vbscript to promote fully qualified name of server

                  Originally posted by vivekmohan View Post
                  yes there is some dulipcate computer name (ie) the computer are doubled, and there are in same OU and there in same domain..
                  If you have duplicate computer names, you have a lot more issues that a script to identify them!
                  Tom Jones
                  MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
                  PhD, MSc, FIAP, MIITT
                  IT Trainer / Consultant
                  Ossian Ltd
                  Scotland

                  ** Remember to give credit where credit is due and leave reputation points where appropriate **

                  Comment


                  • #10
                    Re: need vbscript to promote fully qualified name of server

                    Originally posted by vivekmohan View Post
                    yes there is some dulipcate computer name (ie) the computer are doubled, and there are in same OU and there in same domain..
                    That can't be true. You can't have two objects with the same RDN in a container.
                    If an object is created on one domain controller and an object with the same name is created in the same container on another domain controller before replication occurs, it creates an object name conflict. Active Directory automatically changes the relative distinguished name of the object with the earlier timestamp to a unique name.
                    http://technet.microsoft.com/en-us/l.../bb727059.aspx
                    Caesar's cipher - 3

                    ZKHQ BRX HYHQWXDOOB GHFLSKHU WKLV BRX ZLOO UHDOLVH LW ZDV D ZDVWH RI WLPH!

                    SFX JNRS FC U6 MNGR

                    Comment


                    • #11
                      Re: need vbscript to promote fully qualified name of server

                      Originally posted by vivekmohan View Post
                      yes there is some dulipcate computer name (ie) the computer are doubled, and there are in same OU and there in same domain..

                      how do i rectifiy this reply me,

                      is this script is fetching data from current work station?because i am running this script in windowsXP iam not running on window server installed machine.

                      if i add this code to my project without any modification this will work ?

                      can i apply this script anywhere?
                      As you can see by using ADU&C there are no duplicate computer names in the Active Directory, so something must go wrong in the script when running it.
                      Have you perhaps made any changes to the script???

                      Since the OU and domain of the computers recorded in the script in the records set are exactly the same it would be no problem to skip the duplicate records from the vbDictionary for your case. Therefore you can try the script below:

                      Yes, you can run the script from a client (Windows XPP) as long the access to the AD has not been blocked in someway.

                      Code:
                      ' Get names from all computer objects in AD domain
                      Dim objComputerAccs: Set objComputerAccs = _
                         CreateObject("Scripting.Dictionary")
                      objComputerAccs.CompareMode = vbTextCompare : GetComputerAccounts
                      
                      ' Loop through your list with computer names.
                      inputFile = "c:\test.txt"
                      Const ForReading =  1 
                      Set fso = CreateObject("Scripting.FileSystemObject") 
                      
                      On Error Resume Next
                      Set ts = fso.OpenTextFile(inputFile, ForReading)
                      Do until ts.AtEndOfStream
                         s = trim(ts.ReadLine)
                         If len(s) > 0 Then
                           strComputer = trim(split(s, " ")(0))
                           strComputer = lcase(strComputer)
                           If (objComputerAccs.Exists(strComputer) = True) Then
                             wsh.echo "Nb Accountname:", s, "/ Distinguished Name:" _
                               & vbNewLine & objComputerAccs.Item(strComputer) _
                               & vbNewLine
                           End If
                         End If
                      Loop
                      ts.Close: Set ts = Nothing: Set fso = Nothing
                      wscript.quit
                      
                      Sub GetComputerAccounts
                         Const ADS_SCOPE_SUBTREE = 2
                      
                         objComputerAccs.RemoveAll
                      
                         Set objRootDSE = GetObject("LDAP://RootDSE")
                         strRootDSE = objRootDSE.Get("DefaultNamingContext")
                      
                         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_SUBTREE 
                      
                         strCommand = "SELECT sAMAccountName, distinguishedName " _
                            & "FROM 'LDAP://" & strRootDSE _
                            & "' WHERE ObjectCategory='computer'"  
                         objCommand.CommandText = strCommand
                         Set objRecordSet = objCommand.Execute
                      
                         If not objRecordSet.eof then
                           objRecordSet.MoveFirst
                           Do Until objRecordSet.EOF
                             accName = Empty : strDN = Empty
                             accName = objRecordSet.Fields("sAMAccountname").Value
                             accName = Trim(LEFT(accName,LEN(accName)-1))
                             strDN = Trim(objRecordSet.Fields("distinguishedName").Value)
                             If len(strDN) = 0 Then strDN = False
                             If len(accName) > 0 Then
                               If (objComputerAccs.Exists(accName) = False) Then
                                 objComputerAccs.Add LCase(accName), strDN
                      '         Else
                      '           msgbox "ERROR - Computer with a non-unique name found!!" _
                      '            & vbNewLine & accName & " - " & strDN & vbNewLine _
                      '            & "Double with " & objComputerAccs.Item(accName)
                               End If
                             End If
                             objRecordSet.MoveNext
                           Loop
                         End If
                         objConnection.Close
                      End Sub
                      \Rems



                      EDIT
                      Similar thread by vivekmohan where DSQUERY.exe is executed from within a vbscript instead of using ADO.
                      _
                      Last edited by Rems; 23rd July 2009, 17:22.

                      This posting is provided "AS IS" with no warranties, and confers no rights.

                      __________________

                      ** Remember to give credit where credit's due **
                      and leave Reputation Points for meaningful posts

                      Comment


                      • #12
                        Re: need vbscript to promote fully qualified name of server

                        hi rems,

                        now the code is working fine, now i do have another doubts

                        if i run this script i am not getting the nested groups(recursive function), i need to get nested group of nested groups

                        what need to edited to get the result


                        On Error Resume Next
                        SetobjUser=GetObject("ldap://"Distinguish name")
                        Set colGroups = objUser.Groups
                        For Each objGroup in colGroups
                        Wscript.Echo objGroup.CN
                        GetNested(objGroup)
                        Next
                        Function GetNested(objGroup)
                        On Error Resume Next
                        colMembers = objGroup.GetEx("memberOf")
                        For Each strMember in colMembers
                        strPath = "LDAP://" & strMember
                        Set objNestedGroup = _
                        GetObject(strPath)
                        WScript.Echo objNestedGroup.CN
                        GetNested(objNestedGroup)
                        Next
                        End Function

                        Comment

                        Working...
                        X